So we finished these three endpoints.
Now we need to move on to this last two and we're actually going to accomplish these two in this video
because they're very, very easy and similar to the ones that we've done before.
So the first one is writing a message to a listing and the other one is viewing all of the messages
on a particular listing.
So what we're going to need to know is we're going to need to know exactly what listing we want to write
a message on.
So I think an appropriate endpoint for that would be something like slash API slash car slash listing,
then that particular listing ID and then slash message and it's a post request because we are creating
data now to view all of the listings.
I think that this URL actually works as well because we need to know exactly what listing it is and
we want to get the messages, but what we can do is just change the HTTP verb to a get request.
So let's go ahead and accomplish this together.
So we're going to go right over here and we're going to go to the listings directory.
Let's remember the path here listing slash listing, ID, slash message.
So I'm going to go and I'm going to create a new file and this is going to be slash listing ID and then
I'm going to say Slash and we're going to say message dot post, dot JS just like that.
All right.
So that looks pretty good to me.
And let's go ahead and actually create the get request, which is going to be exactly the same, but
it's going to be dot get dot us.
All right.
So we have those two now.
And now what we can do is we can go ahead and just define this event.
HANDLER In fact, let's just go ahead and copy it from here.
So I'm going to define this event.
HANDLER And this one is the post request.
So I'm just going to return post for now just to see that everything is working fine, Save that.
Let's do the exact same thing right here and let's return.
Get okay, so now let's go to Postman and I'm going to go ahead and make a request to this slash API
slash car slash listing slash three, which is the listing ID and then over here slash message.
So I'm going to go ahead, make a request.
And you can see we have post and if I were to change this to a get request, we should get get.
Okay then that's awesome.
And if I were to change it to anything else, like let's say deletes, we should get a four and five.
And indeed we do.
Okay, so everything is fine.
What we're going to do now is we're going to begin with the post request.
Now, similarly to the post request that we did with creating a listing, we do have to still validate
our data.
So we're going to utilize JOI for this.
So I'm going to go over here very similarly.
We're going to import Joi from Joi and let us go over here and let's define the schema.
So I'm going to say con schema is equal to Joi dot object and then we're going to go ahead and just
say, well, you have to give us an email.
So we're going to say Joi and we're going to say that this has to be a string and actually we can check
if this is an email.
An email.
So we're going to say email right over here.
You can also provide us some options like the Min Domain segment.
So the minimum domain segment, that's going to be two.
So you can give us, you know, just some gibberish domain.
And also we can say that we only allow for some reason, we can just say we allow dot com or dot net,
for example.
So we could do this as well.
And maybe we don't allow.
Okay, Now, probably not the best idea, but I just want to show you the options that we have with
Joi anyways, so that is the email.
Now we can also add a phone number so you can say, Hey, you also need a phone number so we can say
Joi Dot and we can also make this required by the way, required actually, you know what?
We'll leave it as that.
So we'll just say dot string and over here we can say the length of this has to be ten.
I know some phone numbers are a little bit different, but you know what?
We'll we'll worry about those validations a little bit later.
And unfortunately, there is no phone with joy.
But what we can do is we can say pattern, we can give it a regex expression so we can say something
like let's say something like this and give it like a a reg X, we can say it's from 0 to 9 is what
you can give us.
And then over here we can just say plus two, where's the plus sign and then dollar sign like so.
And that's just going to ensure, that's going to ensure that we can only give it a number and the length
is going to be ten.
Okay, So that's good.
And we can also make this required and say required like so.
So now you give us the email, you gave us the phone number.
There's a few more things that you got to give us, like your name we're going to say for this one very
simple joy dot string.
And similarly, we're just going to say a required for this.
And lastly, we're going to say message, because that's the whole point you're messaging us.
You can say joy dot string.
We can also say dot min 20 characters and required.
All right.
So now that is how we are going to validate the schema.
Now, of course, we're going to get the data from we're going to get the data from the body.
So we have to read the body.
So let's go ahead and read that body.
So we're going to say body is equal to a weight and we're going to say read body.
And we also need to grab that listing ID because we need to know exactly, well, where do we want to
link this message to?
Which listing is it?
And we're going to get that from the params we're going to say listing ID and that's going to be equal
to event dot and we're going to say dot context, dot params.
All right.
So we got all the information that we need.
So now let's just validate that everything is fine.
So I'm going to say const error and then value.
So grab that as well, even though we don't really need it.
Actually, you know what?
We could just ignore it because we don't really use it.
And I can say that that is going to be equal to a weight to the schema dot validate.
We want to validate that body.
And if we have an error, what we can do is we can throw, create error.
And you can throw a status code of 412 and also a status message of error, dot message.
All right.
Very similar to what we've done before.
So now that validates that everything is nice, our body is amazing.
And now what we can do is just well, very simply, just say return and we can grab Prisma here.
So we can.
You know what?
Let's just grab it from here.
And what we can say now is return and I can say Prisma dot message this time because we want to create
a message and we want to create it.
And over here we'll provide the data.
And what we'll do is we'll say message is going to be and we could restructure the body, but we'll
just leave it as is.
So we'll say email, we'll say body, dot, email or and you know what?
Let us do structure the body.
This is kind of annoying me.
Let's just say email is equal to a body.
And what else do we have?
We have the phone number.
And I think that should be fine.
So you can say email like so.
Message like so.
And we also should have the name.
We want to do structure so the name and the phone number.
The last thing that we need to do is, of course, link this message to the listing.
And of course the way that we do that is with this foreign key.
Now, this foreign key is going to be the listing ID, so we're going to say listing ID and we have
to remember that the listing ID that we get from the params is going to be a string, so we have to
parse into it to a number.
So that's pretty much it.
And that should create a listing for us.
Let's go ahead and just give this a quick shot.
So over here we have how many?
We have these two listings and let's say I am very interested in this Honda Civic.
So what I'm going to do now is I'm going to go right about here.
I'm going to say this one and I'm going to give it the body.
We're going to give it an actual real body here.
So let's go ahead and add and we're going to give it a JSON.
So we're going to say the message is, Hello, I really like this car and I want to buy it right now.
Right now.
And then we're going to say the phone number.
Is going to be 111 2 to 2, two, three, three, three.
We're going to say the email is lathe at Honda Civic dot com because, you know, maybe I'm a Honda
Civic owner and then with the name we'll say is Laith Harb.
I think that's all the information that we need the listings right there.
So if I were to actually create this.
We get a Oh, yeah, we need to change that to a post request.
So let's go ahead and send this request off.
You can see now we actually created that record.
And if I were to go to messages now you can see now we have this record and it's linked.
It's linked to this one.
So we can actually go to it and it's linked to this Honda Civic.
So that's that's pretty cool.
Okay.
So now that we have created this, now what we can do is we can actually start fetching all of the different
messages based on a listing.
And this one actually is really, really easy.
We don't have to do any sort of validation.
We don't have to do anything.
All we really have to do is just grab Prisma and return all the messages.
So we're going to go here.
We're going to go here and we're just going to say, well, return Prisma dot and then we're going to
say messages and we're going to say Dot.
We're going to say, find many over here.
We're just going to add a where clause.
We're going to say where the listing ID is equal to the listing ID that is inside of our params.
So we can just grab this piece of code here.
And we can say parse into this, of course.
We can say listing ID that's in our params and that's it.
That's actually all we have to do.
And so let's make this exact same request now, but make it a get request and we should get an array
of this.
So if I were to make this request, you can see now we get an array.
And if I were to change this to two, for example, we should get an empty array because there's no
listings there.
So that's really all it is that we need to do.
And we're pretty much done our endpoints.
The only thing that we need to also do is kind of refactor our previous endpoint.
So let's go ahead and actually do that.