Add method to retrieve files for a specific machine

This commit introduces the `findFilesForMachine` method, which retrieves files associated with a given machine ID from the database. It includes functionality to limit and offset the results based on given parameters, and throws a `NotFoundException` if the machine ID is not found.
This commit is contained in:
Mathis H (Avnyr) 2024-10-08 13:57:06 +02:00
parent 289ec09868
commit 3844153340
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -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<Array>} 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();
}
}