From 2ca13714a4affc4597587e53d3fa729dc90d5527 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 11 Oct 2024 11:00:37 +0200 Subject: [PATCH] Refactor file and group handling logic Allow groupId to be optional and added methods for group operations. Updated file insertion to handle optional groupId and adjusted group verification logic accordingly. Added method stubs for creating, deleting, and finding files within groups. --- apps/backend/src/app/db/schema.ts | 5 +---- apps/backend/src/app/files/files.controller.ts | 4 ++-- apps/backend/src/app/files/files.service.ts | 14 ++++++++------ apps/backend/src/app/groups/groups.service.ts | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/apps/backend/src/app/db/schema.ts b/apps/backend/src/app/db/schema.ts index a59754b..f3b1a90 100644 --- a/apps/backend/src/app/db/schema.ts +++ b/apps/backend/src/app/db/schema.ts @@ -73,10 +73,7 @@ export const FilesTable = pgTable("files", { }) .notNull(), - groupId: p - .uuid("group_id") - .notNull() - .references(() => FilesGroupTable.uuid), + groupId: p.uuid("group_id").references(() => FilesGroupTable.uuid), fileSize: p.integer("file_size").notNull(), diff --git a/apps/backend/src/app/files/files.controller.ts b/apps/backend/src/app/files/files.controller.ts index ffaed1e..2381a41 100644 --- a/apps/backend/src/app/files/files.controller.ts +++ b/apps/backend/src/app/files/files.controller.ts @@ -54,7 +54,7 @@ export class FilesController { ); // Vérifier que les en-têtes nécessaires sont présents - if (!_fileName || !_groupId || !_machineId) { + if (!_fileName || !_machineId) { throw new BadRequestException("Header(s) manquant(s)"); } console.log("Header found !"); @@ -62,7 +62,7 @@ export class FilesController { const Params = new Map() .set("fileName", _fileName.toString()) - .set("groupId", _groupId.toString()) + .set("groupId", _groupId.toString() || null) .set("uploadedBy", _uploadedBy.toString()) .set("machineId", Array(JSON.parse(machineId.toString()))) .set("isDocumentation", false) diff --git a/apps/backend/src/app/files/files.service.ts b/apps/backend/src/app/files/files.service.ts index d1eb69b..7ec3265 100644 --- a/apps/backend/src/app/files/files.service.ts +++ b/apps/backend/src/app/files/files.service.ts @@ -14,7 +14,6 @@ import { MachinesTable, } from "apps/backend/src/app/db/schema"; import { StorageService } from "apps/backend/src/app/storage/storage.service"; -import { data } from "autoprefixer"; import { eq, ilike } from "drizzle-orm"; @Injectable() @@ -24,6 +23,9 @@ export class FilesService { private readonly database: DbService, ) {} + //TODO + //TODO + /** * Retrieves a file from the database and storage by its unique identifier. * @@ -119,11 +121,11 @@ export class FilesService { machinesIds.add(machineExists[0].uuid); } - const _group = data.get("groupId") as string; + const _group = data.get("groupId") as string | null; console.log("Linking to group :\n", _group); - if (!_group) { + /*if (!_group) { throw new NotFoundException(`Group with ID "${_group}" not found`); - } + }*/ // verify that the group exist in the database const groupExists = await this.database .use() @@ -133,7 +135,7 @@ export class FilesService { .prepare("checkGroupExists") .execute(); - if (groupExists.length === 0) { + if (_group && groupExists.length === 0) { throw new NotFoundException(`Group with ID "${_group}" not found`); } @@ -155,7 +157,7 @@ export class FilesService { .insert(FilesTable) .values({ fileName: data.get("fileName") as string, - groupId: groupExists[0].uuid, + groupId: groupExists[0].uuid || null, checksum: saveResult.fileChecksum, extension: saveResult.fileType.extension, fileSize: saveResult.fileSize, diff --git a/apps/backend/src/app/groups/groups.service.ts b/apps/backend/src/app/groups/groups.service.ts index 1dd5655..6cfc453 100644 --- a/apps/backend/src/app/groups/groups.service.ts +++ b/apps/backend/src/app/groups/groups.service.ts @@ -21,8 +21,26 @@ export class GroupsService { } //TODO The method to create a group + async newGroup(groupName: string) { + return await this.database + .use() + .insert(FilesGroupTable) + .values({ + groupName: groupName, + }) + .returning() + .prepare("newGroup") + .execute(); + } //TODO a method to delete a group and place the associated file at a null group reference + async deleteGroup(groupId: string) {} //TODO a method to get the files of a group in the database by a specific search with limit, offset and a search field (can be blank) + async findFilesForGroup( + limit: number, + offset: number, + searchField: string, + groupId: string, + ) {} }