Skip to content

Commit

Permalink
Merge pull request #26 from MatheusSanchez/SFD-64-get-projects-by-use…
Browse files Browse the repository at this point in the history
…r-id

Get project by user id use case
  • Loading branch information
MatheusSanchez authored Jan 29, 2024
2 parents d55019f + 7b84aa1 commit 6ec1426
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/repositories/in-memory-db/inMemoryProjectRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ export class InMemoryProjectRepository implements ProjectRepository {

return project
}

async fetchProjectsByUserId(userId: string): Promise<Project[]> {
const projects = this.dbProject.filter(
(project) => project.user_id === userId,
)

return projects
}
}
1 change: 1 addition & 0 deletions src/repositories/project-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { Prisma, Project } from '@prisma/client'

export interface ProjectRepository {
create(data: Prisma.ProjectUncheckedCreateInput): Promise<Project>
fetchProjectsByUserId(userId: string): Promise<Project[]>
}
70 changes: 70 additions & 0 deletions src/use-cases/getProjectByUserIdUseCase.spec.ts
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)
})
})
37 changes: 37 additions & 0 deletions src/use-cases/getProjectsByUserIdUseCase.ts
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,
}
}
}

0 comments on commit 6ec1426

Please sign in to comment.