Compare commits

...

5 Commits

Author SHA1 Message Date
356b6869ad
Remove commented-out code in schema
Eliminated a comment in the FilesGroupTable definition that was redundant. This helps in maintaining cleaner and more readable code.
2024-10-10 15:26:34 +02:00
030b5c814c
Add type management endpoints for machines
This commit introduces endpoints to add, remove, and list types associated with machines. It also includes the addition of the `TypeDto` class to handle type validations and updates HTTP response codes for the existing endpoints.
2024-10-10 15:26:06 +02:00
16ed8d3420
Refactor machines controller todo comments
Removed obsolete todos for DTO and Patch operations. Added todos for types handling related to machine operations as placeholders for future implementation.
2024-10-10 14:15:17 +02:00
0874ffb835
Implement machine list retrieval in controllers
Add functionality to fetch and return a list of machines with limit, offset, and search parameters using the machineService. This enhances the controller's capability to handle queries more effectively.
2024-10-10 14:10:23 +02:00
3d67b8ad18
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.
2024-10-10 11:44:24 +02:00
5 changed files with 128 additions and 15 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();
}
}

View File

@ -105,7 +105,6 @@ export const FilesTable = pgTable("files", {
});
export const FilesGroupTable = pgTable("f_groups", {
// Unique identifier on a technical aspect.
uuid: p.uuid("uuid").unique().primaryKey().defaultRandom().notNull(),
groupName: p

View File

@ -14,36 +14,64 @@ import {
UseGuards,
} from "@nestjs/common";
import { AdminGuard } from "apps/backend/src/app/auth/auth.guard";
import { CreateMachineDto } from "apps/backend/src/app/machines/machines.dto";
import {
CreateMachineDto,
TypeDto,
} from "apps/backend/src/app/machines/machines.dto";
import { MachinesService } from "apps/backend/src/app/machines/machines.service";
@Controller("machines")
export class MachinesController {
constructor(private readonly machineService: MachinesService) {}
@HttpCode(HttpStatus.OK)
@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 await this.machineService.findMany(limit, offset, search);
}
//TODO DTO
@UseGuards(AdminGuard)
@Post("new")
async newMachine(@Body() body: CreateMachineDto) {
return await this.machineService.create(body.machineName, body.machineType);
}
@HttpCode(HttpStatus.ACCEPTED)
@UseGuards(AdminGuard)
@Delete(":machineId")
async deleteMachine(@Param("machineId") machineId: string) {}
//TODO Patch
@HttpCode(HttpStatus.ACCEPTED)
@UseGuards(AdminGuard)
@Post("types/:machineId")
async addTypeToMachine(
@Param("machineId") machineId: string,
@Body() body: TypeDto,
) {
return await this.machineService.addFileType(machineId, body.fileTypeId);
}
//TODO CRUD fileType associated to machine
@HttpCode(HttpStatus.ACCEPTED)
@UseGuards(AdminGuard)
@Delete("types/:machineId")
async remTypeToMachine(
@Param("machineId") machineId: string,
@Body() body: TypeDto,
) {
return await this.machineService.removeFileType(machineId, body.fileTypeId);
}
@HttpCode(HttpStatus.OK)
@HttpCode(HttpStatus.FOUND)
@Get("types/:machineId")
async getTypesOfMachine(@Param("machineId") machineId: string) {
return await this.machineService.getFilesTypes(machineId);
}
@HttpCode(HttpStatus.FOUND)
@Get("files/:machineId")
async getFilesForMachine(
@Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number,

View File

@ -1,4 +1,4 @@
import { MaxLength, MinLength } from "class-validator";
import { IsUUID, MaxLength, MinLength } from "class-validator";
export class CreateMachineDto {
@MaxLength(128)
@ -9,3 +9,8 @@ export class CreateMachineDto {
@MinLength(2)
machineType: string;
}
export class TypeDto {
@IsUUID()
fileTypeId: string;
}