WEBVTT

1
00:00:03.939 --> 00:00:07.340
So, I'll log in with my second user here again.

2
00:00:08.780 --> 00:00:10.220
Technically that's not required.

3
00:00:10.540 --> 00:00:12.660
I could also sign up for my own events.

4
00:00:13.040 --> 00:00:16.200
You could add code to prevent this, but here I'm fine with it.

5
00:00:16.400 --> 00:00:18.040
But I'll use the second user nonetheless.

6
00:00:20.200 --> 00:00:29.180
And then here, create a new request, a new post request targeting localhost 3000 slash

7
00:00:30.120 --> 00:00:32.759
the event with ID 1 slash register.

8
00:00:35.120 --> 00:00:41.660
And then add my header, my authorization header, which is Bera and then my token.

9
00:00:43.000 --> 00:00:50.680
And I don't need to add any extra request body because the event ID is encoded in the

10
00:00:50.680 --> 00:00:51.040
URL.

11
00:00:51.700 --> 00:00:53.420
The token is attached as a header.

12
00:00:54.819 --> 00:00:58.620
And that's all this route and this controller function needs.

13
00:00:59.400 --> 00:01:03.380
The register controller function gets the event ID from the request parameters.

14
00:01:04.739 --> 00:01:10.620
So from the path, the request is sent to and the user ID from the request object where

15
00:01:10.620 --> 00:01:15.340
it's set by that authentication middleware, which extracts it from the token.

16
00:01:16.880 --> 00:01:17.960
So that should work.

17
00:01:18.480 --> 00:01:21.940
If I send this, I get an error though.

18
00:01:22.800 --> 00:01:24.400
Get database is not defined.

19
00:01:27.800 --> 00:01:33.040
So yeah, that code here fails where I try to get access to the database.

20
00:01:34.680 --> 00:01:40.580
And this makes sense because for all the other controller functions, I actually don't have

21
00:01:40.580 --> 00:01:47.500
the database related work here in this events controller JS file, but instead in the event

22
00:01:47.500 --> 00:01:48.380
JS file.

23
00:01:49.970 --> 00:01:55.800
Here I'm reaching out to the database and now for the register and unregister functions,

24
00:01:55.880 --> 00:01:56.620
I change that.

25
00:01:57.220 --> 00:01:58.940
I actually don't like that change.

26
00:01:59.260 --> 00:02:06.140
So instead I'll go to that event JS file in the models folder and add my functions there.

27
00:02:06.520 --> 00:02:11.900
And again, get up copilot thankfully makes some good suggestions here, register user

28
00:02:11.900 --> 00:02:17.420
for event, unregister user from event, get all the database related code in there.

29
00:02:18.900 --> 00:02:24.200
And with those changes made, we can now adjust the code here in this register function, for

30
00:02:24.280 --> 00:02:24.740
example.

31
00:02:25.880 --> 00:02:35.520
Instead of reaching out to the database here, we can use register user for event from this

32
00:02:35.520 --> 00:02:37.560
event JS file in the models folder.

33
00:02:38.940 --> 00:02:41.620
I started typing to get a good suggestion here.

34
00:02:42.720 --> 00:02:48.500
I just want to make sure that it's imported so that it's added to the imports up here.

35
00:02:49.960 --> 00:02:52.360
Same with unregister user from event.

36
00:02:54.079 --> 00:03:01.220
And then I just need the success information here to send back the appropriate response.

37
00:03:02.959 --> 00:03:04.680
And I'll do the same down here.

38
00:03:05.800 --> 00:03:13.760
Get my success information by calling unregister user from event like this.

39
00:03:14.680 --> 00:03:15.040
Okay.

40
00:03:15.180 --> 00:03:16.280
That should do the trick.

41
00:03:17.520 --> 00:03:21.480
Successes this Boolean, which is returned for both functions.

42
00:03:23.200 --> 00:03:28.820
And with that, if we save this and we try this same request, this register request from

43
00:03:28.820 --> 00:03:32.020
before again, now we got registered successfully.

44
00:03:34.000 --> 00:03:40.120
Now if I sent this again, I, well, register again, doesn't make too much sense.

45
00:03:40.400 --> 00:03:46.820
We could try to prevent this, but here to keep this app simple, I'll just, well, set

46
00:03:46.800 --> 00:03:47.600
it up like this.

47
00:03:49.180 --> 00:03:57.280
And instead I'll now add a second request here, a delete request, which will actually

48
00:03:57.280 --> 00:04:00.900
target this route here.

49
00:04:02.540 --> 00:04:07.880
And actually this should be a delete request here since unregistering is essentially, well,

50
00:04:08.280 --> 00:04:09.220
deleting something.

51
00:04:12.280 --> 00:04:23.540
So a delete request sent to HTTP localhost 3000 slash event slash one because we're talking

52
00:04:23.540 --> 00:04:26.800
about the event with ID one slash unregister.

53
00:04:28.680 --> 00:04:37.900
And then again, add some headers, the authorization header, bearer, and then grab that token.

54
00:04:38.720 --> 00:04:46.700
Let's grab it here from the log in request like this, send this, and we unregistered.

55
00:04:47.860 --> 00:04:51.340
Now I can do this as often as I want as well.

56
00:04:51.640 --> 00:04:56.200
At some point I'll just get unregistration failed because, well, I am not registered

57
00:04:56.200 --> 00:04:56.600
anymore.

58
00:04:58.100 --> 00:05:00.300
But in general, this works the way it should.

59
00:05:01.100 --> 00:05:05.660
And as mentioned, you could of course tweak this, make it more granular, add extra checks,

60
00:05:05.940 --> 00:05:07.760
but in general, this is working the way it should.

