Skip to content

Commit

Permalink
editProject route and tests e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Jan 31, 2024
1 parent 4e6aa52 commit 707eb6a
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/controller/project/editProject.spec.ts
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 !',
}),
)
})
})
45 changes: 45 additions & 0 deletions src/controller/project/editProjectById.ts
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 !' })
}
}
}
3 changes: 3 additions & 0 deletions src/controller/project/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 9 additions & 0 deletions src/repositories/prisma/prisma-project-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ export class PrismaProjectRepository implements ProjectRepository {

return project
}

async edit(data: Prisma.ProjectUncheckedCreateInput): Promise<Project> {
const project = await prisma.project.update({
where: { id: data.id },
data,
})

return project
}
}

0 comments on commit 707eb6a

Please sign in to comment.