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(),
groupId: p
.uuid("group_id")
.notNull()
.references(() => FilesGroupTable.uuid),
groupId: p.uuid("group_id").references(() => FilesGroupTable.uuid),
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
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)

View File

@ -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,

View File

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