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.
This commit is contained in:
parent
a0f5c3dab6
commit
ec8af843b5
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<StreamableFile> {
|
||||
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<object>} 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user