Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,290 --> 00:00:03,700
After get and post, let's all learn
2
00:00:03,700 --> 00:00:05,320
how to handle patch requests
3
00:00:05,320 --> 00:00:07,273
to actually update data.
4
00:00:08,350 --> 00:00:10,420
And I'm pretty sure that at this point,
5
00:00:10,420 --> 00:00:11,960
you already have a good idea
6
00:00:11,960 --> 00:00:13,460
of how we're going to do that.
7
00:00:14,670 --> 00:00:16,853
So let's do that after post,
8
00:00:18,260 --> 00:00:19,653
so down here,
9
00:00:20,840 --> 00:00:24,150
and remember that we actually have two http methods
10
00:00:24,150 --> 00:00:25,400
to update data.
11
00:00:25,400 --> 00:00:27,690
We have put and we have patch.
12
00:00:27,690 --> 00:00:30,580
And with put, we expect that our application
13
00:00:30,580 --> 00:00:33,770
receives the entire new updated object,
14
00:00:33,770 --> 00:00:36,854
and with patch, we only expect the properties
15
00:00:36,854 --> 00:00:40,670
that should actually be updated on the object, all right?
16
00:00:40,670 --> 00:00:42,540
So usually, I like to use patch,
17
00:00:42,540 --> 00:00:46,290
because I find it easier to simply update the properties
18
00:00:46,290 --> 00:00:47,950
that were updated.
19
00:00:47,950 --> 00:00:51,000
At least, when we start using MongoDB and Mongoose,
20
00:00:51,000 --> 00:00:54,690
it will be much easier to just do it like that, all right?
21
00:00:54,690 --> 00:00:56,410
And it's alsp easier for the user
22
00:00:56,410 --> 00:00:58,850
to simply send the data that is changing,
23
00:00:58,850 --> 00:01:01,723
instead of having to send the entire new object.
24
00:01:02,640 --> 00:01:05,000
So again, we are going to make our app work
25
00:01:05,000 --> 00:01:07,033
for patch and not for put.
26
00:01:08,100 --> 00:01:12,460
So we expect a patch request to come in on the URL
27
00:01:13,335 --> 00:01:18,335
just like before, "api/v1/tours,"
28
00:01:19,760 --> 00:01:22,490
and now we actually also need the ID
29
00:01:22,490 --> 00:01:24,910
of the tour that should be updated.
30
00:01:24,910 --> 00:01:27,610
And so that is slash and then "id."
31
00:01:27,610 --> 00:01:30,853
Okay, so that's what we learned in the last lecture.
32
00:01:33,510 --> 00:01:36,830
So, what do we want to do when there is a patch request,
33
00:01:36,830 --> 00:01:39,260
so when we want to update the data?
34
00:01:39,260 --> 00:01:41,400
Well, actually, I'm not really gonna implement
35
00:01:41,400 --> 00:01:44,160
this operation here, because that's just a matter
36
00:01:44,160 --> 00:01:45,750
of writing some more JavaScript
37
00:01:45,750 --> 00:01:47,670
that is not really important,
38
00:01:47,670 --> 00:01:52,070
because, again, this is just testing API-using files.
39
00:01:52,070 --> 00:01:54,870
In the real world, we're never gonna use files for that
40
00:01:54,870 --> 00:01:56,280
anyway, okay.
41
00:01:56,280 --> 00:01:59,020
So, I'm really just implementing all of these verbs here
42
00:01:59,020 --> 00:02:01,840
so that you get a good idea of the different verbs
43
00:02:01,840 --> 00:02:03,405
that we use with http,
44
00:02:03,405 --> 00:02:06,730
the kind of status codes that we send back,
45
00:02:06,730 --> 00:02:10,220
for example, here we sent 201,
46
00:02:10,220 --> 00:02:13,720
while here we sent 200 and 400,
47
00:02:13,720 --> 00:02:16,410
and also for example, here,
48
00:02:16,410 --> 00:02:19,343
when we had multiple tours, we sent the results,
49
00:02:20,370 --> 00:02:24,170
and when we created a new tour, we sent that data back
50
00:02:24,170 --> 00:02:28,020
immediately, with this newTour that was created,
51
00:02:28,020 --> 00:02:30,410
and so that's the kind of stuff that I want you to learn
52
00:02:30,410 --> 00:02:32,290
for now, in this section.
53
00:02:32,290 --> 00:02:35,600
So the basics of working with the Express, of course,
54
00:02:35,600 --> 00:02:39,270
and also, the correct way of sending back API responses,
55
00:02:39,270 --> 00:02:42,960
okay, and so, again, I'm not going to implement the updating
56
00:02:42,960 --> 00:02:45,540
of tour here, because that would be a lot of work,
57
00:02:45,540 --> 00:02:49,059
you would have to get tour from the JSON file,
58
00:02:49,059 --> 00:02:53,360
than change that tour and then save it again to the file.
59
00:02:53,360 --> 00:02:55,370
And that's a bit too much work here,
60
00:02:55,370 --> 00:02:57,130
and so let's simply go out
61
00:02:57,130 --> 00:02:59,690
and send back a standard response.
62
00:02:59,690 --> 00:03:04,517
So "response.status,"
63
00:03:05,720 --> 00:03:08,100
and we will still use 200 here,
64
00:03:08,100 --> 00:03:10,910
so when we update an object or a resource,
65
00:03:10,910 --> 00:03:12,603
we send back 200, okay,
66
00:03:13,532 --> 00:03:16,210
and then the usual JSON
67
00:03:18,810 --> 00:03:22,453
with the status of success.
68
00:03:24,190 --> 00:03:26,890
And then we send back the data, and in this case,
69
00:03:26,890 --> 00:03:28,163
the updated tour.
70
00:03:30,650 --> 00:03:33,360
So we say "tour," and then here, in this position,
71
00:03:33,360 --> 00:03:36,880
we would send back the updated tour, okay.
72
00:03:36,880 --> 00:03:40,933
Right now, I'm going to send back a string which says like,
73
00:03:43,677 --> 00:03:46,650
"updated tour here...," something like this.
74
00:03:46,650 --> 00:03:48,950
So basically, just a placeholder here.
75
00:03:48,950 --> 00:03:53,720
And we can also kind of implement this code here,
76
00:03:53,720 --> 00:03:57,490
so in order to only send this when the ID is actually valid,
77
00:03:57,490 --> 00:04:01,590
so let's get that from here,
78
00:04:01,590 --> 00:04:06,590
and so this way, we can also actually use this ID here.
79
00:04:06,790 --> 00:04:07,623
Right.
80
00:04:08,890 --> 00:04:10,440
So we're not gonna do this one
81
00:04:10,440 --> 00:04:13,653
because we don't have any tour, but we will use this one.
82
00:04:15,060 --> 00:04:20,040
So this will be "req.params.id,"
83
00:04:20,040 --> 00:04:24,410
and I'm converting it to a number here as well, times 1,
84
00:04:24,410 --> 00:04:27,230
and if it is greater than the tour's length,
85
00:04:27,230 --> 00:04:29,980
well then just like before, it is an invalid ID,
86
00:04:29,980 --> 00:04:33,000
and we send a 404, for not found.
87
00:04:33,000 --> 00:04:36,283
And so like this, we should now be ready to test it.
88
00:04:38,560 --> 00:04:41,740
So, let's first of all save the request
89
00:04:41,740 --> 00:04:43,540
that we did in the previous lecture.
90
00:04:45,510 --> 00:04:48,667
So, just "Get Tour."
91
00:04:50,120 --> 00:04:53,163
And I'm just going to go ahead and copy this one here.
92
00:04:58,100 --> 00:05:02,190
And let's specify the patch method.
93
00:05:02,190 --> 00:05:05,580
So this one's patch, and I will save it here right away as
94
00:05:08,347 --> 00:05:09,427
"Update Tour."
95
00:05:11,180 --> 00:05:14,250
Okay, and you see this one here gets this gray text,
96
00:05:14,250 --> 00:05:16,770
so they're very easy to distinguish here
97
00:05:16,770 --> 00:05:18,623
in this side column.
98
00:05:20,330 --> 00:05:24,100
So we actually should specify some body here,
99
00:05:24,100 --> 00:05:26,350
so let's just go ahead and copy it from here,
100
00:05:29,300 --> 00:05:34,300
so remember, body, raw, and then JSON,
101
00:05:34,506 --> 00:05:35,653
just like this,
102
00:05:36,600 --> 00:05:39,150
and so, let's say that all that I want to do
103
00:05:39,150 --> 00:05:42,733
is to change the tour number three, let's say,
104
00:05:43,600 --> 00:05:47,320
so let's take a look at that one, so, one, two, three,
105
00:05:47,320 --> 00:05:49,570
and I want to change the duration to fifteen.
106
00:05:53,330 --> 00:05:55,133
Okay. Saving it again.
107
00:05:56,410 --> 00:05:57,940
I'm going to save this one as well,
108
00:05:57,940 --> 00:06:01,910
because then it will actually save this text here in a body,
109
00:06:01,910 --> 00:06:04,770
and if I close it and open it up again later,
110
00:06:04,770 --> 00:06:06,730
then all this text here is back,
111
00:06:06,730 --> 00:06:09,083
and that will make my life a lot easier then.
112
00:06:10,450 --> 00:06:12,410
Okay, but anyway, back to this one.
113
00:06:12,410 --> 00:06:14,270
We set the duration to fifteen.
114
00:06:14,270 --> 00:06:16,100
Now, keep in mind, that this will of course
115
00:06:16,100 --> 00:06:18,030
not change anything in the data
116
00:06:18,030 --> 00:06:20,417
because we did not implement that.
117
00:06:20,417 --> 00:06:23,980
Okay, so I'm sending the response now.
118
00:06:23,980 --> 00:06:27,808
And so, here we see "updated_tour_here...,"
119
00:06:27,808 --> 00:06:30,070
just in the place where in the real world
120
00:06:30,070 --> 00:06:33,130
we would then get the updated data back.
121
00:06:33,130 --> 00:06:36,083
Right, just to test it with a wrong ID,
122
00:06:37,050 --> 00:06:40,160
we get this "fail," and this "Invalid ID."
123
00:06:40,160 --> 00:06:43,750
Okay, so give it a save and close it up,
124
00:06:43,750 --> 00:06:46,870
and I'm going to close up this one as well.
125
00:06:46,870 --> 00:06:50,830
Okay, so almost done with the CRUD operations,
126
00:06:50,830 --> 00:06:54,864
so create, read, update, and delete, and as you can hear,
127
00:06:54,864 --> 00:06:57,560
the last one that's missing is delete,
128
00:06:57,560 --> 00:07:00,010
and so let's take care of that in the next video.
10085
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.