From 3eca2472c6a98cdb3b7ececc669f6a59d4a90d50 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 3 Oct 2024 12:08:17 +0200 Subject: [PATCH] Add CreateGroupDto and integrate DbModule with GroupsService Introduced CreateGroupDto to enforce validation rules for group creation. Integrated DbModule into GroupsService and added a getGroupsByName method for database queries. Placeholder methods for group creation, deletion and file retrieval are also defined. --- .../src/app/groups/groups.controller.ts | 12 +++++--- apps/backend/src/app/groups/groups.dto.ts | 8 ++++++ apps/backend/src/app/groups/groups.module.ts | 2 ++ apps/backend/src/app/groups/groups.service.ts | 28 ++++++++++++++++++- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/apps/backend/src/app/groups/groups.controller.ts b/apps/backend/src/app/groups/groups.controller.ts index 1036324..1f2fd2f 100644 --- a/apps/backend/src/app/groups/groups.controller.ts +++ b/apps/backend/src/app/groups/groups.controller.ts @@ -1,4 +1,5 @@ import { + Body, Controller, DefaultValuePipe, Delete, @@ -6,10 +7,11 @@ import { Param, ParseIntPipe, Post, - Query, -} from "@nestjs/common"; + Query +} from '@nestjs/common'; import { ISearchQuery } from "apps/backend/src/app/groups/groups.types"; import { GroupsService } from "./groups.service"; +import { CreateGroupDto } from 'apps/backend/src/app/groups/groups.dto'; @Controller("groups") export class GroupsController { @@ -22,12 +24,14 @@ export class GroupsController { @Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number, @Query("search", new DefaultValuePipe("")) search: string, ) { - const query = { limit, offset, search }; + //TODO add service method } //POST a new group @Post("new") - async newGroup() {} + async newGroup(@Body() dto : CreateGroupDto) { + + } //DELETE a group @Delete(":groupId") diff --git a/apps/backend/src/app/groups/groups.dto.ts b/apps/backend/src/app/groups/groups.dto.ts index e69de29..406b178 100644 --- a/apps/backend/src/app/groups/groups.dto.ts +++ b/apps/backend/src/app/groups/groups.dto.ts @@ -0,0 +1,8 @@ +import { IsString, MinLength, MaxLength } from 'class-validator'; + +export class CreateGroupDto { + @IsString() + @MinLength(4) + @MaxLength(64) + groupName: string; +} diff --git a/apps/backend/src/app/groups/groups.module.ts b/apps/backend/src/app/groups/groups.module.ts index 718f09d..608a07e 100644 --- a/apps/backend/src/app/groups/groups.module.ts +++ b/apps/backend/src/app/groups/groups.module.ts @@ -1,8 +1,10 @@ import { Module } from "@nestjs/common"; import { GroupsController } from "./groups.controller"; import { GroupsService } from "./groups.service"; +import { DbModule } from '../db/db.module'; @Module({ + imports: [DbModule], controllers: [GroupsController], providers: [GroupsService], }) diff --git a/apps/backend/src/app/groups/groups.service.ts b/apps/backend/src/app/groups/groups.service.ts index 29d53cc..a120625 100644 --- a/apps/backend/src/app/groups/groups.service.ts +++ b/apps/backend/src/app/groups/groups.service.ts @@ -1,4 +1,30 @@ import { Injectable } from "@nestjs/common"; +import { DbService } from 'apps/backend/src/app/db/db.service'; +import { FilesGroupTable } from 'apps/backend/src/app/db/schema'; +import { ilike } from 'drizzle-orm'; @Injectable() -export class GroupsService {} +export class GroupsService { + constructor(private readonly database: DbService) {} + + //TODO a method to fetch groups in the database by a specific search with limit, offset and a search field (can be blank) + async getGroupsByName(limit: number, offset: number, search: string) { + const result = await this.database.use() + .select() + .from(FilesGroupTable) + .where(ilike(FilesGroupTable.groupName, search)) + .limit(limit) + .offset(offset) + .prepare("getGroupsByName") + .execute() + console.log(`Found ${result.length} groups for search :\n > "${search}"`) + return result; + } + + //TODO The method to create a group + + + //TODO a method to delete a group and place the associated file at a null group reference + + //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) +}