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:
parent
3d131193b6
commit
238b01f5f3
@ -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],
|
||||
})
|
||||
|
42
src/modules/class/class.controller.ts
Normal file
42
src/modules/class/class.controller.ts
Normal 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);
|
||||
}
|
||||
}
|
9
src/modules/class/class.module.ts
Normal file
9
src/modules/class/class.module.ts
Normal 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 {}
|
32
src/modules/class/class.service.ts
Normal file
32
src/modules/class/class.service.ts
Normal 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 } });
|
||||
}
|
||||
}
|
3
src/modules/class/dto/create-class.dto.ts
Normal file
3
src/modules/class/dto/create-class.dto.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export class CreateClassDto {
|
||||
name: string;
|
||||
}
|
4
src/modules/class/dto/update-class.dto.ts
Normal file
4
src/modules/class/dto/update-class.dto.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { PartialType } from "@nestjs/swagger";
|
||||
import { CreateClassDto } from "./create-class.dto";
|
||||
|
||||
export class UpdateClassDto extends PartialType(CreateClassDto) {}
|
21
src/modules/class/entities/class.entity.ts
Normal file
21
src/modules/class/entities/class.entity.ts
Normal 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);
|
||||
}
|
||||
}
|
@ -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
2
src/types/http.d.ts
vendored
@ -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";
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user