Skip to content

Commit

Permalink
Merge pull request #27 from MatheusSanchez/SFD-65-controller-get-proj…
Browse files Browse the repository at this point in the history
…ects-from-user

Controller get projects from user
  • Loading branch information
MatheusSanchez authored Jan 29, 2024
2 parents 6ec1426 + 9ee5809 commit 7b2070c
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { projectRoutes } from './controller/project/routes'

export const app = fastify()

app.addHook('preHandler', logMiddleware)
app.register(cors, {
origin: [env.FRONTEND_URL],
})
app.register(userRoutes)

app.register(projectRoutes)
app.addHook('preHandler', logMiddleware)

Expand Down
84 changes: 84 additions & 0 deletions src/controller/project/getProjectsByUserId.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import request from 'supertest'
import { app } from '../../app'
import { ProjectRepository } from '../../repositories/project-repository'
import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { UserRepository } from '../../repositories/user-repository'
import { randomUUID } from 'crypto'

let projectRepository: ProjectRepository
let userRepository: UserRepository

describe('Get Projets By UserId E2E', () => {
beforeAll(async () => {
projectRepository = new PrismaProjectRepository()
userRepository = new PrismaUsersRepository()

await app.ready()
})

afterAll(async () => {
await app.close()
})

it('should be able to get all projects from an user', async () => {
const description = 'ReactProject'
const link = 'www.google.com.br'
const tags = 'React'
const title = 'ReactProject'

const newUser = await userRepository.create({
email: '[email protected]',
name: 'John',
surname: 'Doe',
password_hash: 'password',
})

await projectRepository.create({
// First Project
description,
link,
tags,
title,
user_id: newUser.id,
})

await projectRepository.create({
// Second Project
description,
link,
tags,
title,
user_id: newUser.id,
})

const getProjectsByUserIdResponse = await request(app.server).get(
`/project/${newUser.id}`,
)

expect(getProjectsByUserIdResponse.statusCode).toEqual(200)
expect(getProjectsByUserIdResponse.body.projects).toHaveLength(2)
expect(getProjectsByUserIdResponse.body.projects[0]).toEqual(
expect.objectContaining({ title }),
)

expect(getProjectsByUserIdResponse.body.projects[1]).toEqual(
expect.objectContaining({ tags }),
)
})

it('should not be able to project that user does not exist', async () => {
const getProjectsByUserIdResponse = await request(app.server).get(
`/project/${randomUUID()}`,
)

expect(getProjectsByUserIdResponse.statusCode).toEqual(404)

expect(getProjectsByUserIdResponse.body).toEqual(
expect.objectContaining({
error: 'User was not Found !',
}),
)
})
})
33 changes: 33 additions & 0 deletions src/controller/project/getProjectsByUserId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { FastifyReply, FastifyRequest } from 'fastify'
import { z } from 'zod'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { GetProjectsByUserIdUseCase } from '../../use-cases/getProjectsByUserIdUseCase'
import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository'
import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError'

export async function getProjectsByUserId(
request: FastifyRequest,
response: FastifyReply,
) {
const userRepository = new PrismaUsersRepository()
const projectRepository = new PrismaProjectRepository()
const getProjectByUserId = new GetProjectsByUserIdUseCase(
projectRepository,
userRepository,
)

const GetProjectByUserIdParamsSchema = z.object({
userId: z.string().uuid(),
})

const { userId } = GetProjectByUserIdParamsSchema.parse(request.params)

try {
const { projects } = await getProjectByUserId.execute({ userId })
return response.status(200).send({ projects })
} catch (error) {
if (error instanceof ResourceNotFoundError) {
return response.status(404).send({ error: 'User was not Found !' })
}
}
}
2 changes: 2 additions & 0 deletions src/controller/project/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FastifyInstance } from 'fastify'
import { createProject } from './createProject'
import { getProjectsByUserId } from './getProjectsByUserId'

export async function projectRoutes(app: FastifyInstance) {
app.get('/project/:userId', getProjectsByUserId)
app.post('/user/:userId/project', createProject)
}
1 change: 0 additions & 1 deletion src/controller/user/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FastifyInstance } from 'fastify'
import { getUserById } from './getUserById'
import { getUserByEmail } from './getUserByEmail'
import { verifyJWT } from '../middlewares/verifyJwt'
import { registerUser } from './registerUser'

export async function userRoutes(app: FastifyInstance) {
Expand Down
11 changes: 11 additions & 0 deletions src/repositories/prisma/prisma-project-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@ export class PrismaProjectRepository implements ProjectRepository {

return project
}

async fetchProjectsByUserId(userId: string): Promise<Project[]> {
const projects = await prisma.project.findMany({
where: {
user_id: userId,
},
})

return projects
}

}

0 comments on commit 7b2070c

Please sign in to comment.