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.
This commit is contained in:
Mathis H (Avnyr) 2024-10-08 15:57:47 +02:00
parent 578b529199
commit fa65b62716
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -7,9 +7,10 @@ import { DbService } from "apps/backend/src/app/db/db.service";
import { import {
FilesForMachinesTable, FilesForMachinesTable,
FilesTable, FilesTable,
FilesTypeForMachine,
MachinesTable, MachinesTable,
} from "apps/backend/src/app/db/schema"; } from "apps/backend/src/app/db/schema";
import { eq, ilike } from "drizzle-orm"; import { and, eq, ilike } from "drizzle-orm";
@Injectable() @Injectable()
export class MachinesService { export class MachinesService {
@ -39,7 +40,14 @@ export class MachinesService {
return machines; 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<Object>} 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) { async create(machineName: string, machineType: string) {
try { try {
const newMachine = await this.database 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 //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<Object[]>} 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. * Finds files associated with a specific machine.
* *