nest-project/src/users/users.controller.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-02-22 16:09:31 +08:00
import {
Controller,
Get,
Post,
Patch,
Body,
UseInterceptors,
} from '@nestjs/common'
2023-02-15 20:32:14 +08:00
import { UsersService } from './users.service'
import { ApiTags, ApiOperation } from '@nestjs/swagger'
2023-02-17 14:21:08 +08:00
import { User } from 'src/common/decorators/user.decorator'
import { NeedAuth } from 'src/common/decorators/need-auth.decorator'
2023-02-17 16:13:04 +08:00
import { PasswordInterceptor } from 'src/common/interceptors/password.interceptor'
2023-02-22 09:42:47 +08:00
import { PrismaService } from 'nestjs-prisma'
import { UserEntity } from './entities/user.entity'
import { CreateUserDto } from './dto/create-user.dto'
2023-02-22 16:09:31 +08:00
import { UpdatePassword } from './dto/update-password.dto'
2023-02-15 20:32:14 +08:00
2023-02-17 14:21:08 +08:00
@ApiTags('User')
2023-02-20 16:25:46 +08:00
@Controller('api/users')
2023-02-15 20:32:14 +08:00
export class UsersController {
2023-02-22 09:42:47 +08:00
constructor(
private readonly userService: UsersService,
private readonly prismaService: PrismaService,
) {}
2023-02-17 14:21:08 +08:00
2023-02-20 16:25:46 +08:00
@ApiOperation({ summary: '获取用户信息' })
2023-02-17 16:13:04 +08:00
@UseInterceptors(PasswordInterceptor)
2023-02-17 14:21:08 +08:00
@NeedAuth()
2023-02-20 16:25:46 +08:00
@Get('me')
2023-02-22 09:42:47 +08:00
async getUserInfo(@User('userId') userId: string): Promise<UserEntity> {
return this.prismaService.user.findUniqueOrThrow({ where: { id: userId } })
2023-02-17 14:21:08 +08:00
}
@ApiOperation({ summary: '注册用户' })
@Post()
async register(@Body() userData: CreateUserDto) {
return this.userService.register(userData)
}
2023-02-22 16:09:31 +08:00
@ApiOperation({ summary: '修改密码' })
@UseInterceptors(PasswordInterceptor)
@Patch('me/password')
async updatePassord(@Body() payload: UpdatePassword): Promise<UserEntity> {
return this.userService.updatePassword(payload)
}
// @ApiOperation({ summary: '修改邮箱' })
// @Patch('me/email')
// async updateEmail(@Body() payload: unknown) {
// return
// }
2023-02-15 20:32:14 +08:00
}