From fa65b62716dd3871a1c6bf627cd571f808fc857c Mon Sep 17 00:00:00 2001 From: Mathis Date: Tue, 8 Oct 2024 15:57:47 +0200 Subject: [PATCH] Add removeFileType method to MachinesService Introduce the removeFileType method to handle the removal of specific file types from machines, including error handling for invalid machine or file type IDs. Additional TODOs are placeholders for future methods related to adding and retrieving file types for machines. --- .../src/app/machines/machines.service.ts | 70 ++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/apps/backend/src/app/machines/machines.service.ts b/apps/backend/src/app/machines/machines.service.ts index e90700c..9ea6605 100644 --- a/apps/backend/src/app/machines/machines.service.ts +++ b/apps/backend/src/app/machines/machines.service.ts @@ -7,9 +7,10 @@ import { DbService } from "apps/backend/src/app/db/db.service"; import { FilesForMachinesTable, FilesTable, + FilesTypeForMachine, MachinesTable, } from "apps/backend/src/app/db/schema"; -import { eq, ilike } from "drizzle-orm"; +import { and, eq, ilike } from "drizzle-orm"; @Injectable() export class MachinesService { @@ -39,7 +40,14 @@ export class MachinesService { return machines; } - //TODO The method to create a machine + /** + * Creates a new machine entry in the database. + * + * @param {string} machineName - The name of the machine to create. + * @param {string} machineType - The type of the machine to create. + * @return {Promise} A promise that resolves to the newly created machine object. + * @throws {InternalServerErrorException} If the insertion of the new machine failed. + */ async create(machineName: string, machineType: string) { try { const newMachine = await this.database @@ -63,6 +71,64 @@ export class MachinesService { //TODO a method to delete a machine and delete the associated FilesTypeForMachine row + /** + * Removes a specified file type from a machine. + * + * @param {string} machineId - The ID of the machine from which the file type should be removed. + * @param {string} fileTypeId - The ID of the file type to be removed from the machine. + * @return {Promise} A promise that resolves to the details of the file type removed. + * @throws {NotFoundException} If the machine ID or file type ID is not found. + */ + async removeFileType(machineId: string, fileTypeId: string) { + //TODO Find machineId + const machine = await this.database + .use() + .select() + .from(MachinesTable) + .where(eq(MachinesTable.id, machineId)) + .prepare("findMachineById") + .execute(); + if (machine.length !== 1) + throw new NotFoundException("Machine id not found."); + //TODO Find fileTypeId + const fileType = await this.database + .use() + .select() + .from(FilesTypeForMachine) + .where( + and( + eq(FilesTypeForMachine.machineId, machineId), + eq(FilesTypeForMachine.fileTypeId, fileTypeId), + ), + ) + .prepare("findFileTypeForMachine") + .execute(); + if (fileType.length !== 1) + throw new NotFoundException("File type id not found for the machine."); + + const removeResult = await this.database + .use() + .delete(FilesTypeForMachine) + .where( + and( + eq(FilesTypeForMachine.machineId, machineId), + eq(FilesTypeForMachine.fileTypeId, fileTypeId), + ), + ) + .prepare("removeFileTypeForMachine") + .execute(); + return fileType; + } + + async addFileType(machineId: string, fileTypeId: string) { + //TODO Find machineId + //TODO Find fileTypeId + } + + async getFilesTypes(machineId: string) { + //TODO Find machineId and associated filesTypes + } + /** * Finds files associated with a specific machine. *