-
Notifications
You must be signed in to change notification settings - Fork 1
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 #26 from MatheusSanchez/SFD-64-get-projects-by-use…
…r-id Get project by user id use case
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { expect, describe, it, beforeEach } from 'vitest' | ||
|
||
import { InMemoryProjectRepository } from '../repositories/in-memory-db/inMemoryProjectRepository' | ||
import { InMemoryUserRepository } from '../repositories/in-memory-db/inMemoryUserRepository' | ||
import { GetProjectsByUserIdUseCase } from './getProjectsByUserIdUseCase' | ||
import { ProjectRepository } from '../repositories/project-repository' | ||
import { UserRepository } from '../repositories/user-repository' | ||
import { ResourceNotFoundError } from './errors/ResourceNotFoundError' | ||
|
||
let projectRepository: ProjectRepository | ||
let userRepository: UserRepository | ||
let getProjectByUserIdUseCase: GetProjectsByUserIdUseCase | ||
|
||
describe('Get Project By User Id Use Case', () => { | ||
beforeEach(() => { | ||
projectRepository = new InMemoryProjectRepository() | ||
|
||
userRepository = new InMemoryUserRepository() | ||
getProjectByUserIdUseCase = new GetProjectsByUserIdUseCase( | ||
projectRepository, | ||
userRepository, | ||
) | ||
}) | ||
|
||
it('should be able get projects from an user', async () => { | ||
const newUser = await userRepository.create({ | ||
name: 'Matheus', | ||
surname: 'Sanchez', | ||
email: '[email protected]', | ||
password_hash: '123456', | ||
}) | ||
|
||
await projectRepository.create({ | ||
title: 'React Typescript 1', | ||
description: 'Best Project', | ||
tags: 'React', | ||
link: 'https://github.com/luiseduardo3/nodets-petcanil', | ||
user_id: newUser.id, | ||
}) | ||
|
||
await projectRepository.create({ | ||
title: 'React Typescript 2', | ||
description: 'Best Project 2', | ||
tags: 'React', | ||
link: 'https://github.com/luiseduardo3/nodets-petcanil', | ||
user_id: newUser.id, | ||
}) | ||
|
||
const { projects } = await getProjectByUserIdUseCase.execute({ | ||
userId: newUser.id, | ||
}) | ||
|
||
expect(projects).toHaveLength(2) | ||
expect(projects[0]).toEqual( | ||
expect.objectContaining({ title: 'React Typescript 1' }), | ||
) | ||
|
||
expect(projects[1]).toEqual( | ||
expect.objectContaining({ title: 'React Typescript 2' }), | ||
) | ||
}) | ||
|
||
it('should not be able to create a project if the user was not found.', async () => { | ||
await expect(() => | ||
getProjectByUserIdUseCase.execute({ | ||
userId: 'userNotExist', | ||
}), | ||
).rejects.toBeInstanceOf(ResourceNotFoundError) | ||
}) | ||
}) |
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 { Project } from '@prisma/client' | ||
|
||
import { ProjectRepository } from '../repositories/project-repository' | ||
import { UserRepository } from '../repositories/user-repository' | ||
|
||
import { ResourceNotFoundError } from './errors/ResourceNotFoundError' | ||
|
||
interface FetchProjectsByUserIdUseCaseRequest { | ||
userId: string | ||
} | ||
|
||
interface FetchProjectsByUserIdUseCaseResponse { | ||
projects: Project[] | ||
} | ||
|
||
export class GetProjectsByUserIdUseCase { | ||
constructor( | ||
private projectRepository: ProjectRepository, | ||
private userRepository: UserRepository, | ||
) {} | ||
|
||
async execute({ | ||
userId, | ||
}: FetchProjectsByUserIdUseCaseRequest): Promise<FetchProjectsByUserIdUseCaseResponse> { | ||
const user = await this.userRepository.findById(userId) | ||
|
||
if (!user) { | ||
throw new ResourceNotFoundError() | ||
} | ||
|
||
const projects = await this.projectRepository.fetchProjectsByUserId(userId) | ||
|
||
return { | ||
projects, | ||
} | ||
} | ||
} |