Hi everyone.
Let's continue with CRUD operation for post controller.
And we already have a create post, right?
In this video I will do the get all methods.
So, getAll right here.
And what is the get all.
Firstly, I will remove un-used import statement right here because it's warning me.
Okay.
And inside.
Inside the post service
Let me create async method from getAll.
And what's it return? I want to return for me the promise with response DTO.
Array
Right.
Because this is a get all method.
So in order to do that. Let me show you.
Right here.
I will go to the.
I will getAll post using this.
postModel
.find()
Okay.
This "find()" method right here will get all of documents.
And if I console.log.
Or.
Actually, I think I'll return directly
And now you can see this return will be error because I not convert it inside the DTO.
Right?
We must convert the real entity into the DTO.
So.
Right now I will use the any
And I will show you how we can convert it later.
But for now, let's see, why we use that and how it works. Return this.postService
dot
getAll
Okay, go to the postman and I will name to "create post".
And I will duplicate and change method to get and post right here.
I will change to get all posts
Value.
Okay, let me send, now you can see, if I get all post, we have a __v right here, right?
I don't want it
show that
And for that reason I will use the DTO
Now let's see how we can map it inside the post.
DTO, in post right here. I will change the promise.
ResponseDTO
Array
Okay.
Inside the post I will map it and I will take the "post".
And right now I will return.
I will return the post dto.
Right?
I will return.
The _id.
will be
post._id, title
post
dot
And description.
post
dot
Description.
Okay, that's what we want.
And it's also.
An error, right?
The reason because say the "_id" right here is not assignable to type post
DTO because the post._id right here is not a string.
If I convert it to string here, it will be work.
Because this is same shape with the responseDTO here
You can see, _id, title and description.
So I can return this in this way okay.
And in order to add it more explicitly, I will use as ResponsePostDTO
To make it better, okay.
You can see, right here.
And that's it. If I send again, you can see it only have _id, title and description.
Right.
But you can see, right now it's very tricky for me to create some other method because we always convert
it inside the DTO when we respond.
Right.
And in the few next video I will show you how to refactor this.
So I will see you in the next one.