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.
This commit is contained in:
Mathis H (Avnyr) 2024-10-10 11:44:24 +02:00
parent 3bc440cbf8
commit 3d67b8ad18
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
2 changed files with 88 additions and 7 deletions

View File

@ -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,
);
}
}

View File

@ -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<Array>} 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<Array>} 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<void>} 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();
}
}