Revise file search logic for machine service

Updated the file search functionality to include a search field that filters files by name and added pagination support. This enhances the API by providing more flexible querying and making it easier to handle large datasets.
This commit is contained in:
Mathis H (Avnyr) 2024-10-08 16:11:34 +02:00
parent 84d6743863
commit 7876bc2c38
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -177,14 +177,15 @@ export class MachinesService {
} }
/** /**
* Finds files associated with a specific machine. * Finds files associated with a specific machine based on a search field,
* and returns a limited set of results with an offset for pagination.
* *
* @param {number} limit - The maximum number of files to return. * @param {number} limit - The maximum number of files to return.
* @param {number} offset - The number of files to skip before starting to return results. * @param {number} offset - The offset for pagination.
* @param {string} searchField - The field to search within for files. * @param {string} searchField - The search query to filter files by name.
* @param {string} machineId - The ID of the machine to find files for. * @param {string} machineId - The unique identifier of the machine to find files for.
* @returns {Promise<Array<Object>>} A promise that resolves to an array of files associated with the specified machine. * @returns {Promise<Array>} A promise that resolves to an array of files associated with the specified machine.
* @throws {NotFoundException} If the machine ID is not found. * @throws {NotFoundException} If the specified machine id is not found.
*/ */
async findFilesForMachine( async findFilesForMachine(
limit: number, limit: number,
@ -212,7 +213,13 @@ export class MachinesService {
.where(eq(FilesForMachinesTable.machineId, machineId)) .where(eq(FilesForMachinesTable.machineId, machineId))
.limit(limit) .limit(limit)
.offset(offset) .offset(offset)
.leftJoin(FilesTable, eq(FilesTable.uuid, FilesForMachinesTable.fileId)) .leftJoin(
FilesTable,
and(
eq(FilesTable.uuid, FilesForMachinesTable.fileId),
ilike(FilesTable.fileName, String(`%${searchField}%`)),
),
)
.prepare("findFilesForMachineId") .prepare("findFilesForMachineId")
.execute(); .execute();
} }