Fix data type in pagination schema

Updated the `data` property in the pagination schema from a single object to an array of objects to accurately represent paginated data. This change ensures that the schema aligns with the actual data structure used in the application.
This commit is contained in:
Mathis H (Avnyr) 2024-10-17 12:21:21 +02:00
parent 989ec71e2e
commit 7b4792b612
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -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<object>} 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<IWithCount<IFileTable>> {
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);
}