Skip to content

Commit

Permalink
Controller to get projexts by tags and tests e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Jan 31, 2024
1 parent a45db94 commit 3e9e8e4
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
100 changes: 100 additions & 0 deletions src/controller/project/getProjectsByTags.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import request from 'supertest'
import { app } from '../../app'
import { UserRepository } from '../../repositories/user-repository'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { User } from '@prisma/client'
let userRepository: UserRepository
let newUser: User

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

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

await app.ready()
})

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

it('should be able to get all projects that include some tag', async () => {
const tags = ['tag0', 'tag1']

const projectsToBeCreate = [
{
title: 'Project 01',
description: 'Project 01',
link: 'www.google.com.br',
tags: ['tag0', 'tag1'],
},
{
title: 'Project 02',
description: 'Project 02',
link: 'www.google.com.br',
tags: ['tag0', 'tag1'],
},

{
title: 'Project 03',
description: 'Project 03',
link: 'www.google.com.br',
tags: ['tag7', 'tag8', 'tag9'],
},
]

for (const project of projectsToBeCreate) {
await request(app.server)
.post(`/user/${newUser.id}/project`)
.send(project)
}

const getProjectsByTagsResponse = await request(app.server)
.post(`/projects/tags`)
.send({ tags })

expect(getProjectsByTagsResponse.statusCode).toEqual(200)
expect(getProjectsByTagsResponse.body.projects).toHaveLength(2)
expect(getProjectsByTagsResponse.body.projects[0]).toEqual(
expect.objectContaining({ title: 'Project 01' }),
)
expect(getProjectsByTagsResponse.body.projects[1]).toEqual(
expect.objectContaining({ title: 'Project 02' }),
)
})

it('should return 200 and empty object when not find projects by some tag', async () => {
const tags = ['tagNotExist', 'tagNotExist']

const getProjectsByTagsResponse = await request(app.server)
.post(`/projects/tags`)
.send({ tags })

expect(getProjectsByTagsResponse.statusCode).toEqual(200)
expect(getProjectsByTagsResponse.body.projects).toHaveLength(0)
})

it('should be able to get all projects NOT BEING case- sensitive', async () => {
const tags = ['tAG7', 'TAG8', 'Tag9']

// Projects with tags ['tag7', 'tag8', 'tag9'] are already registered
// once the database is set up once per file.

const getProjectsByTagsResponse = await request(app.server)
.post(`/projects/tags`)
.send({ tags })

expect(getProjectsByTagsResponse.statusCode).toEqual(200)
expect(getProjectsByTagsResponse.body.projects).toHaveLength(1)
expect(getProjectsByTagsResponse.body.projects[0]).toEqual(
expect.objectContaining({ title: 'Project 03' }),
)
})
})
24 changes: 24 additions & 0 deletions src/controller/project/getProjectsByTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { FastifyReply, FastifyRequest } from 'fastify'
import { z } from 'zod'
import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository'
import { GetProjectsByTagsUseCase } from '../../use-cases/getProjetsByTagsUseCase'

export async function getProjectsByTags(
request: FastifyRequest,
response: FastifyReply,
) {
const projectRepository = new PrismaProjectRepository()
const getProjectsByTagsUseCase = new GetProjectsByTagsUseCase(
projectRepository,
)

const GetProjectByTagsBodySchema = z.object({
tags: z.array(z.string()),
})
const { tags } = GetProjectByTagsBodySchema.parse(request.body)

const { projects } = await getProjectsByTagsUseCase.execute({
projectTags: tags,
})
return response.status(200).send({ projects })
}
2 changes: 2 additions & 0 deletions src/controller/project/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { FastifyInstance } from 'fastify'
import { createProject } from './createProject'
import { getProjectsByUserId } from './getProjectsByUserId'
import { getProjectsById } from './getProjectById'
import { getProjectsByTags } from './getProjectsByTags'

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)
Expand Down
8 changes: 7 additions & 1 deletion src/repositories/prisma/prisma-project-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export class PrismaProjectRepository implements ProjectRepository {
}

async fetchProjectByTags(tags: string[]): Promise<Project[]> {
throw new Error('Method not implemented.')
const project = await prisma.project.findMany({
where: {
tags: { hasEvery: tags },
},
})

return project
}
}

0 comments on commit 3e9e8e4

Please sign in to comment.