diff --git a/src/modules/user/ApiResponses/UserReponse.ts b/src/modules/user/ApiResponses/UserReponse.ts index 66ca59d..fdff4fa 100644 --- a/src/modules/user/ApiResponses/UserReponse.ts +++ b/src/modules/user/ApiResponses/UserReponse.ts @@ -11,6 +11,13 @@ export const UserResponse = { id: "1", role: "ADMIN", username: "admin", + Class: [ + { + id: "1", + name: "Class 1", + createdAt: new Date(), + }, + ], } as UserEntity, }, }, diff --git a/src/modules/user/entities/user.entity.ts b/src/modules/user/entities/user.entity.ts index 0891de3..d424bd4 100644 --- a/src/modules/user/entities/user.entity.ts +++ b/src/modules/user/entities/user.entity.ts @@ -1,3 +1,4 @@ +import { ClassEntity } from "@/modules/class/entities/class.entity"; import { ApiProperty, ApiSchema } from "@nestjs/swagger"; import { Expose } from "class-transformer"; @@ -11,6 +12,10 @@ export class UserEntity { @ApiProperty() username: string; + @Expose() + @ApiProperty() + Class?: ClassEntity[]; + constructor(partial: Partial) { Object.assign(this, partial); } diff --git a/src/modules/user/user.controller.ts b/src/modules/user/user.controller.ts index 74e8327..0a13f82 100644 --- a/src/modules/user/user.controller.ts +++ b/src/modules/user/user.controller.ts @@ -11,7 +11,7 @@ import { Patch, Query, Req, - UseGuards + UseGuards, } from "@nestjs/common"; import { ApiBearerAuth, @@ -19,8 +19,9 @@ import { ApiOperation, ApiParam, ApiQuery, - ApiUnauthorizedResponse + ApiUnauthorizedResponse, } from "@nestjs/swagger"; +import { Request } from "express"; import { UserCountResponse, UserResponse, @@ -29,7 +30,6 @@ import { import { UpdateUserDTO } from "./dto/update-user.dto"; import { UserEntity } from "./entities/user.entity"; import { UserService } from "./user.service"; -import { Request } from "express"; @Controller("user") @UseGuards(RolesGuard) @@ -45,14 +45,21 @@ export class UserController { @ApiOperation({ summary: "Get all users" }) async findAll(): Promise { return await this.userService - .findAll() + .findAll({ + include: { + Class: true, + }, + }) .then((users) => users.map((user) => new UserEntity(user))); } @Get(":id") @ApiOkResponse(UserResponse) @ApiOperation({ summary: "Get user by id" }) - async findOne(@Param("id") id: string, @Req() req: Request): Promise { + async findOne( + @Param("id") id: string, + @Req() req: Request, + ): Promise { if (id === "@me") id = req.user.id; return this.userService diff --git a/src/modules/user/user.service.ts b/src/modules/user/user.service.ts index a048a36..0752c5e 100644 --- a/src/modules/user/user.service.ts +++ b/src/modules/user/user.service.ts @@ -7,7 +7,7 @@ import { UpdateUserDTO } from "./dto/update-user.dto"; @Injectable() export class UserService { - constructor(private readonly prisma: PrismaService) { } + constructor(private readonly prisma: PrismaService) {} async findAll({ skip, @@ -15,12 +15,14 @@ export class UserService { cursor, where, orderBy, + include, }: { skip?: number; take?: number; cursor?: Prisma.UserWhereUniqueInput; where?: Prisma.UserWhereInput; orderBy?: Record; + include?: Prisma.UserInclude; } = {}) { return await this.prisma.user.findMany({ skip, @@ -28,6 +30,7 @@ export class UserService { cursor, where, orderBy, + include, }); } @@ -48,7 +51,7 @@ export class UserService { user = await this.prisma.user.create({ data: { id, - username + username, }, }); } else if (user.username !== username) { @@ -103,7 +106,7 @@ export class UserService { select: { id: true, username: true, - } + }, }); } } diff --git a/src/types/http.d.ts b/src/types/http.d.ts index fdecb74..e6c8f1e 100644 --- a/src/types/http.d.ts +++ b/src/types/http.d.ts @@ -4,6 +4,6 @@ import { IncomingMessage } from "http"; declare module "http" { interface IncomingMessage { - user?: UserEntity; + user?: User; } }