Add addFileType method to machines service

Implemented the `addFileType` method to link a file type to a machine. This includes database checks for valid machine and file type IDs, and the actual insertion logic. Updated imports and removed obsolete TODO comments.
This commit is contained in:
Mathis H (Avnyr) 2024-10-08 16:03:16 +02:00
parent fa65b62716
commit a8a190198b
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -8,6 +8,7 @@ import {
FilesForMachinesTable, FilesForMachinesTable,
FilesTable, FilesTable,
FilesTypeForMachine, FilesTypeForMachine,
FilesTypesTable,
MachinesTable, MachinesTable,
} from "apps/backend/src/app/db/schema"; } from "apps/backend/src/app/db/schema";
import { and, eq, ilike } from "drizzle-orm"; 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. * @throws {NotFoundException} If the machine ID or file type ID is not found.
*/ */
async removeFileType(machineId: string, fileTypeId: string) { async removeFileType(machineId: string, fileTypeId: string) {
//TODO Find machineId
const machine = await this.database const machine = await this.database
.use() .use()
.select() .select()
@ -90,7 +90,7 @@ export class MachinesService {
.execute(); .execute();
if (machine.length !== 1) if (machine.length !== 1)
throw new NotFoundException("Machine id not found."); throw new NotFoundException("Machine id not found.");
//TODO Find fileTypeId
const fileType = await this.database const fileType = await this.database
.use() .use()
.select() .select()
@ -120,9 +120,45 @@ export class MachinesService {
return fileType; 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<object>} 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) { async addFileType(machineId: string, fileTypeId: string) {
//TODO Find machineId const machine = await this.database
//TODO Find fileTypeId .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) { async getFilesTypes(machineId: string) {