Our query service has a little bit scaffolding put together.
So now we need to make sure that it can receive events of type post created income and created.
Whenever we get post created we're gonna save the post whenever we get common created.
We're gonna save the comment with the associated post.
The first thing you do back over here is decide on a data structure to use to save all these posts and
comments.
We're going to once again use a simple object to store all the posts and comments.
Let me very quickly give you an example of what this object is going to look like eventually so you
don't have to write this code out.
This is a quick example of what post is going to look like.
The Post will be equal eventually to having an object full of some keys and for every one of these keys
we're gonna have the idea of the post that would be identical to the idea right there.
It will have the title The Post and it will have an array of comments associated with this.
And of course every comment will have its own ideas and some content like so to over time as users add
in more post to our application.
We would have say a second post and then a third post and so on.
Again that's what this post object is going to look like.
We just need to make sure that as we receive all these different events we insert data into this post
object with the appropriate structure.
So inside of our post to events right here we're gonna take a look at that req objects.
Remember the wrecked body property is going to be the event itself that we actually care about.
So let's take a look at that event.
Remember that every event that we create as a type and a data property will pull that out of record
body.
We will then set up a very simple series of if statements to take a look at that type.
So let's say if Type is equal to post created then let's go ahead and run some code or if type is equal
to comment created we'll do something entirely different so let's handle post created first if it's
equal the post created that we want to take.
The I.D.
and the title out of data because remember with post created that event is always going to have an I.D.
and a title and we're going to insert that information into our post object we'll say post at I.D. is
going to be equal to an object of IP and title and that should be it will probably also want to default
the value of comments to be an empty array is as well so that later on whenever someone you mentioned
event of common created and we receive that we don't have to worry about creating that array or anything
like that Dallas handle a event of type common created remember an event of type common created is going
to have slightly different structure to it that's going to have an I.D. content and a post I.D. So let's
pull that information out say I.D. content and post I.D.
then we will find the appropriate posts inside of our post object Z a post is posted at post i.e.
well then take a look at post dot comments and we'll push in this new comment so our new comment is
going to have a given I.D. and some content and that's to be it.
Now of course at the very bottom here after we have processed the events this still is a root handler.
So we need to make sure that we send back some kind of response.
We'll do a resort sense of just an empty object to say hey we received the event and processed it then
finally let's go back up to our slash post and point
and anytime someone tries to get the collection of all the posts we have we'll just send back the entire
post object.
They'll do a red dot sent of posts and that should be at let's say this.
I'm going to go back over to my terminal you'll notice I've changed the layout here just make sure everything
is all a bit more legible.
So the order now is the react app those comments very empty.
And the event boss right here so we're going to try to create a new post and a comment.
And we're right now just going to make sure that we don't see any errors coming out of this window right
here because that's where our query service is running.
We're not actually using the query service inside the reactor application just yet again.
I just want to make sure we don't have any errors.
So going to flip back over to the react application going to enter in a title your post I'll refresh
and I'll add in some comment to that post and refresh everything over here still works.
So we expected because we haven't made any change to really any existing code just yet and if I go back
to my terminal window it looks like the query service is running successfully as well cause we can't
really see any data we can't really verify that it's receiving these events or processing them correctly.
We'll tell you what about after we process an event inside of our query service that we run these two
if statements let's just print out the current data structure or the current post collection.
We have it's all save this I'm going to go back over again I'll enter in a second post and refresh and
then a second comment and submit and now if I go back over my terminal I should see the appropriate
data being reflected inside of my query service.
Take a look at that we've got two separate console logs the first one was from receiving the event of
post created and it looks like we received that post and add it to our post collection.
And the second console log right here is from creating the comment.
So there's the exact same post there is our list of comments and you can see that there is a object
inside that array is being abbreviated right now just because node when it prints out a nested data
structure or nested object doesn't want to recursively print out everything inside there at a and same
depth.
So chances are the correct comment is inside there.
We just can't really see it effectively using this tool just yet.
All right.
Once this looks pretty good.
So in theory our query service now has all the data that is required to get the entire list of posts
and all the associated comments.
Now the last thing to do is update our ReACT application and make sure that the react application will
make a request to this end point to get this data structure right here are set and then use that structure
to list out all the posts and comments let's take a quick pause right here and start to work on the
react app in just a moment.