Compare commits

..

No commits in common. "84d67438638729cca75d4e0e320b81283badb8c7" and "578b52919914f0fd2ba07075c7fc62888f1292ed" have entirely different histories.

View File

@ -7,11 +7,9 @@ import { DbService } from "apps/backend/src/app/db/db.service";
import {
FilesForMachinesTable,
FilesTable,
FilesTypeForMachine,
FilesTypesTable,
MachinesTable,
} from "apps/backend/src/app/db/schema";
import { and, eq, ilike } from "drizzle-orm";
import { eq, ilike } from "drizzle-orm";
@Injectable()
export class MachinesService {
@ -41,14 +39,7 @@ export class MachinesService {
return machines;
}
/**
* 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.
*/
//TODO The method to create a machine
async create(machineName: string, machineType: string) {
try {
const newMachine = await this.database
@ -72,110 +63,6 @@ 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.
*