-
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 #27 from MatheusSanchez/SFD-65-controller-get-proj…
…ects-from-user Controller get projects from user
- Loading branch information
Showing
6 changed files
with
132 additions
and
1 deletion.
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
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,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 !', | ||
}), | ||
) | ||
}) | ||
}) |
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,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 !' }) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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) | ||
} |
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