File size: 1,299 Bytes
96ed566
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { z } from 'zod';

import { Query, Get, Controller, BadRequestException } from '@nestjs/common';
import { ImagePresenter } from '@/infra/http/presenters/image.presenter';
import { ZodValidationPipe } from '@/infra/http/pipes/zod-validation.pipe';
import { GetRandomImageUseCase } from '@/domain/alcremie/application/use-cases/cases/get-random-image/get-random-image';

const getRandomImageQuery = z.object({
  q: z.string().default(''),
});

type GetRandomImageQuery = z.infer<typeof getRandomImageQuery>;

const queryValidationPipe = new ZodValidationPipe(getRandomImageQuery);

@Controller('random-image')
export class GetRandomImageController {
  constructor(private readonly getRandomImageUseCase: GetRandomImageUseCase) {}

  @Get()
  async getRandomImage(@Query(queryValidationPipe) query: GetRandomImageQuery) {
    if (query.q === '') {
      const result = await this.getRandomImageUseCase.execute({});

      if (result.isLeft()) {
        throw new BadRequestException();
      }

      return ImagePresenter.toHTTPWithoutTags(result.value.image);
    }

    const result = await this.getRandomImageUseCase.execute({ tagId: query.q });

    if (result.isLeft()) {
      throw new BadRequestException();
    }

    return ImagePresenter.toHTTPWithoutTags(result.value.image);
  }
}