update users module

This commit is contained in:
秦秋旭 2023-02-22 09:42:47 +08:00
parent 085deabc7f
commit dfa91f8e7a
2 changed files with 9 additions and 17 deletions

View File

@ -4,18 +4,22 @@ import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { User } from 'src/common/decorators/user.decorator'
import { NeedAuth } from 'src/common/decorators/need-auth.decorator'
import { PasswordInterceptor } from 'src/common/interceptors/password.interceptor'
import { PrismaService } from 'nestjs-prisma'
import { UserEntity } from './entities/user.entity'
@ApiTags('User')
@Controller('api/users')
export class UsersController {
constructor(private readonly userService: UsersService) {}
constructor(
private readonly userService: UsersService,
private readonly prismaService: PrismaService,
) {}
@ApiOperation({ summary: '获取用户信息' })
@UseInterceptors(PasswordInterceptor)
@NeedAuth()
@Get('me')
async getUserInfo(@User('userId') userId: string) {
const user = await this.userService.findUser({ id: userId })
return user
async getUserInfo(@User('userId') userId: string): Promise<UserEntity> {
return this.prismaService.user.findUniqueOrThrow({ where: { id: userId } })
}
}

View File

@ -1,19 +1,7 @@
import { Injectable, NotFoundException } from '@nestjs/common'
import { Injectable } from '@nestjs/common'
import { PrismaService } from 'nestjs-prisma'
import { Prisma } from '@prisma/client'
import { UserEntity } from './entities/user.entity'
@Injectable()
export class UsersService {
constructor(private prismaService: PrismaService) {}
async findUser(where: Prisma.UserWhereUniqueInput): Promise<UserEntity> {
const user = await this.prismaService.user.findUnique({
where,
})
if (!user) {
throw new NotFoundException(`No user found`)
}
return user
}
}