diff --git a/backend/src/user/dto/create-user.dto.ts b/backend/src/user/dto/create-user.dto.ts index 0311be13..8a5e72e0 100644 --- a/backend/src/user/dto/create-user.dto.ts +++ b/backend/src/user/dto/create-user.dto.ts @@ -1 +1,12 @@ -export class CreateUserDto {} +import { ApiProperty } from '@nestjs/swagger'; + +export class CreateUserDto { + @ApiProperty() + email: string; + + @ApiProperty({ required: false }) + name?: string; + + @ApiProperty() + password: string; +} diff --git a/backend/src/user/user.controller.ts b/backend/src/user/user.controller.ts index 25ff847e..edd6b0a1 100644 --- a/backend/src/user/user.controller.ts +++ b/backend/src/user/user.controller.ts @@ -8,6 +8,7 @@ import { Delete, } from '@nestjs/common'; import { UserService } from './user.service'; +import { CreateUserDto } from './dto/create-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; import { User as UserModel } from '@prisma/client'; @@ -17,9 +18,9 @@ export class UserController { @Post() create( - @Body() userData: { name?: string; email: string; password: string }, + @Body() createUserDto: CreateUserDto, ): Promise { - return this.userService.create(userData); + return this.userService.create(createUserDto); } @Get() @@ -35,9 +36,9 @@ export class UserController { @Patch(':id') update( @Param('id') id: string, - @Body() userData: { name?: string; email?: string; password?: string }, + @Body() updateUserDto: UpdateUserDto, ) { - return this.userService.update(+id, userData); + return this.userService.update(+id, updateUserDto); } @Delete(':id') diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index f0fc280e..ac73d3be 100644 --- a/backend/src/user/user.service.ts +++ b/backend/src/user/user.service.ts @@ -1,4 +1,5 @@ import { Injectable } from '@nestjs/common'; +import { CreateUserDto } from './dto/create-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; import { PrismaService } from 'src/prisma/prisma.service'; import { User, Prisma } from '@prisma/client'; @@ -8,11 +9,11 @@ import bcrypt from 'bcrypt'; export class UserService { constructor(private prisma: PrismaService) {} - async create(data: Prisma.UserCreateInput): Promise { + async create(createUserDto: CreateUserDto): Promise { const saltRounds = 10; - const hashedPassword = await bcrypt.hash(data.password, saltRounds); - data.password = hashedPassword; - return this.prisma.user.create({ data }); + const hashedPassword = await bcrypt.hash(createUserDto.password, saltRounds); + createUserDto.password = hashedPassword; + return this.prisma.user.create({ data: createUserDto }); } findAll() { @@ -20,13 +21,13 @@ export class UserService { } findOne(id: number) { - return this.prisma.user.findFirst({ where: { id: id } }); + return this.prisma.user.findUnique({ where: { id: id } }); } - update(id: number, data: Prisma.UserUpdateInput) { + update(id: number, updateUserDto: UpdateUserDto) { return this.prisma.user.update({ - data, where: { id: id }, + data: updateUserDto, }); }