Skip to content

Commit

Permalink
get project by id use case
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Jan 29, 2024
1 parent 08545b3 commit eee8c40
Show file tree
Hide file tree
Showing 5 changed files with 88 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 @@ -32,4 +32,12 @@ export class InMemoryProjectRepository implements ProjectRepository {

return projects
}

async fetchProjectById(projectId: string): Promise<Project | null> {
const project = this.dbProject.find((project) => project.id === projectId)
if (!project) {
return null
}
return project
}
}
3 changes: 3 additions & 0 deletions src/repositories/prisma/prisma-project-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ export class PrismaProjectRepository implements ProjectRepository {
return projects
}

async fetchProjectById(_projectId: string): Promise<Project | null> {
throw new Error('Method not implemented.')
}
}
1 change: 1 addition & 0 deletions src/repositories/project-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { Prisma, Project } from '@prisma/client'
export interface ProjectRepository {
create(data: Prisma.ProjectUncheckedCreateInput): Promise<Project>
fetchProjectsByUserId(userId: string): Promise<Project[]>
fetchProjectById(projectId: string): Promise<Project | null>
}
45 changes: 45 additions & 0 deletions src/use-cases/getProjectsByIdUseCase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect, describe, it, beforeEach } from 'vitest'

import { InMemoryProjectRepository } from '../repositories/in-memory-db/inMemoryProjectRepository'
import { ProjectRepository } from '../repositories/project-repository'
import { ResourceNotFoundError } from './errors/ResourceNotFoundError'
import { GetProjectsByIdUseCase } from './getProjectsByIdUseCase'

let projectRepository: ProjectRepository
let getProjectByIdUseCase: GetProjectsByIdUseCase

describe('Get Project By Id Use Case', () => {
beforeEach(() => {
projectRepository = new InMemoryProjectRepository()
getProjectByIdUseCase = new GetProjectsByIdUseCase(projectRepository)
})

it('should be able get project by ID', async () => {
const newProject = await projectRepository.create({
title: 'React Typescript 1',
description: 'Best Project',
tags: 'React',
link: 'https://github.com/luiseduardo3/nodets-petcanil',
user_id: 'user_id',
})

const { project } = await getProjectByIdUseCase.execute({
projectId: newProject.id,
})

expect(project).toEqual(
expect.objectContaining({
title: 'React Typescript 1',
id: newProject.id,
}),
)
})

it('should not be able to get a project that does not exist', async () => {
await expect(() =>
getProjectByIdUseCase.execute({
projectId: 'projectDoesNotExist',
}),
).rejects.toBeInstanceOf(ResourceNotFoundError)
})
})
31 changes: 31 additions & 0 deletions src/use-cases/getProjectsByIdUseCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Project } from '@prisma/client'

import { ProjectRepository } from '../repositories/project-repository'

import { ResourceNotFoundError } from './errors/ResourceNotFoundError'

interface GetProjectByIdRequest {
projectId: string
}

interface GetProjectByIdResponse {
project: Project
}

export class GetProjectsByIdUseCase {
constructor(private projectRepository: ProjectRepository) {}

async execute({
projectId,
}: GetProjectByIdRequest): Promise<GetProjectByIdResponse> {
const project = await this.projectRepository.fetchProjectById(projectId)

if (!project) {
throw new ResourceNotFoundError()
}

return {
project,
}
}
}

0 comments on commit eee8c40

Please sign in to comment.