From 081b3a303d0d6e7566d954650d643f888d8cd722 Mon Sep 17 00:00:00 2001 From: IlhamTahir Date: Tue, 10 Dec 2024 19:12:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/controller/user.controller.ts | 23 +++++++++++++++++++- src/core/dto/update-user.request.ts | 24 +++++++++++++++++++++ src/core/entity/user.entity.ts | 29 ++++++++++++++++++++++++++ src/core/enum/Gender.ts | 5 +++++ src/core/mapper/user.mapper.ts | 6 ++++++ src/core/service/user.service.ts | 16 ++++++++++++++ src/core/vo/user.vo.ts | 19 +++++++++++++++++ 7 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/core/dto/update-user.request.ts create mode 100644 src/core/enum/Gender.ts diff --git a/src/core/controller/user.controller.ts b/src/core/controller/user.controller.ts index a8f8134..0614410 100644 --- a/src/core/controller/user.controller.ts +++ b/src/core/controller/user.controller.ts @@ -1,4 +1,12 @@ -import { Controller, Get, HttpStatus, Query } from '@nestjs/common'; +import { + Body, + Controller, + Get, + HttpStatus, + Param, + Put, + Query, +} from '@nestjs/common'; import { UserService } from '../service/user.service'; import { SearchUserFilter } from '../dto/search-user.filter'; import { NoAuth } from '../decorators/no-auth.decorator'; @@ -12,6 +20,7 @@ import { getSchemaPath, } from '@nestjs/swagger'; import { PageResult } from '../vo/page-result'; +import { UpdateUserRequest } from '../dto/update-user.request'; @Controller('users') export class UserController { @@ -56,4 +65,16 @@ export class UserController { async current() { return UserMapper.toVo(this.userService.getCurrentUser()); } + + @Put('current') + @ApiBearerAuth() + @ApiOkResponse({ + description: '更新用户信息', + type: UserVo, + }) + async update(@Body() updateUserRequest: UpdateUserRequest): Promise { + return UserMapper.toVo( + await this.userService.updateCurrentUser(updateUserRequest), + ); + } } diff --git a/src/core/dto/update-user.request.ts b/src/core/dto/update-user.request.ts new file mode 100644 index 0000000..b525419 --- /dev/null +++ b/src/core/dto/update-user.request.ts @@ -0,0 +1,24 @@ +import { IsOptional } from 'class-validator'; +import { Gender } from '../enum/Gender'; +import { ApiProperty } from '@nestjs/swagger'; + +export class UpdateUserRequest { + @ApiProperty() + @IsOptional() + trueName: string; + @ApiProperty() + @IsOptional() + age: number; + @ApiProperty() + @IsOptional() + gender: Gender; + @ApiProperty() + @IsOptional() + avatar: string; + @ApiProperty() + @IsOptional() + occupation: string; + @ApiProperty() + @IsOptional() + address: string; +} diff --git a/src/core/entity/user.entity.ts b/src/core/entity/user.entity.ts index f230659..86a8ef3 100644 --- a/src/core/entity/user.entity.ts +++ b/src/core/entity/user.entity.ts @@ -2,6 +2,7 @@ import { Column, Entity, JoinTable, ManyToMany, OneToMany } from 'typeorm'; import { BaseEntity } from './base.entity'; import { Role } from './role.entity'; import { UserBind } from './user-bind.entity'; +import { Gender } from '../enum/Gender'; @Entity() export class User extends BaseEntity { @@ -23,6 +24,34 @@ export class User extends BaseEntity { }) enabled: boolean = true; + @Column() + trueName: string; + + @Column() + age: number; + + @Column({ + type: 'enum', + enum: Gender, + default: Gender.UNKNOWN, + }) + gender: Gender = Gender.UNKNOWN; + + @Column({ + nullable: true, + }) + occupation: string; + + @Column({ + nullable: true, + }) + address: string; + + @Column({ + nullable: true, + }) + avatar: string; + @ManyToMany(() => Role, (role) => role.users) @JoinTable({ name: 'user_role', diff --git a/src/core/enum/Gender.ts b/src/core/enum/Gender.ts new file mode 100644 index 0000000..0049f8b --- /dev/null +++ b/src/core/enum/Gender.ts @@ -0,0 +1,5 @@ +export enum Gender { + MALE, + FEMALE, + UNKNOWN, +} diff --git a/src/core/mapper/user.mapper.ts b/src/core/mapper/user.mapper.ts index f21c674..5782156 100644 --- a/src/core/mapper/user.mapper.ts +++ b/src/core/mapper/user.mapper.ts @@ -10,6 +10,12 @@ export class UserMapper { username: entity.username, locked: entity.locked, enabled: entity.enabled, + trueName: entity.trueName, + age: entity.age, + gender: entity.gender, + occupation: entity.occupation, + address: entity.address, + avatar: entity.avatar, createdTime: DateUtil.format(entity.createdTime), updatedTime: DateUtil.format(entity.updatedTime), }; diff --git a/src/core/service/user.service.ts b/src/core/service/user.service.ts index 398a6be..b20774f 100644 --- a/src/core/service/user.service.ts +++ b/src/core/service/user.service.ts @@ -6,6 +6,7 @@ import * as bcrypt from 'bcrypt'; import { SearchUserFilter } from '../dto/search-user.filter'; import { JwtPayload } from 'jsonwebtoken'; import { CreateUserRequest } from '../dto/create-user.request'; +import { UpdateUserRequest } from '../dto/update-user.request'; @Injectable() export class UserService { @@ -85,4 +86,19 @@ export class UserService { user.encryptedPassword = await bcrypt.hash('admin123', salt); return this.userRepository.save(user); } + + async update(id: string, updateUserRequest: UpdateUserRequest) { + const user = await this.findById(id); + user.trueName = updateUserRequest.trueName; + user.age = updateUserRequest.age; + user.gender = updateUserRequest.gender; + user.avatar = updateUserRequest.avatar; + user.occupation = updateUserRequest.occupation; + user.address = updateUserRequest.address; + return this.userRepository.save(user); + } + + async updateCurrentUser(updateUserRequest: UpdateUserRequest) { + return this.update(this.currentUser.id, updateUserRequest); + } } diff --git a/src/core/vo/user.vo.ts b/src/core/vo/user.vo.ts index dc700ae..ff824b6 100644 --- a/src/core/vo/user.vo.ts +++ b/src/core/vo/user.vo.ts @@ -1,5 +1,6 @@ import { BaseVo } from './base.vo'; import { ApiProperty, ApiSchema } from '@nestjs/swagger'; +import { Gender } from '../enum/Gender'; @ApiSchema({ name: 'User', @@ -11,4 +12,22 @@ export class UserVo extends BaseVo { locked: boolean; @ApiProperty() enabled: boolean; + + @ApiProperty() + trueName: string; + + @ApiProperty() + age: number; + + @ApiProperty() + gender: Gender = Gender.UNKNOWN; + + @ApiProperty() + occupation: string; + + @ApiProperty() + avatar: string; + + @ApiProperty() + address: string; }