diff --git a/apps/backend/src/app/machines/machines.service.ts b/apps/backend/src/app/machines/machines.service.ts index fd7fead..fc29661 100644 --- a/apps/backend/src/app/machines/machines.service.ts +++ b/apps/backend/src/app/machines/machines.service.ts @@ -1,7 +1,10 @@ -import { Injectable } from "@nestjs/common"; +import { Injectable, NotFoundException } from "@nestjs/common"; import { DbService } from "apps/backend/src/app/db/db.service"; -import { MachinesTable } from "apps/backend/src/app/db/schema"; -import { ilike } from "drizzle-orm"; +import { + FilesForMachinesTable, + MachinesTable, +} from "apps/backend/src/app/db/schema"; +import { eq, ilike } from "drizzle-orm"; @Injectable() export class MachinesService { @@ -35,5 +38,40 @@ export class MachinesService { //TODO a method to delete a machine and delete the associated FilesTypeForMachine row - //TODO a method to get the files of a group in the database by a specific search with limit, offset and a search field (can be blank) + /** + * Retrieves a list of files associated with a specific machine from the database. + * + * @param {number} limit - The maximum number of files to retrieve. + * @param {number} offset - The offset from which to start retrieving files. + * @param {string} searchField - The specific field to search within the files. + * @param {string} machineId - The unique identifier of the machine. + * @return {Promise} A promise that resolves to an array of file records for the machine. + * @throws {NotFoundException} If the machine with the given id is not found. + */ + async findFilesForMachine( + limit: number, + offset: number, + searchField: string, + machineId: string, + ) { + const existingMachine = await this.database + .use() + .select() + .from(MachinesTable) + .where(eq(MachinesTable.id, machineId)) + .prepare("findMachineById") + .execute(); + if (existingMachine.length !== 1) + throw new NotFoundException("Machine id not found."); + + return await this.database + .use() + .select() + .from(FilesForMachinesTable) + .where(eq(FilesForMachinesTable.machineId, machineId)) + .limit(limit) + .offset(offset) + .prepare("findFilesForMachineId") + .execute(); + } }