#How should I handle DTO

7 messages · Page 1 of 1 (latest)

limber sun
#

I have this DTO:

export class SituationDto {
  @IsNumber()
  @IsNotEmpty()
  public borrower_id: number;

  @IsString()
  @IsNotEmpty()
  public situation_name: string;

  @IsString()
  @IsNotEmpty()
  public created_by: string;
}

I have this route:

  @Post()
  async postSituation(
    @Body() body: SituationDto,
    @Request() req: CustomRequestType
  ): Promise<number> {
    return this.situationService.createSituation({
      ...body,
      created_by: req.username
    });
  }

The problem is, I am getting username from req.username and then I am merging it down in a object which I send to my createSituation function in a service:

  async createSituation(@Body() situationDto: SituationDto): Promise<number> {
    const situation = this.situationRepository.create(situationDto);
    const { situation_id } = await this.situationRepository.save(situation);
    return situation_id;
  }

So the problem is, in controller I am not going to get createdBy property in situationDto but in service I will, how can I handle this?

quasi panther
#

An input and an output DTO?

limber sun
#

Hmm, but this DTO is input in both cases, isnt it?

#

Because I am getting it in a controller as a body and then I am extending it when I call service
```js
return this.situationService.createSituation({
...body,
created_by: req.username
});

#

Maybe I dont understand what is input and output DTO

#

How should I handle DTO

blazing stump
#

I'd have a DTO for the controller, what it expects to be sent via the request, and an interface for the createSituation method's parameters. By the way, that @Body() in the service doesn't do anything