Compare commits

...

4 Commits

Author SHA1 Message Date
84d6743863
Add getFilesTypes method to machines service
Implemented a new method to retrieve file types associated with a specific machine by its ID. This method queries the database and returns a promise that resolves to an array of file types.
2024-10-08 16:08:50 +02:00
86da1b7bce
Refactor file type insertion method in machine service
Replaced variable assignment with direct return of the database insertion result to streamline the code. This improves readability and eliminates an unnecessary variable.
2024-10-08 16:04:49 +02:00
a8a190198b
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.
2024-10-08 16:03:16 +02:00
fa65b62716
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.
2024-10-08 15:57:47 +02:00

View File

@ -7,9 +7,11 @@ import { DbService } from "apps/backend/src/app/db/db.service";
import {
FilesForMachinesTable,
FilesTable,
FilesTypeForMachine,
FilesTypesTable,
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 +41,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<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) {
try {
const newMachine = await this.database
@ -63,6 +72,110 @@ 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<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) {
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(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;
}
/**
* 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) {
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.");
return await this.database
.use()
.insert(FilesTypeForMachine)
.values({
machineId,
fileTypeId,
})
.prepare("insertFileTypeForMachine")
.execute();
}
/**
* Retrieves the file types associated with a specific machine based on the given machine ID.
*
* @param {string} machineId - The unique identifier of the machine whose file types are to be retrieved.
* @return {Promise<Array<object>>} A promise that resolves to an array of file types associated with the machine.
*/
async getFilesTypes(machineId: string) {
return this.database
.use()
.select()
.from(FilesTypeForMachine)
.where(eq(FilesTypeForMachine.machineId, machineId))
.prepare("getMachineFilesTypesById")
.execute();
}
/**
* Finds files associated with a specific machine.
*