From dfa91f8e7a21c33bf4f63acf2ef399505ddff188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E7=A7=8B=E6=97=AD?= Date: Wed, 22 Feb 2023 09:42:47 +0800 Subject: [PATCH] update users module --- src/users/users.controller.ts | 12 ++++++++---- src/users/users.service.ts | 14 +------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index d5e96c7..1c45339 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -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 { + return this.prismaService.user.findUniqueOrThrow({ where: { id: userId } }) } } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 14b390b..c35c3d3 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -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 { - const user = await this.prismaService.user.findUnique({ - where, - }) - if (!user) { - throw new NotFoundException(`No user found`) - } - return user - } }