Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,010 --> 00:00:02,530
We finished up our post service.
2
00:00:02,570 --> 00:00:07,580
So now we're going to put together an initial implementation of our comments service as well just as
3
00:00:07,580 --> 00:00:08,270
we did before.
4
00:00:08,300 --> 00:00:13,160
It's gonna be very much be worth our time to really hash out exactly what we want our comments service
5
00:00:13,160 --> 00:00:18,590
to do so I took a quick stab at how we might want to implement this comment service.
6
00:00:18,620 --> 00:00:20,480
I think that we're going to have two different root handlers.
7
00:00:20,480 --> 00:00:25,340
Once again we're going to have one row handler that's going to allow us to create a new comment that
8
00:00:25,340 --> 00:00:28,190
is associated with some very particular post.
9
00:00:28,340 --> 00:00:33,050
So you'll notice that inside the path right here we're going to make a request to post slash and then
10
00:00:33,050 --> 00:00:39,490
the I.D. of the post that we're going to associate this comment with when we make a post request.
11
00:00:39,490 --> 00:00:44,320
We're going to have a body that has a content that is some string so content is going to be the actual
12
00:00:44,320 --> 00:00:46,000
text of the comment itself.
13
00:00:47,240 --> 00:00:51,320
We're then going to have a second row handler that's went to give us all of the different comments that
14
00:00:51,320 --> 00:00:57,020
are associated with some very particular post I.D. so make a get request to that route.
15
00:00:57,020 --> 00:01:00,550
That will give us all the comments associate with some given post I.D..
16
00:01:00,560 --> 00:01:04,540
So pretty straightforward but we are going to have just a little bit more complexity on the service.
17
00:01:04,550 --> 00:01:06,560
I'll tell you why in just a moment right now.
18
00:01:06,590 --> 00:01:11,880
Let's open up our code editor and start to put this thing together it's going to look backward my terminal.
19
00:01:11,970 --> 00:01:15,140
I'm gonna open up my code editor inside of my comments directory
20
00:01:17,800 --> 00:01:18,880
inside of here right away.
21
00:01:18,910 --> 00:01:25,490
I'll make an index dot j ust file and at the very top we'll write out a little bit of boilerplate very
22
00:01:25,490 --> 00:01:28,360
similar to what we ended up with inside of our postal service.
23
00:01:28,370 --> 00:01:34,770
Just a moment ago it's all get Express.
24
00:01:35,000 --> 00:01:36,170
I'll get a body parser
25
00:01:39,860 --> 00:01:47,590
and I'll get that random bytes function that we're using to just generate random ideas.
26
00:01:47,600 --> 00:01:57,340
Next up going to create my express application I'll associate the body parser middleware with its and
27
00:01:57,390 --> 00:01:59,770
I'll put together are two root handlers.
28
00:01:59,850 --> 00:02:06,720
So we want to have a root handler that will deal with a get request to posts i.e. comments
29
00:02:09,580 --> 00:02:13,420
and of course make sure that you've got that colon right before I.D..
30
00:02:13,420 --> 00:02:14,020
Right there
31
00:02:17,930 --> 00:02:19,160
and we could do something very similar.
32
00:02:19,170 --> 00:02:21,000
Daniel Post requests as well.
33
00:02:21,000 --> 00:02:27,750
So we're gonna look for a post request to post a line I.D. comments.
34
00:02:27,860 --> 00:02:28,790
Very good.
35
00:02:28,830 --> 00:02:34,370
And then finally let's make sure that our Express application listens on some court so say Abdul listen
36
00:02:34,760 --> 00:02:39,950
and remember we already have another service listening on Port 4000 on our computer.
37
00:02:39,950 --> 00:02:45,500
So we have to put in another unique port right here in my case I got to put in four thousand and one
38
00:02:45,590 --> 00:02:56,950
like so once I start listening on there I'll do a console log of listening on four thousand one okay.
39
00:02:57,110 --> 00:03:03,670
So good start now the next we need to do is that in some actual implementation or getting all the post
40
00:03:03,670 --> 00:03:09,610
with some given I.D. and all the to me all the comments associated with some post I.D. and creating
41
00:03:09,610 --> 00:03:11,000
a new common as well.
42
00:03:11,020 --> 00:03:16,710
Let's first begin with creating a new comment just as we had back on our post micros service.
43
00:03:16,720 --> 00:03:21,580
We're gonna store all of our comments instead of an in-memory data structure so just like we had previously
44
00:03:21,710 --> 00:03:29,350
I'm going to create a new object right here called comments by post I.D. and I'll make that an object.
45
00:03:29,360 --> 00:03:31,690
You'll notice I'm calling the thing by post I.D..
46
00:03:32,090 --> 00:03:37,070
The reason for that is that we really want this data structure to be optimized for looking up all of
47
00:03:37,070 --> 00:03:41,960
the different comments associated with a given post so we can have a slightly more complicated data
48
00:03:41,960 --> 00:03:44,950
structure right here than what we had back on our post service.
49
00:03:44,960 --> 00:03:50,040
Let me show you a diagram to help you understand what this object is going to really look like.
50
00:03:50,080 --> 00:03:50,380
All right.
51
00:03:50,470 --> 00:03:51,180
So here's the idea.
52
00:03:51,210 --> 00:03:52,840
Here's what we're going to do that.
53
00:03:52,840 --> 00:03:56,790
Comments by post I.D. is going to look like this diagram right here.
54
00:03:56,830 --> 00:03:58,090
It's going to have some keys.
55
00:03:58,090 --> 00:04:00,090
Of course because it is an object.
56
00:04:00,090 --> 00:04:06,810
And those keys are going to be the I.D. of a post that has been created inside of application then for
57
00:04:06,810 --> 00:04:07,890
each of those keys.
58
00:04:07,890 --> 00:04:15,510
We're going to have an array of comments so here's comment number one comment number two and so on.
59
00:04:15,510 --> 00:04:20,610
So this is going to make it really really easy and straightforward to look up all the comments associated
60
00:04:20,610 --> 00:04:22,890
with some given post by the Post I.D..
61
00:04:22,920 --> 00:04:28,290
All we have to do is reach into the comments by post I.D. object look up the given idea of the post
62
00:04:28,600 --> 00:04:33,810
and that will give us back an array of all the comments that are associated with that post.
63
00:04:33,900 --> 00:04:39,170
Pretty straightforward to do that look at OK so now we have a better idea of what this object right
64
00:04:39,170 --> 00:04:40,260
here is going to look like.
65
00:04:40,280 --> 00:04:44,270
Let's start making use of it inside of our poster request handler inside of here.
66
00:04:44,270 --> 00:04:48,980
The first thing I'm going to do is generate a new comment I.D. This is going to be just a random I.D.
67
00:04:49,040 --> 00:04:52,590
so we can identify this comment at some point in the future if we need to.
68
00:04:52,600 --> 00:05:00,560
So I'll say comment I.D. is going to be that random bytes function we're gonna call it and get a or
69
00:05:00,590 --> 00:05:04,160
random bytes generated and then convert that to a string.
70
00:05:04,160 --> 00:05:06,130
That is a hexadecimal string.
71
00:05:06,170 --> 00:05:13,600
So again just a randomly generated ideas next up will pull out the content property that is provided
72
00:05:13,600 --> 00:05:15,530
by the user inside of our incoming request.
73
00:05:15,550 --> 00:05:19,960
Because remember we had said that anytime soon makes a post request we're going gonna have a body that
74
00:05:19,960 --> 00:05:29,850
is a content that has some string so I'll get content from wreck dot body now.
75
00:05:29,850 --> 00:05:35,640
Next up we're going to take a look at our comments by I.D. by post idea object right here.
76
00:05:35,700 --> 00:05:42,780
We're gonna check to see if we've already got some array inside there or the given post I.D. so I'll
77
00:05:42,780 --> 00:05:49,800
say const comments is going to be comments by post I.D. and we'll look up the post I.D. that was provided
78
00:05:49,800 --> 00:05:50,980
inside the URL.
79
00:05:51,120 --> 00:05:51,510
We'll do.
80
00:05:51,510 --> 00:05:58,940
Rick brands dot I.D. and that will give us either in array or undefined.
81
00:05:58,990 --> 00:06:03,410
It will give us undefined if we've never had a comment as created that is associate with this post before
82
00:06:03,950 --> 00:06:06,000
this might be undefined right here.
83
00:06:06,050 --> 00:06:11,360
So to guard against that I'll say if this results in undefined then just give me by default an empty
84
00:06:11,360 --> 00:06:14,850
array.
85
00:06:14,860 --> 00:06:19,060
So now we're going to push in the new comment that we're trying to create into this comments array will
86
00:06:19,060 --> 00:06:28,020
say comments that push and I want to push in an idea of common I.D. and the content that the user just
87
00:06:28,020 --> 00:06:34,880
provided and I'll make sure that I assign this comments array back to the given post inside of our comments
88
00:06:34,880 --> 00:06:35,880
by post I.D. object.
89
00:06:35,880 --> 00:06:47,070
So we'll say comments by post I.D. at Rick brands I.D. equals comments and that should be it nonetheless
90
00:06:47,100 --> 00:06:48,200
thing that we can do right here.
91
00:06:48,210 --> 00:06:52,670
We'll send back just the entire array of comments we could send back just the comment that was created.
92
00:06:52,770 --> 00:06:56,180
But what the heck let's just send back the entire array of comments.
93
00:06:56,230 --> 00:07:04,290
I'll do a rez that status to a one and I'll send back the entire array of comments get.
94
00:07:04,320 --> 00:07:06,110
So there's our post request handler.
95
00:07:06,110 --> 00:07:10,300
Now we'll put together our get request handler which is going to be a way easier.
96
00:07:10,300 --> 00:07:17,660
So up here we'll do a rez that sent.
97
00:07:17,850 --> 00:07:21,510
We're going to look inside of our comments by post I.D. object.
98
00:07:21,930 --> 00:07:23,320
I'm going to look up inside there.
99
00:07:23,340 --> 00:07:29,830
The idea that was provided inside the path so we'll do a wreck per Rams I.D. and then once again if
100
00:07:29,830 --> 00:07:35,650
that results in undefined let's guard against that was put in and or empty array like so so that no
101
00:07:35,650 --> 00:07:41,670
matter what we always send back an array to whoever is making this request OK.
102
00:07:41,710 --> 00:07:43,030
So it looks good.
103
00:07:43,180 --> 00:07:44,630
It's going to say this file.
104
00:07:44,760 --> 00:07:47,580
The last thing I'm going to do is open up my package not just on file.
105
00:07:47,590 --> 00:07:52,480
We're going to find a script inside of there as well for starting up our project with Node 1.
106
00:07:52,510 --> 00:07:59,920
So here's my package that Chase on file and then replace the START script or create a start script.
107
00:07:59,930 --> 00:08:10,150
I should say and we'll do a node on index dot J.S. Well that should be it for our common service.
108
00:08:10,150 --> 00:08:11,800
So once again let's take a quick pause right here.
109
00:08:11,800 --> 00:08:17,530
We'll come back the next video and just do a very quick little manual test with postman post ask me
110
00:08:17,740 --> 00:08:21,880
post man not node 1 post man to make sure that this is all working as expected.
12004
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.