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";