Hi everyone!
In previous video I already showed you how to create transformDTO for response, right?
for DTO Response and make sure we use the exclude.
And inside the response we use the @Expose
Okay all of them
This will work.
And now let's go to the next application.
Let me create next endpoint will be getOne
I already have a create and getAll.
Right?
So in this video I will create a get one method.
And I will do the get method with the :id, okay
And with the get one.
And I will use the decoration.
Decorator will be the @Param and pass the id
Make sure the "id" right here, and "id" right here will be the same
And it will be string because this is the id of the MongoDB, right?
But if this is the number, make sure we use
The Parse
ParseIntPipe
Okay, I already explained in the first section, right?
So this is a string.
And now let me see.
What is the id here?
It will concatenation from the post.
And with the :id from here okay.
This is endpoint.
Right? I hope you
Very clear.
And let me go to the service and I will create an async getOne
And create post = await
Now what is the method?
Right now we don't.
We don't know.
If you don't know how to find the methods inside the post model.
You can look at the documentation.
This is Mongo right here.
It doesn't have anything related to the method in this documentation, because this method is not related
to the NestJS.
It actually is the mongoose okay.
It's a different library, right?
For that reason, if you go and click on Read the Docs and inside the API right here, you can click
on the query and you will have many query right here.
For example like deleteMany, deleteOne, right?
Or findOne, find, findOne, right?
And that's how to look at.
And the findOne() right here is what we look.
We will pass it some filter okay.
Now I will show you how to do that.
findOne() and I can use the _id.
Right.
This is the method from.
This is the property okay.
This is the default _id from the MongoDB document.
And I will pass the id from parameter will be string.
And I will pass directly id right here.
And I will return the post okay that is and let's see how it works.
In order to make sure it work I will drop the post and I will recreate again over.
Okay, I will drop it and I will create a post for example like post one.
And post 2.
Okay I will create 2 posts
And in here inside here, I duplicate one more.
Endpoint using the get one post and I will specify the id okay.
The ID is what it is right now.
I will refresh and I will look into the documentation.
Go to the posts collection and look at the documentation right here I will copy right here okay.
Like this.
And I send a request.
This is not work.
Why is not work?
I will console.log the id I will go here.
id I will check.
I will check the ID.
Here.
And open.
So again.
Oh, sorry for us, I forgot I'll return this.postService
.get
getOne pass the id
Okay, make sure we call the service.
And I send again, now
You can see, it's already work, right?
And it have a id like this
Very cool
Right.
And for example, if I pass the different ID let's see this return something like this.
That's not what we want?
Okay.
I want it has a not found error
I can easily check inside the service
I can easily check it.
!post
I will throw
new NotFoundException, actually NotFoundException, okay
NOT
NotFoundError
You can see, NotFoundException inside a nestjs/common
And I will pass
Post
With id not found, for example like that, okay. This Exception right here.
Is the buil-in exception
NestJS already write it for you.
Now if I send it have status and message not found.
Right.
Very cool.
So that's for the video.
I will see you in the next one.