feat: add Class property to UserEntity and update user retrieval to include classes
This commit is contained in:
parent
7aab149bb2
commit
7fa7e4448d
@ -11,6 +11,13 @@ export const UserResponse = {
|
||||
id: "1",
|
||||
role: "ADMIN",
|
||||
username: "admin",
|
||||
Class: [
|
||||
{
|
||||
id: "1",
|
||||
name: "Class 1",
|
||||
createdAt: new Date(),
|
||||
},
|
||||
],
|
||||
} as UserEntity,
|
||||
},
|
||||
},
|
||||
|
@ -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<UserEntity>) {
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
|
@ -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<UserEntity[]> {
|
||||
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<UserEntity> {
|
||||
async findOne(
|
||||
@Param("id") id: string,
|
||||
@Req() req: Request,
|
||||
): Promise<UserEntity> {
|
||||
if (id === "@me") id = req.user.id;
|
||||
|
||||
return this.userService
|
||||
|
@ -15,12 +15,14 @@ export class UserService {
|
||||
cursor,
|
||||
where,
|
||||
orderBy,
|
||||
include,
|
||||
}: {
|
||||
skip?: number;
|
||||
take?: number;
|
||||
cursor?: Prisma.UserWhereUniqueInput;
|
||||
where?: Prisma.UserWhereInput;
|
||||
orderBy?: Record<string, unknown>;
|
||||
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,
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
2
src/types/http.d.ts
vendored
2
src/types/http.d.ts
vendored
@ -4,6 +4,6 @@ import { IncomingMessage } from "http";
|
||||
|
||||
declare module "http" {
|
||||
interface IncomingMessage {
|
||||
user?: UserEntity;
|
||||
user?: User;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user