diff --git a/apps/backend/src/app/machines/machines.service.ts b/apps/backend/src/app/machines/machines.service.ts index 9ea6605..37a9e64 100644 --- a/apps/backend/src/app/machines/machines.service.ts +++ b/apps/backend/src/app/machines/machines.service.ts @@ -8,6 +8,7 @@ import { FilesForMachinesTable, FilesTable, FilesTypeForMachine, + FilesTypesTable, MachinesTable, } from "apps/backend/src/app/db/schema"; import { and, eq, ilike } from "drizzle-orm"; @@ -80,7 +81,6 @@ export class MachinesService { * @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() @@ -90,7 +90,7 @@ export class MachinesService { .execute(); if (machine.length !== 1) throw new NotFoundException("Machine id not found."); - //TODO Find fileTypeId + const fileType = await this.database .use() .select() @@ -120,9 +120,45 @@ export class MachinesService { return fileType; } + /** + * Adds a file type to a machine by their respective IDs. + * + * @param {string} machineId - The ID of the machine to which the file type will be added. + * @param {string} fileTypeId - The ID of the file type to be added to the machine. + * @return {Promise} The result of the database insert operation. + * @throws {NotFoundException} If the machine ID or file type ID are not found in the database. + */ async addFileType(machineId: string, fileTypeId: string) { - //TODO Find machineId - //TODO Find fileTypeId + 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."); + + const fileType = await this.database + .use() + .select() + .from(FilesTypesTable) + .where(eq(FilesTypesTable.id, fileTypeId)) + .prepare("findFileTypeById") + .execute(); + if (fileType.length !== 1) + throw new NotFoundException("File type not found."); + + const insertFileType = await this.database + .use() + .insert(FilesTypeForMachine) + .values({ + machineId, + fileTypeId, + }) + .prepare("insertFileTypeForMachine") + .execute(); + return insertFileType; } async getFilesTypes(machineId: string) {