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

38 lines
997 B
TypeScript
Raw Normal View History

2023-02-16 10:40:03 +08:00
import { Injectable, NotFoundException } from '@nestjs/common'
2023-02-15 20:32:14 +08:00
import { PrismaService } from 'nestjs-prisma'
2023-02-16 10:40:03 +08:00
import { CreateUserDto } from './dto/create-user.dto'
import { PasswordService } from './password.service'
import { Prisma } from '@prisma/client'
2023-02-17 14:21:08 +08:00
import { UserEntity } from './entities/user.entity'
2023-02-15 20:32:14 +08:00
@Injectable()
export class UsersService {
2023-02-16 10:40:03 +08:00
constructor(
private prisma: PrismaService,
private passwordService: PasswordService,
) {}
2023-02-17 14:21:08 +08:00
async findUser(where: Prisma.UserWhereUniqueInput): Promise<UserEntity> {
2023-02-16 10:40:03 +08:00
const user = await this.prisma.user.findUnique({
where,
})
if (!user) {
throw new NotFoundException(`No user found`)
}
return user
}
async createUser(payload: CreateUserDto) {
const hashedPassword = await this.passwordService.hashPassword(
payload.password,
)
const user = await this.prisma.user.create({
data: {
...payload,
password: hashedPassword,
},
})
return user
}
2023-02-15 20:32:14 +08:00
}