So, I'll log in with my second user here again.
Technically that's not required.
I could also sign up for my own events.
You could add code to prevent this, but here I'm fine with it.
But I'll use the second user nonetheless.
And then here, create a new request, a new post request targeting localhost 3000 slash
the event with ID 1 slash register.
And then add my header, my authorization header, which is Bera and then my token.
And I don't need to add any extra request body because the event ID is encoded in the
URL.
The token is attached as a header.
And that's all this route and this controller function needs.
The register controller function gets the event ID from the request parameters.
So from the path, the request is sent to and the user ID from the request object where
it's set by that authentication middleware, which extracts it from the token.
So that should work.
If I send this, I get an error though.
Get database is not defined.
So yeah, that code here fails where I try to get access to the database.
And this makes sense because for all the other controller functions, I actually don't have
the database related work here in this events controller JS file, but instead in the event
JS file.
Here I'm reaching out to the database and now for the register and unregister functions,
I change that.
I actually don't like that change.
So instead I'll go to that event JS file in the models folder and add my functions there.
And again, get up copilot thankfully makes some good suggestions here, register user
for event, unregister user from event, get all the database related code in there.
And with those changes made, we can now adjust the code here in this register function, for
example.
Instead of reaching out to the database here, we can use register user for event from this
event JS file in the models folder.
I started typing to get a good suggestion here.
I just want to make sure that it's imported so that it's added to the imports up here.
Same with unregister user from event.
And then I just need the success information here to send back the appropriate response.
And I'll do the same down here.
Get my success information by calling unregister user from event like this.
Okay.
That should do the trick.
Successes this Boolean, which is returned for both functions.
And with that, if we save this and we try this same request, this register request from
before again, now we got registered successfully.
Now if I sent this again, I, well, register again, doesn't make too much sense.
We could try to prevent this, but here to keep this app simple, I'll just, well, set
it up like this.
And instead I'll now add a second request here, a delete request, which will actually
target this route here.
And actually this should be a delete request here since unregistering is essentially, well,
deleting something.
So a delete request sent to HTTP localhost 3000 slash event slash one because we're talking
about the event with ID one slash unregister.
And then again, add some headers, the authorization header, bearer, and then grab that token.
Let's grab it here from the log in request like this, send this, and we unregistered.
Now I can do this as often as I want as well.
At some point I'll just get unregistration failed because, well, I am not registered
anymore.
But in general, this works the way it should.
And as mentioned, you could of course tweak this, make it more granular, add extra checks,
but in general, this is working the way it should.