From 3d67b8ad18c659e25d83f32b73bf5d3f44703f72 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 10 Oct 2024 11:44:24 +0200 Subject: [PATCH] Add service methods for managing authors and files Implemented methods in AuthorsService to find authors, filter files by author, and delete authors. Integrated these methods in AuthorsController to handle HTTP requests accordingly. --- .../src/app/authors/authors.controller.ts | 21 ++++-- .../src/app/authors/authors.service.ts | 74 ++++++++++++++++++- 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/apps/backend/src/app/authors/authors.controller.ts b/apps/backend/src/app/authors/authors.controller.ts index 8474c74..def2b52 100644 --- a/apps/backend/src/app/authors/authors.controller.ts +++ b/apps/backend/src/app/authors/authors.controller.ts @@ -21,23 +21,32 @@ export class AuthorsController { @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, @Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number, @Query("search", new DefaultValuePipe("")) search: string, - ) {} + ) { + return await this.authorService.find(limit, offset, search); + } //TODO DTO - @Post("new") - async newAuthor() {} @UseGuards(AdminGuard) @Delete(":autor") - async deleteAuthor(@Param("author") author: string) {} + async deleteAuthor(@Param("author") author: string) { + return await this.authorService.delete(author); + } //TODO Patch @Get("files/:author") - async getFilesForAuthor( + async filterFilesForAuthor( @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, @Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number, @Query("search", new DefaultValuePipe("")) search: string, @Param("machineId") author: string, - ) {} + ) { + return await this.authorService.findFileForAuthor( + limit, + offset, + search, + author, + ); + } } diff --git a/apps/backend/src/app/authors/authors.service.ts b/apps/backend/src/app/authors/authors.service.ts index dd04e4e..51113a9 100644 --- a/apps/backend/src/app/authors/authors.service.ts +++ b/apps/backend/src/app/authors/authors.service.ts @@ -1,4 +1,76 @@ import { Injectable } from "@nestjs/common"; +import { DbService } from "apps/backend/src/app/db/db.service"; +import { FilesTable } from "apps/backend/src/app/db/schema"; +import { and, eq, ilike } from "drizzle-orm"; @Injectable() -export class AuthorsService {} +export class AuthorsService { + constructor(private readonly database: DbService) {} + + /** + * Searches for records in the FilesTable based on the uploadedBy field. + * + * @param {number} limit - The maximum number of records to return. + * @param {number} offset - The number of records to skip before starting to collect the result set. + * @param {string} searchField - The search term to filter the uploadedBy field. + * @return {Promise} A promise that resolves to an array of matching records. + */ + async find(limit: number, offset: number, searchField: string) { + return this.database + .use() + .select() + .from(FilesTable) + .where(ilike(FilesTable.uploadedBy, String(`%${searchField}%`))) + .limit(limit) + .offset(offset) + .prepare("searchAuthor") + .execute(); + } + + /** + * Finds files uploaded by a specific author based on search criteria. + * + * @param {number} limit - The maximum number of files to return. + * @param {number} offset - The offset for pagination purposes. + * @param {string} searchField - The search term to filter files by their name. + * @param {string} author - The author of the files. + * @return {Promise} A promise that resolves to an array of files matching the criteria. + */ + async findFileForAuthor( + limit: number, + offset: number, + searchField: string, + author: string, + ) { + return this.database + .use() + .select() + .from(FilesTable) + .where( + and( + eq(FilesTable.uploadedBy, author), + ilike(FilesTable.fileName, String(`%${searchField}%`)), + ), + ) + .prepare("searchAuthor") + .execute(); + } + + /** + * Deletes the reference of an author from the FilesTable by replacing the uploadedBy field with "none". + * + * @param {string} authorName - The name of the author whose reference is to be deleted. + * @return {Promise} A promise that resolves when the operation is complete. + */ + async delete(authorName: string) { + await this.database + .use() + .update(FilesTable) + .set({ + uploadedBy: "none", + }) + .where(eq(FilesTable.uploadedBy, authorName)) + .prepare("replaceAuthorFieldForMatch") + .execute(); + } +}