From ec8af843b562d80e62d7a6d3e5fe76f509da8e52 Mon Sep 17 00:00:00 2001 From: Mathis Date: Tue, 8 Oct 2024 12:19:51 +0200 Subject: [PATCH] Enhance file retrieval and search functionalities Refined JSDoc comments for clearer API documentation. Added error handling in the search method and implemented search endpoint in the controller. --- .../backend/src/app/files/files.controller.ts | 8 +++- apps/backend/src/app/files/files.service.ts | 39 ++++++++++++------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/apps/backend/src/app/files/files.controller.ts b/apps/backend/src/app/files/files.controller.ts index 8c08111..8dc62a2 100644 --- a/apps/backend/src/app/files/files.controller.ts +++ b/apps/backend/src/app/files/files.controller.ts @@ -100,15 +100,19 @@ export class FilesController { return; } + @HttpCode(HttpStatus.FOUND) @Get("find") async findMany( @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, @Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number, @Query("search", new DefaultValuePipe("")) search: string, - ) {} + ) { + return this.filesService.search(limit, offset, search) + } + @HttpCode(HttpStatus.FOUND) @Get(":fileId") async getFile(@Param("fileId") fileId: string) { - return this.filesService.get(fileId); + return await this.filesService.get(fileId); } } diff --git a/apps/backend/src/app/files/files.service.ts b/apps/backend/src/app/files/files.service.ts index 3e4e288..d1eb69b 100644 --- a/apps/backend/src/app/files/files.service.ts +++ b/apps/backend/src/app/files/files.service.ts @@ -25,11 +25,11 @@ export class FilesService { ) {} /** - * Retrieves a file and its related information from the database and associated storage. + * Retrieves a file from the database and storage by its unique identifier. * - * @param fileId The unique identifier for the file to be retrieved. - * @return A promise that resolves to an object containing the streamable file and additional file information. - * @throws NotFoundException if the file does not exist in the database. + * @param fileId A string representing the unique identifier of the file. + * @return A Promise resolving to a StreamableFile if found. + * @throws NotFoundException If no file with the given identifier is found. */ public async get(fileId: string): Promise { const foundFiles = await this.database @@ -68,16 +68,29 @@ 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) { - return await this.database - .use() - .select() - .from(FilesTable) - .where(ilike(FilesTable.fileName, String(searchField))) - .limit(limit) - .offset(offset) - .prepare("searchFiles") - .execute(); + try { + return await this.database + .use() + .select() + .from(FilesTable) + .where(ilike(FilesTable.fileName, String(`%${searchField}%`))) + .limit(limit) + .offset(offset) + .prepare("searchFiles") + .execute(); + } catch (error) { + throw new InternalServerErrorException(error); + } } //TODO save a file