From 707eb6a80ea686bb6902ce6a92467633e5008938 Mon Sep 17 00:00:00 2001 From: Matheus Sanchez Date: Wed, 31 Jan 2024 20:59:10 -0300 Subject: [PATCH] editProject route and tests e2e --- src/controller/project/editProject.spec.ts | 81 +++++++++++++++++++ src/controller/project/editProjectById.ts | 45 +++++++++++ src/controller/project/routes.ts | 3 + .../prisma/prisma-project-repository.ts | 9 +++ 4 files changed, 138 insertions(+) create mode 100644 src/controller/project/editProject.spec.ts create mode 100644 src/controller/project/editProjectById.ts diff --git a/src/controller/project/editProject.spec.ts b/src/controller/project/editProject.spec.ts new file mode 100644 index 0000000..4f5e491 --- /dev/null +++ b/src/controller/project/editProject.spec.ts @@ -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: 'john_doe@email.com', + 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 !', + }), + ) + }) +}) diff --git a/src/controller/project/editProjectById.ts b/src/controller/project/editProjectById.ts new file mode 100644 index 0000000..5244dd5 --- /dev/null +++ b/src/controller/project/editProjectById.ts @@ -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 !' }) + } + } +} diff --git a/src/controller/project/routes.ts b/src/controller/project/routes.ts index 6c0ea47..93cbd85 100644 --- a/src/controller/project/routes.ts +++ b/src/controller/project/routes.ts @@ -3,10 +3,13 @@ import { createProject } from './createProject' import { getProjectsByUserId } from './getProjectsByUserId' import { getProjectsById } from './getProjectById' import { getProjectsByTags } from './getProjectsByTags' +import { editProject } from './editProjectById' export async function projectRoutes(app: FastifyInstance) { app.post('/projects/tags', getProjectsByTags) app.get('/projects/:userId', getProjectsByUserId) app.get('/project/:projectId', getProjectsById) app.post('/user/:userId/project', createProject) + + app.put('/project/:projectId/edit', editProject) } diff --git a/src/repositories/prisma/prisma-project-repository.ts b/src/repositories/prisma/prisma-project-repository.ts index 5d49359..1965a4a 100644 --- a/src/repositories/prisma/prisma-project-repository.ts +++ b/src/repositories/prisma/prisma-project-repository.ts @@ -40,4 +40,13 @@ export class PrismaProjectRepository implements ProjectRepository { return project } + + async edit(data: Prisma.ProjectUncheckedCreateInput): Promise { + const project = await prisma.project.update({ + where: { id: data.id }, + data, + }) + + return project + } }