-
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 #38 from MatheusSanchez/SFD-85-edit-project
Edit Project Feature
- Loading branch information
Showing
8 changed files
with
254 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
import request from 'supertest' | ||
import { app } from '../../app' | ||
import { randomUUID } from 'crypto' | ||
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' | ||
import { UserRepository } from '../../repositories/user-repository' | ||
|
||
let userRepository: UserRepository | ||
|
||
describe('edit Project E2E', () => { | ||
beforeAll(async () => { | ||
userRepository = new PrismaUsersRepository() | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to edit a project', async () => { | ||
const createProjectBody = { | ||
title: 'Squad40 Project', | ||
tags: ['react', 'node'], | ||
link: 'https://Squad40.com', | ||
description: 'Squad40 description', | ||
} | ||
|
||
const newUser = await userRepository.create({ | ||
email: '[email protected]', | ||
name: 'John', | ||
surname: 'Doe', | ||
password_hash: 'password', | ||
}) | ||
|
||
const createProjectResponse = await request(app.server) | ||
.post(`/user/${newUser.id}/project`) | ||
.send(createProjectBody) | ||
|
||
const editProjectResponse = await request(app.server) | ||
.put(`/project/${createProjectResponse.body.project.id}/edit`) | ||
.send({ | ||
title: 'EditedTitle', | ||
tags: ['react', 'node', 'edit'], | ||
link: 'https://editedlin.com', | ||
description: 'EditedDescription', | ||
}) | ||
|
||
expect(createProjectResponse.statusCode).toEqual(201) | ||
|
||
expect(editProjectResponse.statusCode).toEqual(200) | ||
|
||
expect(editProjectResponse.body.project.title).toEqual('EditedTitle') | ||
expect(editProjectResponse.body.project.tags).toEqual([ | ||
'react', | ||
'node', | ||
'edit', | ||
]) | ||
expect(editProjectResponse.body.project.description).toEqual( | ||
'EditedDescription', | ||
) | ||
}) | ||
|
||
it('should not be able to edit a project that does not exist', async () => { | ||
const editProjectResponse = await request(app.server) | ||
.put(`/project/${randomUUID()}/edit`) | ||
.send({ | ||
title: 'EditedTitle', | ||
tags: ['react', 'node', 'edit'], | ||
link: 'https://editedlin.com', | ||
description: 'EditedDescription', | ||
}) | ||
|
||
expect(editProjectResponse.statusCode).toEqual(404) | ||
|
||
expect(editProjectResponse.body).toEqual( | ||
expect.objectContaining({ | ||
error: 'Project was not Found !', | ||
}), | ||
) | ||
}) | ||
}) |
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,45 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository' | ||
import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError' | ||
import { EditProjectUseCase } from '../../use-cases/project/editProjectUseCase' | ||
|
||
export async function editProject( | ||
request: FastifyRequest, | ||
response: FastifyReply, | ||
) { | ||
const editProjectBodySchema = z.object({ | ||
title: z.string(), | ||
tags: z.array(z.string()), | ||
link: z.string(), | ||
description: z.string(), | ||
}) | ||
|
||
const editProjectParamsSchema = z.object({ | ||
projectId: z.string().uuid(), | ||
}) | ||
|
||
const { title, tags, link, description } = editProjectBodySchema.parse( | ||
request.body, | ||
) | ||
const { projectId } = editProjectParamsSchema.parse(request.params) | ||
|
||
const projectRepository = new PrismaProjectRepository() | ||
const editProjectUseCase = new EditProjectUseCase(projectRepository) | ||
|
||
try { | ||
const { project } = await editProjectUseCase.execute({ | ||
projectId, | ||
title, | ||
tags, | ||
link, | ||
description, | ||
}) | ||
|
||
return response.status(200).send({ project }) | ||
} catch (error) { | ||
if (error instanceof ResourceNotFoundError) { | ||
return response.status(404).send({ error: 'Project was not Found !' }) | ||
} | ||
} | ||
} |
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
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,56 @@ | ||
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 { EditProjectUseCase } from './editProjectUseCase' | ||
|
||
let projectRepository: ProjectRepository | ||
let editProjectUseCase: EditProjectUseCase | ||
|
||
describe('Edit Project By Id Use Case', () => { | ||
beforeEach(() => { | ||
projectRepository = new InMemoryProjectRepository() | ||
editProjectUseCase = new EditProjectUseCase(projectRepository) | ||
}) | ||
|
||
it('should be able edit one project by ID', async () => { | ||
const projectWithoutEditing = await projectRepository.create({ | ||
title: 'React Typescript 1', | ||
description: 'Best Project', | ||
tags: ['react', 'node'], | ||
link: 'https://github.com/luiseduardo3/nodets-petcanil', | ||
user_id: 'user_id', | ||
}) | ||
|
||
const { project: projectEdited } = await editProjectUseCase.execute({ | ||
title: 'New Title', | ||
description: projectWithoutEditing.description, | ||
tags: projectWithoutEditing.tags, | ||
link: projectWithoutEditing.link, | ||
projectId: projectWithoutEditing.id, | ||
}) | ||
|
||
expect(projectEdited).toEqual( | ||
expect.objectContaining({ | ||
title: 'New Title', | ||
description: projectWithoutEditing.description, | ||
tags: projectWithoutEditing.tags, | ||
link: projectWithoutEditing.link, | ||
id: projectWithoutEditing.id, | ||
}), | ||
) | ||
}) | ||
|
||
it('should not be able to edit a project that does not exist', async () => { | ||
await expect(() => | ||
editProjectUseCase.execute({ | ||
title: 'title', | ||
description: 'description', | ||
tags: ['tags'], | ||
link: 'link', | ||
projectId: 'projectThatDoesNotExist', | ||
}), | ||
).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,49 @@ | ||
import { Project } from '@prisma/client' | ||
|
||
import { ProjectRepository } from '../../repositories/project-repository' | ||
|
||
import { ResourceNotFoundError } from '../errors/ResourceNotFoundError' | ||
|
||
interface EditProjectUseCaseRequest { | ||
title: string | ||
description: string | ||
tags: string[] | ||
link: string | ||
projectId: string | ||
} | ||
|
||
interface EditProjectUseCaseResponse { | ||
project: Project | ||
} | ||
|
||
export class EditProjectUseCase { | ||
constructor(private projectRepository: ProjectRepository) {} | ||
|
||
async execute({ | ||
title, | ||
description, | ||
link, | ||
tags, | ||
projectId, | ||
}: EditProjectUseCaseRequest): Promise<EditProjectUseCaseResponse> { | ||
const projectToBeUpdated = | ||
await this.projectRepository.fetchProjectById(projectId) | ||
|
||
if (!projectToBeUpdated) { | ||
throw new ResourceNotFoundError() | ||
} | ||
|
||
const project = await this.projectRepository.edit({ | ||
id: projectId, | ||
title, | ||
tags, | ||
link, | ||
description, | ||
user_id: projectToBeUpdated.user_id, | ||
}) | ||
|
||
return { | ||
project, | ||
} | ||
} | ||
} |