Hi everyone.
Let's talk about the DTO, or we can say "data transfer object".
So what is the data transfer object?
It's just a dumb object.
Okay, we can say it's just a "dumb object".
The only purpose of the data transfer object a reduced amount of data.
Okay, that's the first purpose will be reduce the data.
And in the MVC architecture, we usually write the validation logic inside the data transfer object.
Now first forget that.
Let me show you what is the reduced data right now.
Right now you can see, for example, I have a schema like this id username, email and password.
Now lets think about how we can create the data like this.
In order to create we don't need the the "id" right?
The "id" is useless in this case.
We don't need the ID when we.
Create the data, right?
For that reason, if we use the DTO, it will reduce the data.
It means we will remove the "id" when we create the "dump object".
Based on the original object.
Okay.
That the first one.
And the second one.
Now let's think about how the data can be response.
For example, when we create the data right here and the server will response for you the data, we don't
need the password, right?
We don't need
The password.
And it's also "reduce" the data, right? Here and here.
We will call that the "DTO" -> data transfer object.
And we need two data transfer object for create and for response.
Right.
And that's the idea.
Of DTO.
And when clients send this amount of data into the server we usually send the "DTO" over server okay.
And server it will return the "DTO" also.
And we will validation inside the DTO.
Also.
Let me see how we can do that right now.
If you scroll down inside the Mongo option right here, Let me see.
That you know it's not right here.
I will scroll up a little bit.
Here.
Right here.
You can see it.
DTO, right?
This is the "Cat DTO"
And let me show you how we can do that in here in the post.
I will create the "dto".
I think "dtos" is the best way.
Okay.
And I will create.
One "dto" will be "create-post"
post.dto.ts
Right.
And this is just the "dumb object" or "dumb class" based on the original class.
The original class will be the "post", right?
And now let me export class "CreatePostDTO"
And in here, what we have, we have a title will be string and the description is string.
Well, the reason, the reason it is the same property because we don't have so much property in the
post
So this is not important.
This case.
But later when we work with user we will understand more about that.
Okay that's the first one.
And because we already use that, we will use inside the controller and service. In the controller right here
The "requestBody", I will use the "CreatePostDTO" and the body right here, I will use
CreatePostDTO
CreatePostDTO okay, that the first look.
About
DTO it's just a data type right now
And the application will work if you see.
Okay.
But for example if I remove the title let's see what happened.
It will create this.
Right?
I don't want that I want it.
Show the error.
Show how I can do that.
And in the next video I will show you how we can validation.
I will see you in the next one.