From 030b5c814c6b3795d8fe792db3fc62322acbda9b Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 10 Oct 2024 15:26:06 +0200 Subject: [PATCH] Add type management endpoints for machines This commit introduces endpoints to add, remove, and list types associated with machines. It also includes the addition of the `TypeDto` class to handle type validations and updates HTTP response codes for the existing endpoints. --- .../src/app/machines/machines.controller.ts | 37 ++++++++++++++++--- apps/backend/src/app/machines/machines.dto.ts | 7 +++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/apps/backend/src/app/machines/machines.controller.ts b/apps/backend/src/app/machines/machines.controller.ts index a2d158d..0548c8b 100644 --- a/apps/backend/src/app/machines/machines.controller.ts +++ b/apps/backend/src/app/machines/machines.controller.ts @@ -14,13 +14,17 @@ import { UseGuards, } from "@nestjs/common"; import { AdminGuard } from "apps/backend/src/app/auth/auth.guard"; -import { CreateMachineDto } from "apps/backend/src/app/machines/machines.dto"; +import { + CreateMachineDto, + TypeDto, +} from "apps/backend/src/app/machines/machines.dto"; import { MachinesService } from "apps/backend/src/app/machines/machines.service"; @Controller("machines") export class MachinesController { constructor(private readonly machineService: MachinesService) {} + @HttpCode(HttpStatus.OK) @Get("find") async findMany( @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, @@ -36,15 +40,38 @@ export class MachinesController { return await this.machineService.create(body.machineName, body.machineType); } + @HttpCode(HttpStatus.ACCEPTED) @UseGuards(AdminGuard) @Delete(":machineId") async deleteMachine(@Param("machineId") machineId: string) {} - //TODO Type add for machine - //TODO Type remove for machine - //TODO Type list for machine + @HttpCode(HttpStatus.ACCEPTED) + @UseGuards(AdminGuard) + @Post("types/:machineId") + async addTypeToMachine( + @Param("machineId") machineId: string, + @Body() body: TypeDto, + ) { + return await this.machineService.addFileType(machineId, body.fileTypeId); + } - @HttpCode(HttpStatus.OK) + @HttpCode(HttpStatus.ACCEPTED) + @UseGuards(AdminGuard) + @Delete("types/:machineId") + async remTypeToMachine( + @Param("machineId") machineId: string, + @Body() body: TypeDto, + ) { + return await this.machineService.removeFileType(machineId, body.fileTypeId); + } + + @HttpCode(HttpStatus.FOUND) + @Get("types/:machineId") + async getTypesOfMachine(@Param("machineId") machineId: string) { + return await this.machineService.getFilesTypes(machineId); + } + + @HttpCode(HttpStatus.FOUND) @Get("files/:machineId") async getFilesForMachine( @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, diff --git a/apps/backend/src/app/machines/machines.dto.ts b/apps/backend/src/app/machines/machines.dto.ts index a93e519..bccdcd5 100644 --- a/apps/backend/src/app/machines/machines.dto.ts +++ b/apps/backend/src/app/machines/machines.dto.ts @@ -1,4 +1,4 @@ -import { MaxLength, MinLength } from "class-validator"; +import { IsUUID, MaxLength, MinLength } from "class-validator"; export class CreateMachineDto { @MaxLength(128) @@ -9,3 +9,8 @@ export class CreateMachineDto { @MinLength(2) machineType: string; } + +export class TypeDto { + @IsUUID() + fileTypeId: string; +}