Skip to content

Commit

Permalink
feat(users): add create method and findbyEmail.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipeex committed Jan 16, 2023
1 parent 4ca0932 commit 994dd3d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/app/repositories/users-repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { User } from '../entities/user';
import type { User as UserPrisma } from '@prisma/client';

export abstract class UserRepository {
abstract findById(userId: string): Promise<User>;
abstract findAll(): Promise<User[]>;
abstract findById(userId: string): Promise<User>;
abstract findByEmail(userEmail: string): Promise<User>;
abstract delete(userId: string): Promise<User>;
abstract create(user: User): Promise<UserPrisma>;
}
22 changes: 20 additions & 2 deletions src/app/use-cases/create-user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import { User } from '@app/entities/user';
import { Injectable } from '@nestjs/common';
import { UserRepository } from '../repositories/users-repository';
import type { User as UserPrisma } from '@prisma/client';
import { Error } from 'src/utils/error.filter';
import { HttpStatus } from '@nestjs/common/enums';

interface CreateUserRequest {}
interface CreateUserResponse {}
type CreateUserRequest = User;
type CreateUserResponse = UserPrisma;

@Injectable()
export class CreateUser {
constructor(private userRepository: UserRepository) {}

async execute(request: CreateUserRequest): Promise<CreateUserResponse> {
const { email } = request;
const existUser = await this.userRepository.findByEmail(email);

if (existUser) {
throw new Error({
message: ['Email exist'],
statusCode: HttpStatus.CONFLICT,
});
}

return await this.userRepository.create(request);
}
}
20 changes: 20 additions & 0 deletions src/infra/database/prisma/repositories/prisma-user-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { User } from 'src/app/entities/user';
import { UserRepository } from 'src/app/repositories/users-repository';
import { PrismaUserMapper } from '../mappers/prisma-user-mappers';
import { PrismaService } from '../prisma.service';
import type { User as UserPrisma } from '@prisma/client';

@Injectable()
export class PrismaUserRepository implements UserRepository {
Expand Down Expand Up @@ -35,4 +36,23 @@ export class PrismaUserRepository implements UserRepository {

return PrismaUserMapper.toDomain(deleted);
}

async create(user: User): Promise<UserPrisma> {
const raw = PrismaUserMapper.toPrisma(user);

return await this.prisma.user.create({
data: raw,
});
}

async findByEmail(email: string): Promise<User> {
const user = await this.prisma.user.findUnique({
where: {
email,
},
});

if (!user) return null;
return PrismaUserMapper.toDomain(user);
}
}
15 changes: 10 additions & 5 deletions src/infra/http/controllers/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@ import {
Param,
Body,
} from '@nestjs/common';
import { FindUserById } from 'src/app/use-cases/find-user';
import deleteUser from '../dtos/delete-user';
import { FindUserById } from 'src/app/use-cases/find-user';
import { UserViewModel } from '../view-models/user-view-model';
import createUser from '../dtos/create-user';
import { CreateUser as CreateUserU } from '@app/use-cases/create-user';
import { User } from '@app/entities/user';

@Controller()
export class UsersController {
constructor(
private findUserById: FindUserById,
private findAllUsers: FindAllUsers,
private deleteUserById: DeleteUserById,
private createUser: CreateUserU,
) {}

/* @Post('user')
create(@Body() data: createUser) {
return this.usersService.create(data);
} */
@Post('user')
async create(@Body() data: createUser) {
const user = new User(data);
return await this.createUser.execute(user);
}

@Get('users')
async findAll() {
Expand Down
5 changes: 4 additions & 1 deletion src/infra/http/dtos/create-user.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { IsNotEmpty, IsEmail } from 'class-validator';
import { IsNotEmpty, IsEmail, Min } from 'class-validator';

export default class createUser {
id: string;

@IsNotEmpty({ message: 'name is empty' })
name: string;

@IsNotEmpty({ message: 'email is empty' })
@IsEmail()
@Min(6, { message: 'password is not 6 characters' })
email: string;

@IsNotEmpty({ message: 'password is empty' })
Expand Down
3 changes: 2 additions & 1 deletion src/infra/http/http.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CreateUser } from '@app/use-cases/create-user';
import { DeleteUserById } from '@app/use-cases/delete-user';
import { FindAllUsers } from '@app/use-cases/find-users';
import { DatabaseModule } from '@infra/database/database.module';
Expand All @@ -8,6 +9,6 @@ import { UsersController } from './controllers/users.controller';
@Module({
imports: [DatabaseModule],
controllers: [UsersController],
providers: [FindUserById, FindAllUsers, DeleteUserById],
providers: [FindUserById, FindAllUsers, DeleteUserById, CreateUser],
})
export class HttpModule {}

0 comments on commit 994dd3d

Please sign in to comment.