-
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.
- Loading branch information
Showing
10 changed files
with
232 additions
and
2 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
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,69 @@ | ||
import { ProjectRepository } from "../../repositories/project-repository"; | ||
import { UserRepository } from "../../repositories/user-repository"; | ||
import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
import request from 'supertest' | ||
|
||
import { PrismaProjectRepository } from "../../repositories/prisma/prisma-project-repository"; | ||
import { PrismaUsersRepository } from "../../repositories/prisma/prisma-users-repository"; | ||
import { app } from "../../app"; | ||
|
||
let projectRepository: ProjectRepository | ||
let userRepository: UserRepository | ||
|
||
describe('Get Projects By Tags E2E', () => { | ||
beforeAll(async () => { | ||
projectRepository = new PrismaProjectRepository() | ||
userRepository = new PrismaUsersRepository() | ||
|
||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to get a project by Tags', async () => { | ||
const newUser = await userRepository.create({ | ||
email: '[email protected]', | ||
name: 'John', | ||
surname: 'Doe', | ||
password_hash: 'password', | ||
}) | ||
|
||
await projectRepository.create({ | ||
title: 'Projeto 1', | ||
tags: 'react', | ||
description: 'Descrição 1', | ||
link: 'https://github.com/pedrodecf', | ||
user_id: newUser.id, | ||
}) | ||
|
||
await projectRepository.create({ | ||
title: 'Projeto 2', | ||
tags: 'react', | ||
description: 'Descrição 2', | ||
link: 'https://www.linkedin.com/in/pedrodecf/', | ||
user_id: newUser.id, | ||
}) | ||
|
||
await projectRepository.create({ | ||
title: 'Projeto 3', | ||
tags: 'Node', | ||
description: 'Descrição 3', | ||
link: 'https://www.acabouminhasredesociais.com.br', | ||
user_id: newUser.id, | ||
}) | ||
|
||
const getProjectByTagsResponse = await request(app.server).get( | ||
`/projects/tags/react`, | ||
) | ||
|
||
expect(getProjectByTagsResponse.statusCode).toEqual(200) | ||
|
||
expect(getProjectByTagsResponse.body.projects).toHaveLength(2); | ||
|
||
expect(getProjectByTagsResponse.body.projects[0]).toEqual( | ||
expect.objectContaining({ tags: 'react' }), | ||
) | ||
}) | ||
}) |
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,29 @@ | ||
import { FastifyReply, FastifyRequest } from "fastify"; | ||
import { PrismaProjectRepository } from "../../repositories/prisma/prisma-project-repository"; | ||
import { GetProjectsByTagsUseCase } from "../../use-cases/getProjectsByTagsUseCase"; | ||
import { z } from "zod"; | ||
import { ResourceNotFoundError } from "../../use-cases/errors/ResourceNotFoundError"; | ||
|
||
|
||
export async function getProjectsByTags( | ||
request: FastifyRequest, | ||
response: FastifyReply | ||
) { | ||
const projectRepository = new PrismaProjectRepository() | ||
const getProjectsByTagsUseCase = new GetProjectsByTagsUseCase(projectRepository) | ||
|
||
const GetProjectsByTagsParamsSchema = z.object({ | ||
projectTags: z.string() | ||
}) | ||
|
||
const { projectTags } = GetProjectsByTagsParamsSchema.parse(request.params) | ||
|
||
try { | ||
const { projects } = await getProjectsByTagsUseCase.execute({ projectTags }) | ||
return response.status(200).send({ projects }) | ||
} 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
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,72 @@ | ||
import { beforeEach, describe, expect, it } from "vitest"; | ||
import { ProjectRepository } from "../repositories/project-repository"; | ||
import { GetProjectsByTagsUseCase } from "./getProjectsByTagsUseCase"; | ||
import { InMemoryProjectRepository } from "../repositories/in-memory-db/inMemoryProjectRepository"; | ||
import { ResourceNotFoundError } from "./errors/ResourceNotFoundError"; | ||
|
||
|
||
let projectRepository: ProjectRepository | ||
let getProjectByTagsUseCase: GetProjectsByTagsUseCase | ||
|
||
describe('Get Projects By Tags Use Case', () => { | ||
beforeEach(() => { | ||
projectRepository = new InMemoryProjectRepository() | ||
getProjectByTagsUseCase = new GetProjectsByTagsUseCase(projectRepository) | ||
}) | ||
|
||
it('should be able get project by Tags', async () => { | ||
await projectRepository.create({ | ||
title: 'React Typescript 1', | ||
description: 'Best Project', | ||
tags: 'react', | ||
link: 'https://github.com/luiseduardo3/nodets-petcanil', | ||
user_id: 'user_id', | ||
}) | ||
|
||
await projectRepository.create({ | ||
title: 'React Typescript 2', | ||
description: 'Best Project 2', | ||
tags: 'react', | ||
link: 'https://github.com/luiseduardo3/nodets-petcanil', | ||
user_id: 'user_id', | ||
}) | ||
|
||
await projectRepository.create({ | ||
title: 'Node with Typescript', | ||
description: 'Best Node Project', | ||
tags: 'Node', | ||
link: 'https://github.com/luiseduardo3/nodets-petcanil', | ||
user_id: 'user_id', | ||
}) | ||
|
||
const { projects } = await getProjectByTagsUseCase.execute({ | ||
projectTags: 'React' | ||
}) | ||
|
||
expect(projects).toHaveLength(2) | ||
|
||
expect(projects[0]).toEqual( | ||
expect.objectContaining({ tags: 'react' }), | ||
) | ||
|
||
expect(projects[1]).toEqual( | ||
expect.objectContaining({ tags: 'react' }), | ||
) | ||
}) | ||
|
||
it('should not be able to get a project that dont have the searched tags' , async () => { | ||
await projectRepository.create({ | ||
title: 'Node with Typescript', | ||
description: 'Best Node Project', | ||
tags: 'Node', | ||
link: 'https://github.com/luiseduardo3/nodets-petcanil', | ||
user_id: 'user_id', | ||
}) | ||
|
||
const { projects } = await getProjectByTagsUseCase.execute({ | ||
projectTags: 'React' | ||
}) | ||
|
||
expect(projects).toEqual([]) | ||
}) | ||
}) |
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,31 @@ | ||
import { Project } from '@prisma/client' | ||
|
||
import { ProjectRepository } from '../repositories/project-repository' | ||
|
||
import { ResourceNotFoundError } from './errors/ResourceNotFoundError' | ||
|
||
interface GetProjectByTagsRequest { | ||
projectTags: string | ||
} | ||
|
||
interface GetProjectByTagsResponse { | ||
projects: Project[] | ||
} | ||
|
||
export class GetProjectsByTagsUseCase { | ||
constructor(private projectRepository: ProjectRepository) {} | ||
|
||
async execute({ | ||
projectTags, | ||
}: GetProjectByTagsRequest): Promise<GetProjectByTagsResponse> { | ||
const projects = await this.projectRepository.fetchProjectByTags(projectTags) | ||
|
||
if (!projects) { | ||
throw new ResourceNotFoundError() | ||
} | ||
|
||
return { | ||
projects | ||
} | ||
} | ||
} |