All files / blog/adapters/api/v1 blog.controller.ts

53.19% Statements 25/47
25% Branches 2/8
41.66% Functions 5/12
52.27% Lines 23/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                                                              1x       1x   3x 3x 3x 3x 3x 3x       1x 1x         1x       1x             1x             1x                   1x         1x 1x       1x                 1x         1x       1x 1x                 1x                            
import {
  Body,
  Controller,
  Delete,
  Get,
  HttpException,
  Inject,
  Param,
  Post,
  Put,
  Query,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
 
import { BlogService, IBlogService } from '../../../domain/blog.service';
import { BlogPrincipalRes, BlogRes } from './dto/blog.res';
import { CreateBlogReq, SearchBlogQuery } from './dto/blog.req';
import { BlogApiV1Mapper } from './blog.mapper';
import { redis, stores } from '../../../../constants';
import { ApiBody, ApiConsumes, ApiProperty } from '@nestjs/swagger';
import Redis from 'ioredis';
import { StorageService } from '../../../../infra/services/storage.service';
import { ConfigService } from '@nestjs/config';
import { File, FileInterceptor } from '@nest-lab/fastify-multer';
import { PinoLogger } from 'nestjs-pino';
import { context, Counter, trace } from '@opentelemetry/api';
import { OtelCounter, OtelMethodCounter } from 'nestjs-otel';
 
class FileUploadDto {
  @ApiProperty({ type: 'string', format: 'binary' })
  file: any;
}
 
@Controller({ version: '1', path: 'blogs' })
export class BlogV1Controller {
  constructor(
    @Inject(BlogService) private readonly service: IBlogService,
    @Inject(BlogApiV1Mapper) private readonly mapper: BlogApiV1Mapper,
    @Inject(PinoLogger) private readonly logger: PinoLogger,
    @Inject(redis.MAIN) private readonly cache: Redis,
    @Inject(stores.MAIN) private readonly storage: StorageService,
    @Inject(ConfigService) private readonly config: ConfigService,
  ) {}
 
  @Get()
  async search(@Query() query: SearchBlogQuery): Promise<BlogPrincipalRes[]> {
    const blogs = await this.service.search(
      query.search,
      query.limit,
      query.skip,
    );
    return blogs.map(x => this.mapper.toPrincipalRes(x));
  }
 
  @Get(':id')
  async get(@Param('id') id: string): Promise<BlogRes> {
    const blog = await this.service.get(id);
    if (blog != null) return this.mapper.toRes(blog);
    throw new HttpException('Blog not found', 404);
  }
 
  @Post()
  async create(@Body() req: CreateBlogReq): Promise<BlogPrincipalRes> {
    const domain = this.mapper.fromCreateReq(req);
    const blog = await this.service.create(domain);
    return this.mapper.toPrincipalRes(blog);
  }
 
  @Put(':id')
  async update(
    @Param('id') id: string,
    @Body() req: CreateBlogReq,
  ): Promise<BlogPrincipalRes> {
    const domain = this.mapper.fromUpdateReq(req);
    const blog = await this.service.update(id, domain);
    return this.mapper.toPrincipalRes(blog);
  }
 
  @Delete(':id')
  async delete(@Param('id') id: string): Promise<void> {
    await this.service.delete(id);
  }
 
  @Get('test/:id')
  async test(@Param('id') id: string): Promise<string> {
    return (await this.cache.get(id)) ?? 'not found';
  }
 
  @Post('test/:id/:val')
  async test2(
    @Param('id') id: string,
    @Param('val') val: string,
  ): Promise<string> {
    return (await this.cache.set(id, val)) ?? 'not found';
  }
 
  @Get('random')
  @OtelMethodCounter()
  async random(): Promise<string> {
    return 'new';
  }
 
  @Get('secret')
  secret(
    @OtelCounter('s_counter')
    counter: Counter,
  ): string {
    counter.add(5);
    return this.config.get<string>('secret') ?? 'not found';
  }
 
  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    type: FileUploadDto,
  })
  async uploadFile(@UploadedFile() file: File): Promise<string> {
    try {
      const span = trace.getSpan(context.active());
      console.log('span', span);
      this.logger.info(file.buffer?.toString());
      const r = await this.storage.putObject('test', file.buffer!);
      this.logger.info(r);
      return 'ok';
    } catch (e) {
      this.logger.error(e);
      return 'error';
    }
  }
}