We finished up our post service.
So now we're going to put together an initial implementation of our comments service as well just as
we did before.
It's gonna be very much be worth our time to really hash out exactly what we want our comments service
to do so I took a quick stab at how we might want to implement this comment service.
I think that we're going to have two different root handlers.
Once again we're going to have one row handler that's going to allow us to create a new comment that
is associated with some very particular post.
So you'll notice that inside the path right here we're going to make a request to post slash and then
the I.D. of the post that we're going to associate this comment with when we make a post request.
We're going to have a body that has a content that is some string so content is going to be the actual
text of the comment itself.
We're then going to have a second row handler that's went to give us all of the different comments that
are associated with some very particular post I.D. so make a get request to that route.
That will give us all the comments associate with some given post I.D..
So pretty straightforward but we are going to have just a little bit more complexity on the service.
I'll tell you why in just a moment right now.
Let's open up our code editor and start to put this thing together it's going to look backward my terminal.
I'm gonna open up my code editor inside of my comments directory
inside of here right away.
I'll make an index dot j ust file and at the very top we'll write out a little bit of boilerplate very
similar to what we ended up with inside of our postal service.
Just a moment ago it's all get Express.
I'll get a body parser
and I'll get that random bytes function that we're using to just generate random ideas.
Next up going to create my express application I'll associate the body parser middleware with its and
I'll put together are two root handlers.
So we want to have a root handler that will deal with a get request to posts i.e. comments
and of course make sure that you've got that colon right before I.D..
Right there
and we could do something very similar.
Daniel Post requests as well.
So we're gonna look for a post request to post a line I.D. comments.
Very good.
And then finally let's make sure that our Express application listens on some court so say Abdul listen
and remember we already have another service listening on Port 4000 on our computer.
So we have to put in another unique port right here in my case I got to put in four thousand and one
like so once I start listening on there I'll do a console log of listening on four thousand one okay.
So good start now the next we need to do is that in some actual implementation or getting all the post
with some given I.D. and all the to me all the comments associated with some post I.D. and creating
a new common as well.
Let's first begin with creating a new comment just as we had back on our post micros service.
We're gonna store all of our comments instead of an in-memory data structure so just like we had previously
I'm going to create a new object right here called comments by post I.D. and I'll make that an object.
You'll notice I'm calling the thing by post I.D..
The reason for that is that we really want this data structure to be optimized for looking up all of
the different comments associated with a given post so we can have a slightly more complicated data
structure right here than what we had back on our post service.
Let me show you a diagram to help you understand what this object is going to really look like.
All right.
So here's the idea.
Here's what we're going to do that.
Comments by post I.D. is going to look like this diagram right here.
It's going to have some keys.
Of course because it is an object.
And those keys are going to be the I.D. of a post that has been created inside of application then for
each of those keys.
We're going to have an array of comments so here's comment number one comment number two and so on.
So this is going to make it really really easy and straightforward to look up all the comments associated
with some given post by the Post I.D..
All we have to do is reach into the comments by post I.D. object look up the given idea of the post
and that will give us back an array of all the comments that are associated with that post.
Pretty straightforward to do that look at OK so now we have a better idea of what this object right
here is going to look like.
Let's start making use of it inside of our poster request handler inside of here.
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.
so we can identify this comment at some point in the future if we need to.
So I'll say comment I.D. is going to be that random bytes function we're gonna call it and get a or
random bytes generated and then convert that to a string.
That is a hexadecimal string.
So again just a randomly generated ideas next up will pull out the content property that is provided
by the user inside of our incoming request.
Because remember we had said that anytime soon makes a post request we're going gonna have a body that
is a content that has some string so I'll get content from wreck dot body now.
Next up we're going to take a look at our comments by I.D. by post idea object right here.
We're gonna check to see if we've already got some array inside there or the given post I.D. so I'll
say const comments is going to be comments by post I.D. and we'll look up the post I.D. that was provided
inside the URL.
We'll do.
Rick brands dot I.D. and that will give us either in array or undefined.
It will give us undefined if we've never had a comment as created that is associate with this post before
this might be undefined right here.
So to guard against that I'll say if this results in undefined then just give me by default an empty
array.
So now we're going to push in the new comment that we're trying to create into this comments array will
say comments that push and I want to push in an idea of common I.D. and the content that the user just
provided and I'll make sure that I assign this comments array back to the given post inside of our comments
by post I.D. object.
So we'll say comments by post I.D. at Rick brands I.D. equals comments and that should be it nonetheless
thing that we can do right here.
We'll send back just the entire array of comments we could send back just the comment that was created.
But what the heck let's just send back the entire array of comments.
I'll do a rez that status to a one and I'll send back the entire array of comments get.
So there's our post request handler.
Now we'll put together our get request handler which is going to be a way easier.
So up here we'll do a rez that sent.
We're going to look inside of our comments by post I.D. object.
I'm going to look up inside there.
The idea that was provided inside the path so we'll do a wreck per Rams I.D. and then once again if
that results in undefined let's guard against that was put in and or empty array like so so that no
matter what we always send back an array to whoever is making this request OK.
So it looks good.
It's going to say this file.
The last thing I'm going to do is open up my package not just on file.
We're going to find a script inside of there as well for starting up our project with Node 1.
So here's my package that Chase on file and then replace the START script or create a start script.
I should say and we'll do a node on index dot J.S. Well that should be it for our common service.
So once again let's take a quick pause right here.
We'll come back the next video and just do a very quick little manual test with postman post ask me
post man not node 1 post man to make sure that this is all working as expected.