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.
This commit is contained in:
Mathis H (Avnyr) 2024-10-11 11:00:37 +02:00
parent 356b6869ad
commit 2ca13714a4
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
4 changed files with 29 additions and 12 deletions

View File

@ -73,10 +73,7 @@ export const FilesTable = pgTable("files", {
}) })
.notNull(), .notNull(),
groupId: p groupId: p.uuid("group_id").references(() => FilesGroupTable.uuid),
.uuid("group_id")
.notNull()
.references(() => FilesGroupTable.uuid),
fileSize: p.integer("file_size").notNull(), fileSize: p.integer("file_size").notNull(),

View File

@ -54,7 +54,7 @@ export class FilesController {
); );
// Vérifier que les en-têtes nécessaires sont présents // 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)"); throw new BadRequestException("Header(s) manquant(s)");
} }
console.log("Header found !"); console.log("Header found !");
@ -62,7 +62,7 @@ export class FilesController {
const Params = new Map() const Params = new Map()
.set("fileName", _fileName.toString()) .set("fileName", _fileName.toString())
.set("groupId", _groupId.toString()) .set("groupId", _groupId.toString() || null)
.set("uploadedBy", _uploadedBy.toString()) .set("uploadedBy", _uploadedBy.toString())
.set("machineId", Array(JSON.parse(machineId.toString()))) .set("machineId", Array(JSON.parse(machineId.toString())))
.set("isDocumentation", false) .set("isDocumentation", false)

View File

@ -14,7 +14,6 @@ import {
MachinesTable, MachinesTable,
} from "apps/backend/src/app/db/schema"; } from "apps/backend/src/app/db/schema";
import { StorageService } from "apps/backend/src/app/storage/storage.service"; import { StorageService } from "apps/backend/src/app/storage/storage.service";
import { data } from "autoprefixer";
import { eq, ilike } from "drizzle-orm"; import { eq, ilike } from "drizzle-orm";
@Injectable() @Injectable()
@ -24,6 +23,9 @@ export class FilesService {
private readonly database: DbService, private readonly database: DbService,
) {} ) {}
//TODO
//TODO
/** /**
* Retrieves a file from the database and storage by its unique identifier. * Retrieves a file from the database and storage by its unique identifier.
* *
@ -119,11 +121,11 @@ export class FilesService {
machinesIds.add(machineExists[0].uuid); 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); console.log("Linking to group :\n", _group);
if (!_group) { /*if (!_group) {
throw new NotFoundException(`Group with ID "${_group}" not found`); throw new NotFoundException(`Group with ID "${_group}" not found`);
} }*/
// verify that the group exist in the database // verify that the group exist in the database
const groupExists = await this.database const groupExists = await this.database
.use() .use()
@ -133,7 +135,7 @@ export class FilesService {
.prepare("checkGroupExists") .prepare("checkGroupExists")
.execute(); .execute();
if (groupExists.length === 0) { if (_group && groupExists.length === 0) {
throw new NotFoundException(`Group with ID "${_group}" not found`); throw new NotFoundException(`Group with ID "${_group}" not found`);
} }
@ -155,7 +157,7 @@ export class FilesService {
.insert(FilesTable) .insert(FilesTable)
.values({ .values({
fileName: data.get("fileName") as string, fileName: data.get("fileName") as string,
groupId: groupExists[0].uuid, groupId: groupExists[0].uuid || null,
checksum: saveResult.fileChecksum, checksum: saveResult.fileChecksum,
extension: saveResult.fileType.extension, extension: saveResult.fileType.extension,
fileSize: saveResult.fileSize, fileSize: saveResult.fileSize,

View File

@ -21,8 +21,26 @@ export class GroupsService {
} }
//TODO The method to create a group //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 //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) //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,
) {}
} }