Skip to content

Commit

Permalink
[backend] Add swagger to user controller
Browse files Browse the repository at this point in the history
- Use DTOs in controller
  • Loading branch information
usatie committed Nov 4, 2023
1 parent 431807b commit 135f888
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
13 changes: 12 additions & 1 deletion backend/src/user/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 5 additions & 4 deletions backend/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -17,9 +18,9 @@ export class UserController {

@Post()
create(
@Body() userData: { name?: string; email: string; password: string },
@Body() createUserDto: CreateUserDto,
): Promise<UserModel> {
return this.userService.create(userData);
return this.userService.create(createUserDto);
}

@Get()
Expand All @@ -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')
Expand Down
15 changes: 8 additions & 7 deletions backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,25 +9,25 @@ import bcrypt from 'bcrypt';
export class UserService {
constructor(private prisma: PrismaService) {}

async create(data: Prisma.UserCreateInput): Promise<User> {
async create(createUserDto: CreateUserDto): Promise<User> {
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() {
return this.prisma.user.findMany();
}

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,
});
}

Expand Down

0 comments on commit 135f888

Please sign in to comment.