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.
This commit is contained in:
M1000fr 2024-12-10 11:40:04 +01:00
parent 3d131193b6
commit 238b01f5f3
10 changed files with 115 additions and 2 deletions

View File

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

View File

@ -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);
}
}

View File

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

View File

@ -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 } });
}
}

View File

@ -0,0 +1,3 @@
export class CreateClassDto {
name: string;
}

View File

@ -0,0 +1,4 @@
import { PartialType } from "@nestjs/swagger";
import { CreateClassDto } from "./create-class.dto";
export class UpdateClassDto extends PartialType(CreateClassDto) {}

View File

@ -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<ClassEntity>) {
Object.assign(this, partial);
}
}

View File

@ -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()

2
src/types/http.d.ts vendored
View File

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