Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update UpdateUserDto, its route and userService for Prisma template #246

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions lib/prisma/src/dtos/users.dto.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { IsEmail, IsString, IsNotEmpty, MinLength, MaxLength } from 'class-validator';

export class CreateUserDto {
@IsEmail()
public email: string;
import { IsEmail, IsString, IsNotEmpty, Length } from 'class-validator';

class BaseUserDto {
@IsString()
@IsNotEmpty()
@MinLength(9)
@MaxLength(32)
@Length(9,32)
public password: string;
}

export class UpdateUserDto {
@IsString()
@IsNotEmpty()
@MinLength(9)
@MaxLength(32)
public password: string;
export class CreateUserDto extends BaseUserDto {
@IsEmail()
public email: string;
}

export class UpdateUserDto extends BaseUserDto {}
4 changes: 2 additions & 2 deletions lib/prisma/src/routes/users.route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router } from 'express';
import { UserController } from '@controllers/users.controller';
import { CreateUserDto } from '@dtos/users.dto';
import { CreateUserDto, UpdateUserDto } from '@dtos/users.dto';
import { Routes } from '@interfaces/routes.interface';
import { ValidationMiddleware } from '@middlewares/validation.middleware';

Expand All @@ -17,7 +17,7 @@ export class UserRoute implements Routes {
this.router.get(`${this.path}`, this.user.getUsers);
this.router.get(`${this.path}/:id(\\d+)`, this.user.getUserById);
this.router.post(`${this.path}`, ValidationMiddleware(CreateUserDto), this.user.createUser);
this.router.put(`${this.path}/:id(\\d+)`, ValidationMiddleware(CreateUserDto, true), this.user.updateUser);
this.router.put(`${this.path}/:id(\\d+)`, ValidationMiddleware(UpdateUserDto, true), this.user.updateUser);
this.router.delete(`${this.path}/:id(\\d+)`, this.user.deleteUser);
}
}
4 changes: 2 additions & 2 deletions lib/prisma/src/services/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client';
import { hash } from 'bcrypt';
import { Service } from 'typedi';
import { CreateUserDto } from '@dtos/users.dto';
import { CreateUserDto, UpdateUserDto } from '@dtos/users.dto';
import { HttpException } from '@/exceptions/httpException';
import { User } from '@interfaces/users.interface';

Expand Down Expand Up @@ -30,7 +30,7 @@ export class UserService {
return createUserData;
}

public async updateUser(userId: number, userData: CreateUserDto): Promise<User> {
public async updateUser(userId: number, userData: UpdateUserDto): Promise<User> {
const findUser: User = await this.user.findUnique({ where: { id: userId } });
if (!findUser) throw new HttpException(409, "User doesn't exist");

Expand Down