All files / user user.controller.ts

100% Statements 67/67
83.33% Branches 15/18
100% Functions 6/6
100% Lines 67/67

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 681x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 3x 1x 3x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 2x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { Controller, Get, Post, Body, Patch, Param, Delete, UseInterceptors, ClassSerializerInterceptor, HttpException, HttpStatus, UseGuards } from "@nestjs/common";
import { UserService, UserException } from "./user.service";
import { CreateUserDto } from "./dto/create-user.dto";
import { UpdateUserDto } from "./dto/update-user.dto";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { ExpertiseException } from "src/expertise/expertise.service";
import { instanceToPlain } from "class-transformer";
import { AuthGuard } from "src/auth/auth.guard";
 
@ApiTags("user")
@ApiBearerAuth()
@Controller("user")
export class UserController {
    constructor(private readonly userService: UserService) { }
 
    @Post()
    async create(@Body() createUserDto: CreateUserDto) {
        try {
            return await this.userService.create(createUserDto);
        } catch (e) {
            if (e instanceof UserException) {
                throw new HttpException(e.name, HttpStatus.CONFLICT);
            } else if (e instanceof ExpertiseException) {
                throw new HttpException(e.name, HttpStatus.NOT_FOUND);
            }
 
            throw e;
        }
    }
 
    @UseInterceptors(ClassSerializerInterceptor)
    @Get()
    async findAll() {
        return {
            users: instanceToPlain(await this.userService.findAll()),
        };
    }
 
    @UseInterceptors(ClassSerializerInterceptor)
    @Get(":username")
    async findOne(@Param("username") username: string) {
        try {
            return instanceToPlain(await this.userService.findOneByUsername(username));
        } catch (e) {
            if (e instanceof UserException && e.name === "USER DOESNT EXIST") {
                throw new HttpException(e.name, HttpStatus.NOT_FOUND);
            }
 
            throw e;
        }
    }
 
    @Patch(":username")
    @UseGuards(AuthGuard)
    async update(
        @Param("username") username: string,
        @Body() updateUserDto: UpdateUserDto,
    ) {
        await this.userService.update(username, updateUserDto);
    }
 
    @Delete(":username")
    @UseGuards(AuthGuard)
    async remove(@Param("username") username: string) {
        await this.userService.remove(username);
    }
}