-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from Rocketseat/chore/change-db-provider
chore: change db provider
- Loading branch information
Showing
41 changed files
with
610 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
# Database | ||
DATABASE_HOST=localhost | ||
DATABASE_PORT=5432 | ||
DATABASE_USER=docker | ||
DATABASE_PASS=docker | ||
DATABASE_PORT=3306 | ||
DATABASE_USER=root | ||
DATABASE_PASS=toor | ||
DATABASE_NAME=app | ||
|
||
DATABASE_URL="postgresql://${DATABASE_USER}:${DATABASE_PASS}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}" | ||
DATABASE_URL="mysql://${DATABASE_USER}:${DATABASE_PASS}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}" | ||
|
||
JWT_SECRET_KEY= | ||
JWT_PUBLIC_KEY= | ||
JWT_SECRET_KEY='JWT_SECRET_KEY' | ||
JWT_PUBLIC_KEY='JWT_PUBLIC_KEY' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Database | ||
DATABASE_HOST=localhost | ||
DATABASE_PORT=5432 | ||
DATABASE_USER=docker | ||
DATABASE_PASS=docker | ||
DATABASE_PORT=3306 | ||
DATABASE_USER=root | ||
DATABASE_PASS=toor | ||
DATABASE_NAME=app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
prisma/migrations/20230411210350_users_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- CreateTable | ||
CREATE TABLE `users` ( | ||
`id` VARCHAR(191) NOT NULL, | ||
`email` VARCHAR(191) NOT NULL, | ||
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), | ||
`updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), | ||
|
||
UNIQUE INDEX `users_email_key`(`email`), | ||
PRIMARY KEY (`id`) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "mysql" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { DatabaseModule } from '@infra/database/database.module'; | ||
|
||
import { GetUserByEmailUseCase } from './user/get-user-by-email.use-case'; | ||
|
||
@Module({ | ||
imports: [DatabaseModule], | ||
providers: [GetUserByEmailUseCase], | ||
exports: [GetUserByEmailUseCase], | ||
}) | ||
export class UseCasesModule {} |
8 changes: 8 additions & 0 deletions
8
src/application/use-cases/user/errors/user-by-email-not-found.error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { DomainError } from '@core/domain/errors/DomainError'; | ||
|
||
export class UserByEmailNotFoundError extends Error implements DomainError { | ||
constructor(email: string) { | ||
super(`User with email '${email}' was not found.`); | ||
this.name = 'UserNotFound'; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/application/use-cases/user/get-user-by-email.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { EmailBadFormattedError } from '@domain/value-objects/errors/email-bad-formatted-error'; | ||
|
||
import { UsersRepository } from '@infra/database/repositories/users.repository'; | ||
|
||
import { makeFakeUser } from '@test/factories/users.factory'; | ||
import { InMemoryUsersRepository } from '@test/repositories/in-memory-users.repository'; | ||
|
||
import { UserByEmailNotFoundError } from './errors/user-by-email-not-found.error'; | ||
import { GetUserByEmailUseCase } from './get-user-by-email.use-case'; | ||
|
||
describe('GetUserByEmailUseCase', () => { | ||
let usersRepository: UsersRepository; | ||
|
||
let getUserByEmailUseCase: GetUserByEmailUseCase; | ||
|
||
beforeEach(() => { | ||
usersRepository = new InMemoryUsersRepository(); | ||
|
||
getUserByEmailUseCase = new GetUserByEmailUseCase(usersRepository); | ||
}); | ||
|
||
it('should be able to get user by email', async () => { | ||
const user = makeFakeUser(); | ||
|
||
await usersRepository.create(user); | ||
|
||
const output = await getUserByEmailUseCase.handle(user.email); | ||
|
||
expect(output.isRight()).toBeTruthy(); | ||
expect(output.value).toEqual(user); | ||
}); | ||
|
||
it('should be able an error is returned in case an invalid email address is provided', async () => { | ||
const invalidEmail = 'invalid_email'; | ||
|
||
const output = await getUserByEmailUseCase.handle(invalidEmail); | ||
|
||
expect(output.isLeft()).toBeTruthy(); | ||
expect(output.value).toBeInstanceOf(EmailBadFormattedError); | ||
}); | ||
|
||
it('should be able to return user not found error', async () => { | ||
const email = '[email protected]'; | ||
|
||
const output = await getUserByEmailUseCase.handle(email); | ||
|
||
expect(output.isLeft()).toBeTruthy(); | ||
expect(output.value).toBeInstanceOf(UserByEmailNotFoundError); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
src/application/use-cases/user/get-user-by-email.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { Either, left, right } from '@core/logic/Either'; | ||
|
||
import { User } from '@domain/entities/user.entity'; | ||
import { Email } from '@domain/value-objects/email'; | ||
import { EmailBadFormattedError } from '@domain/value-objects/errors/email-bad-formatted-error'; | ||
|
||
import { UsersRepository } from '@infra/database/repositories/users.repository'; | ||
|
||
import { UserByEmailNotFoundError } from './errors/user-by-email-not-found.error'; | ||
|
||
type GetUserByEmailResponse = Either< | ||
EmailBadFormattedError | UserByEmailNotFoundError, | ||
User | ||
>; | ||
|
||
@Injectable() | ||
export class GetUserByEmailUseCase { | ||
constructor(private readonly usersRepository: UsersRepository) {} | ||
|
||
async handle(email: string): Promise<GetUserByEmailResponse> { | ||
const isInvalidEmail = !Email.validate(email); | ||
|
||
if (isInvalidEmail) { | ||
return left(new EmailBadFormattedError(email)); | ||
} | ||
|
||
const user = await this.usersRepository.findByEmail(email); | ||
|
||
if (!user) { | ||
return left(new UserByEmailNotFoundError(email)); | ||
} | ||
|
||
return right(user); | ||
} | ||
} |
Oops, something went wrong.