Skip to content

Commit

Permalink
use case correction and unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
luiseduardo3 committed Jan 29, 2024
1 parent 9b1bc00 commit 75d13f2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 69 deletions.
24 changes: 3 additions & 21 deletions src/repositories/in-memory-db/inMemoryProjectRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,18 @@ import { ProjectRepository } from '../prisma/project-repository'
export class InMemoryProjectRepository implements ProjectRepository {
public dbProject: Project[] = []

constructor() {
const project: Prisma.ProjectCreateWithoutUserInput = {
title: 'React',
description: 'Novo',
tags: 'nov, nn',
link: 'https:',
}

const userId = '1'

this.create(project, userId)
}

async findById(id: string) {
const project = this.dbProject.find((project) => project.id === id)

return !project ? null : project
}
constructor() {}

async create(
data: Prisma.ProjectCreateWithoutUserInput,
userId: string,
data: Prisma.ProjectCreateWithoutUserInput & { user_id: string },
): Promise<Project> {
const project: Project = {
id: data.id ?? randomUUID(),
title: data.title,
description: data.description,
tags: data.tags,
link: data.link,
user_id: userId,
user_id: data.user_id,
created_at: new Date(),
updated_at: new Date(),
}
Expand Down
6 changes: 1 addition & 5 deletions src/repositories/prisma/project-repository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Prisma, Project } from '@prisma/client'

export interface ProjectRepository {
create(
data: Prisma.ProjectCreateWithoutUserInput,
userId: string,
): Promise<Project>
findById(projectId: string): Promise<Project | null>
create(data: Prisma.ProjectUncheckedCreateInput): Promise<Project>
}
54 changes: 54 additions & 0 deletions src/use-cases/addProjectUseCase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect, describe, it, beforeEach } from 'vitest'

import { CreateProjectUseCase } from './addProjectUseCase'

import { InMemoryProjectRepository } from '../repositories/in-memory-db/inMemoryProjectRepository'
import { InMemoryUserRepository } from '../repositories/in-memory-db/inMemoryUserRepository'

let projectRepository: InMemoryProjectRepository
let userRepository: InMemoryUserRepository
let systemUnderTest: CreateProjectUseCase

describe('Create Project Use Case', () => {
beforeEach(() => {
projectRepository = new InMemoryProjectRepository()
userRepository = new InMemoryUserRepository()
systemUnderTest = new CreateProjectUseCase(
projectRepository,
userRepository,
)
})

it('should be able to create a new project', async () => {
const newUser = await userRepository.create({
name: 'Luis',
surname: 'Pereira',
email: '[email protected]',
password_hash: '123456',
})

const { project } = await systemUnderTest.execute({
title: 'React Typescript',
description: 'Melhor Projeto',
tags: 'React, Node',
link: 'https://github.com/luiseduardo3/nodets-petcanil',
userId: newUser.id,
})

expect(project.id).toEqual(expect.any(String))
})

it('should not create a project if user does not exist', async () => {
try {
await systemUnderTest.execute({
title: 'Project with nonexistent user',
description: 'Project without a valid user',
tags: 'Invalid, Project',
link: 'https://github.com/example/project-with-nonexistent-user',
userId: 'non-existent-UserId',
})
} catch (error) {
expect(error).toBeInstanceOf(Error)
}
})
})
30 changes: 20 additions & 10 deletions src/use-cases/addProject.ts → src/use-cases/addProjectUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Project } from '@prisma/client'

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

import { UserAlreadyExistsError } from './errors/user-already-exists-error'

interface CreateProjectUseCaseRequest {
title: string
Expand All @@ -15,7 +18,10 @@ interface CreateProjectUseCaseResponse {
}

export class CreateProjectUseCase {
constructor(private projectRepository: ProjectRepository) {}
constructor(
private projectRepository: ProjectRepository,
private userRepository: UserRepository,
) { }

async execute({
title,
Expand All @@ -24,15 +30,19 @@ export class CreateProjectUseCase {
link,
userId,
}: CreateProjectUseCaseRequest): Promise<CreateProjectUseCaseResponse> {
const project = await this.projectRepository.create(
{
title,
description,
tags,
link,
},
userId,
)
const user = await this.userRepository.findById(userId)

if (!user) {
throw new UserAlreadyExistsError()
}

const project = await this.projectRepository.create({
title,
description,
tags,
link,
user_id: userId,
})

return {
project,
Expand Down
33 changes: 0 additions & 33 deletions src/use-cases/addproject.spec.ts

This file was deleted.

0 comments on commit 75d13f2

Please sign in to comment.