diff --git a/apps/backend/src/app/groups/groups.controller.ts b/apps/backend/src/app/groups/groups.controller.ts index e9deb0a..b9cde4e 100644 --- a/apps/backend/src/app/groups/groups.controller.ts +++ b/apps/backend/src/app/groups/groups.controller.ts @@ -25,18 +25,18 @@ export class GroupsController { @Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number, @Query("search", new DefaultValuePipe("")) search: string, ) { - return await this.groupsService.getGroupsByName(limit, offset, search) + return await this.groupsService.getGroupsByName(limit, offset, search); } @Post("new") async newGroup(@Body() body: CreateGroupDto) { - return await this.groupsService.newGroup(body.groupName) + return await this.groupsService.newGroup(body.groupName); } @UseGuards(AdminGuard) @Delete(":groupId") async deleteGroup(@Param("groupId") groupId: string) { - return await this.groupsService.deleteGroup(groupId) + return await this.groupsService.deleteGroup(groupId); } //TODO Patch @@ -48,6 +48,11 @@ export class GroupsController { @Query("search", new DefaultValuePipe("")) search: string, @Param("groupId") groupId: string, ) { - return await this.groupsService.findFilesForGroup(limit, offset, search, groupId) + return await this.groupsService.findFilesForGroup( + limit, + offset, + search, + groupId, + ); } } diff --git a/apps/backend/src/app/groups/groups.service.ts b/apps/backend/src/app/groups/groups.service.ts index c4d0c9d..f1fc42a 100644 --- a/apps/backend/src/app/groups/groups.service.ts +++ b/apps/backend/src/app/groups/groups.service.ts @@ -1,7 +1,11 @@ -import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'; +import { + Injectable, + InternalServerErrorException, + NotFoundException, +} from "@nestjs/common"; import { DbService } from "apps/backend/src/app/db/db.service"; -import { FilesGroupTable, FilesTable } from 'apps/backend/src/app/db/schema'; -import { and, eq, ilike } from 'drizzle-orm'; +import { FilesGroupTable, FilesTable } from "apps/backend/src/app/db/schema"; +import { and, eq, ilike } from "drizzle-orm"; @Injectable() export class GroupsService { @@ -19,7 +23,6 @@ export class GroupsService { .execute(); } - //TODO The method to create a group async newGroup(groupName: string) { return await this.database .use() @@ -32,9 +35,9 @@ export class GroupsService { .execute(); } - //TODO a method to delete a group and place the associated file at a null group reference async deleteGroup(groupId: string) { - const groupInDb = await this.database.use() + const groupInDb = await this.database + .use() .select() .from(FilesGroupTable) .where(eq(FilesGroupTable.uuid, groupId)) @@ -43,24 +46,24 @@ export class GroupsService { if (groupInDb.length === 0) throw new NotFoundException("Group not found"); // Replace entry by null await this.database - .use() - .update(FilesTable) - // @ts-ignore - .set({ groupId: null }) - .where(eq(FilesTable.groupId, groupId)) - .prepare("updateFilesGroupReference") - .execute(); + .use() + .update(FilesTable) + // @ts-ignore + .set({ groupId: null }) + .where(eq(FilesTable.groupId, groupId)) + .prepare("updateFilesGroupReference") + .execute(); try { await this.database .use() .delete(FilesGroupTable) - .where(eq(FilesGroupTable.uuid, groupId)) - return true + .where(eq(FilesGroupTable.uuid, groupId)); + return true; } catch (e) { throw new InternalServerErrorException("Error while deleting group"); } } - + async findFilesForGroup( limit: number, offset: number, @@ -71,10 +74,15 @@ export class GroupsService { .use() .select() .from(FilesTable) - .where(and(eq(FilesTable.groupId, groupId), ilike(FilesTable.fileName, String(`%${searchField}%`)) ) ) + .where( + and( + eq(FilesTable.groupId, groupId), + ilike(FilesTable.fileName, String(`%${searchField}%`)), + ), + ) .limit(limit) .offset(offset) - .prepare('findFilesInGroup') + .prepare("findFilesInGroup") .execute(); } }