From 238b01f5f37aefe8752e121dbe2b2fa833c4a785 Mon Sep 17 00:00:00 2001 From: M1000fr Date: Tue, 10 Dec 2024 11:40:04 +0100 Subject: [PATCH] feat: Add Class module and controller Add a new Class module and controller to handle CRUD operations for classes. This includes creating a new file for the Class controller, as well as creating the Class module and updating the app.module.ts file to include the Class module. --- src/app.module.ts | 2 + src/modules/class/class.controller.ts | 42 +++++++++++++++++++ src/modules/class/class.module.ts | 9 ++++ src/modules/class/class.service.ts | 32 ++++++++++++++ src/modules/class/dto/create-class.dto.ts | 3 ++ src/modules/class/dto/update-class.dto.ts | 4 ++ src/modules/class/entities/class.entity.ts | 21 ++++++++++ .../user/{ => entities}/user.entity.ts | 0 src/modules/user/user.controller.ts | 2 +- src/types/http.d.ts | 2 +- 10 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 src/modules/class/class.controller.ts create mode 100644 src/modules/class/class.module.ts create mode 100644 src/modules/class/class.service.ts create mode 100644 src/modules/class/dto/create-class.dto.ts create mode 100644 src/modules/class/dto/update-class.dto.ts create mode 100644 src/modules/class/entities/class.entity.ts rename src/modules/user/{ => entities}/user.entity.ts (100%) diff --git a/src/app.module.ts b/src/app.module.ts index 34a559c..848c1cc 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -8,6 +8,7 @@ import { AuthModule } from "@Modules/auth/auth.module"; import { UserModule } from "@Modules/user/user.module"; import { PrismaModule } from "nestjs-prisma"; import { AppController } from "./app.controller"; +import { ClassModule } from "./modules/class/class.module"; @Module({ imports: [ @@ -31,6 +32,7 @@ import { AppController } from "./app.controller"; }), UserModule, AuthModule, + ClassModule, ], controllers: [AppController], }) diff --git a/src/modules/class/class.controller.ts b/src/modules/class/class.controller.ts new file mode 100644 index 0000000..8ae4133 --- /dev/null +++ b/src/modules/class/class.controller.ts @@ -0,0 +1,42 @@ +import { + Controller, + Get, + Post, + Body, + Patch, + Param, + Delete, +} from "@nestjs/common"; +import { ClassService } from "./class.service"; +import { CreateClassDto } from "./dto/create-class.dto"; +import { UpdateClassDto } from "./dto/update-class.dto"; + +@Controller("class") +export class ClassController { + constructor(private readonly classService: ClassService) {} + + @Post() + create(@Body() createClassDto: CreateClassDto) { + return this.classService.create(createClassDto); + } + + @Get() + findAll() { + return this.classService.findAll(); + } + + @Get(":id") + findOne(@Param("id") id: string) { + return this.classService.findOne(+id); + } + + @Patch(":id") + update(@Param("id") id: string, @Body() updateClassDto: UpdateClassDto) { + return this.classService.update(+id, updateClassDto); + } + + @Delete(":id") + remove(@Param("id") id: string) { + return this.classService.remove(+id); + } +} diff --git a/src/modules/class/class.module.ts b/src/modules/class/class.module.ts new file mode 100644 index 0000000..992c43b --- /dev/null +++ b/src/modules/class/class.module.ts @@ -0,0 +1,9 @@ +import { Module } from "@nestjs/common"; +import { ClassService } from "./class.service"; +import { ClassController } from "./class.controller"; + +@Module({ + controllers: [ClassController], + providers: [ClassService], +}) +export class ClassModule {} diff --git a/src/modules/class/class.service.ts b/src/modules/class/class.service.ts new file mode 100644 index 0000000..b2e33f4 --- /dev/null +++ b/src/modules/class/class.service.ts @@ -0,0 +1,32 @@ +import { Injectable } from "@nestjs/common"; +import { CreateClassDto } from "./dto/create-class.dto"; +import { UpdateClassDto } from "./dto/update-class.dto"; +import { PrismaService } from "nestjs-prisma"; + +@Injectable() +export class ClassService { + constructor(private readonly prisma: PrismaService) {} + + async create(createClassDto: CreateClassDto) { + return await this.prisma.class.create({ data: createClassDto }); + } + + async findAll({ name }: { name: string }) { + return await this.prisma.class.create({ data: { name } }); + } + + async findOne(id: string) { + return await this.prisma.class.findUnique({ where: { id } }); + } + + async update(id: string, updateClassDto: UpdateClassDto) { + return await this.prisma.class.update({ + where: { id }, + data: updateClassDto, + }); + } + + async remove(id: string) { + return await this.prisma.class.delete({ where: { id } }); + } +} diff --git a/src/modules/class/dto/create-class.dto.ts b/src/modules/class/dto/create-class.dto.ts new file mode 100644 index 0000000..00e3628 --- /dev/null +++ b/src/modules/class/dto/create-class.dto.ts @@ -0,0 +1,3 @@ +export class CreateClassDto { + name: string; +} diff --git a/src/modules/class/dto/update-class.dto.ts b/src/modules/class/dto/update-class.dto.ts new file mode 100644 index 0000000..b86fdb5 --- /dev/null +++ b/src/modules/class/dto/update-class.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from "@nestjs/swagger"; +import { CreateClassDto } from "./create-class.dto"; + +export class UpdateClassDto extends PartialType(CreateClassDto) {} diff --git a/src/modules/class/entities/class.entity.ts b/src/modules/class/entities/class.entity.ts new file mode 100644 index 0000000..769b0ce --- /dev/null +++ b/src/modules/class/entities/class.entity.ts @@ -0,0 +1,21 @@ +import { ApiProperty, ApiSchema } from "@nestjs/swagger"; +import { Expose } from "class-transformer"; + +@ApiSchema({ name: "Class" }) +export class ClassEntity { + @Expose() + @ApiProperty() + id: string; + + @Expose() + @ApiProperty() + name: string; + + @Expose() + @ApiProperty() + createdAt: Date; + + constructor(partial: Partial) { + Object.assign(this, partial); + } +} diff --git a/src/modules/user/user.entity.ts b/src/modules/user/entities/user.entity.ts similarity index 100% rename from src/modules/user/user.entity.ts rename to src/modules/user/entities/user.entity.ts diff --git a/src/modules/user/user.controller.ts b/src/modules/user/user.controller.ts index 265780b..448e7cb 100644 --- a/src/modules/user/user.controller.ts +++ b/src/modules/user/user.controller.ts @@ -22,7 +22,7 @@ import { RolesGuard } from "@/modules/auth/guards/role.guard"; import { BulkDeleteUserDTO } from "./dto/bulk-delete-user.dto"; import { CreateUserDTO } from "./dto/create-user.dto"; -import { UserEntity } from "./user.entity"; +import { UserEntity } from "./entities/user.entity"; import { UserService } from "./user.service"; @Controller() diff --git a/src/types/http.d.ts b/src/types/http.d.ts index 5144fb5..fdecb74 100644 --- a/src/types/http.d.ts +++ b/src/types/http.d.ts @@ -1,4 +1,4 @@ -import { UserEntity } from "@/modules/user/user.entity"; +import { UserEntity } from "@/modules/user/entities/user.entity"; import { User } from "@prisma/client"; import { IncomingMessage } from "http";