diff --git a/apps/backend/src/app/files/files.service.ts b/apps/backend/src/app/files/files.service.ts index 508d440..8662e9c 100644 --- a/apps/backend/src/app/files/files.service.ts +++ b/apps/backend/src/app/files/files.service.ts @@ -11,11 +11,11 @@ import { FilesGroupTable, FilesTable, FilesTypeForMachine, - FilesTypesTable, - MachinesTable, -} from "apps/backend/src/app/db/schema"; + FilesTypesTable, IFileTable, IWithCount, + MachinesTable +} from 'apps/backend/src/app/db/schema'; import { StorageService } from "apps/backend/src/app/storage/storage.service"; -import { eq, ilike } from "drizzle-orm"; +import { count, eq, ilike } from 'drizzle-orm'; @Injectable() export class FilesService { @@ -123,18 +123,14 @@ export class FilesService { } } - /** - * Searches for files in the database using the specified search field, limit, and offset. - * - * @param {number} limit - The maximum number of results to return. - * @param {number} offset - The number of results to skip before starting to return results. - * @param {string} searchField - The field used to search for matching file names. - * - * @return {Promise} A promise that resolves to the search results. - */ - public async search(limit: number, offset: number, searchField: string) { + public async search(limit: number, offset: number, searchField: string): Promise> { try { - return await this.database + const countResult = await this.database.use() + .select({count : count()}) + .from(FilesTable).where(ilike(FilesTable.fileName, String(`%${searchField}%`))) + .prepare("searchFilesCount") + .execute() + const dataResult = await this.database .use() .select() .from(FilesTable) @@ -143,6 +139,12 @@ export class FilesService { .offset(offset) .prepare("searchFiles") .execute(); + return { + count: countResult[0].count, + limit: limit, + currentOffset: offset, + data: dataResult + } } catch (error) { throw new InternalServerErrorException(error); }