So, of course now we got authorization added, finally it's now also time to make sure
that we can also register for events and not just create them.
Hence we need more event-related routes and for that I'll go to the events.js file in
the routes folder.
And here I want to add more routes like a post route which can be used for signing up
and again, thankfully, GitHub Copilot is pretty smart here and it suggests a route which makes
a lot of sense, though I'll name it register, not attend.
And this attend function on the events controller object doesn't exist yet, but I'll add
it soon.
I'll also enter a new line so that GitHub Copilot suggests the unregister route which
looks good.
And now I just need to add a register and unregister controller function to the event
controller, so to the events controller.js file.
So in there at the bottom, I'll just place my cursor here, wait a couple of seconds and
again, since GitHub Copilot tries to keep your last actions in mind and keep it in its
context, so to say, I get a good suggestion here, a function named register, which is
exported, which seems to do the job I want it to do.
It extracts the ID from the request parameters, finds the event with that ID.
If there is no such event, it returns an error.
Otherwise it tries to insert an entry in the registrations table, which we have yet to
set up, but it tries to enter an entry there where we have basically a combination of event
ID and user ID.
And that is essentially a registration, a certain user registered for a certain event.
Then this is executed and then we get different responses.
Now we also need an unregister function.
Let's see if this is suggested as well.
Yep, looks like it is for me here.
If it's not, you can of course simply start typing unreg and at some point it should be
suggested and here we do almost the same as before, but we delete a registration by the
event ID user ID combination here.
And the user ID is retrieved from the request.
And it should be available there because in the routes file, the authenticate middleware
is added here as well.
And that does extract the user ID from the attached token.
So that should work, but of course we must make sure that the appropriate database table
is set up.
So for that, I'll go to the database.js file and here where I set up the other tables,
I'll enter a new line and see if I got some suggestions.
If not, I'll start typing.
Create table if not exists registrations.
Sure, let's go step by step.
Here we go.
Let's accept this and this looks good to me.
Event ID, user ID, both are foreign keys pointing to other tables.
That looks good.
To make sure that this is really executed, you can restart the server, though it should
be picked up automatically due to watch mode normally.
With that done, you should be able to send those registration requests.