Skip to content

Commit

Permalink
Fix create project use case to accept tags as an string array
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Jan 31, 2024
1 parent 468c0b1 commit e4d33e9
Show file tree
Hide file tree
Showing 15 changed files with 22 additions and 225 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Warnings:
- The `tags` column on the `Project` table would be dropped and recreated. This will lead to data loss if there is data in the column.
*/
-- AlterTable
ALTER TABLE "Project" DROP COLUMN "tags",
ADD COLUMN "tags" TEXT[];
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ model User {
model Project {
id String @id @default(uuid())
title String
tags String
tags String[]
link String
description String
created_at DateTime @default(now())
Expand Down
2 changes: 1 addition & 1 deletion src/controller/project/getProjectById.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Get Projets By ID E2E', () => {
it('should be able to get a project by ID', async () => {
const description = 'ReactProject'
const link = 'www.google.com.br'
const tags = 'React'
const tags = ['react', 'node']
const title = 'ReactProject'

const newUser = await userRepository.create({
Expand Down
69 changes: 0 additions & 69 deletions src/controller/project/getProjectByTags.spec.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/controller/project/getProjectByTags.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/controller/project/getProjectsByUserId.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Get Projets By UserId E2E', () => {
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 tags = ['react', 'node']
const title = 'ReactProject'

const newUser = await userRepository.create({
Expand Down
2 changes: 0 additions & 2 deletions src/controller/project/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { FastifyInstance } from 'fastify'
import { createProject } from './createProject'
import { getProjectsByUserId } from './getProjectsByUserId'
import { getProjectsById } from './getProjectById'
import { getProjectsByTags } from './getProjectByTags'

export async function projectRoutes(app: FastifyInstance) {
app.get('/projects/:userId', getProjectsByUserId)
app.get('/projects/tags/:projectTags', getProjectsByTags)
app.get('/project/:projectId', getProjectsById)
app.post('/user/:userId/project', createProject)
}
12 changes: 1 addition & 11 deletions src/repositories/in-memory-db/inMemoryProjectRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class InMemoryProjectRepository implements ProjectRepository {
id: data.id ?? randomUUID(),
title: data.title,
description: data.description,
tags: data.tags,
tags: data.tags as string[],
link: data.link,
user_id: data.user_id,
created_at: new Date(),
Expand All @@ -40,14 +40,4 @@ export class InMemoryProjectRepository implements ProjectRepository {
}
return project
}

async fetchProjectByTags(projectTags: string): Promise<Project[]> {
const lowercasedTags = projectTags.toLowerCase();

const projects = this.dbProject.filter(
(project) => project.tags.toLowerCase() === lowercasedTags
);

return projects;
}
}
1 change: 0 additions & 1 deletion src/repositories/project-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export interface ProjectRepository {
create(data: Prisma.ProjectUncheckedCreateInput): Promise<Project>
fetchProjectsByUserId(userId: string): Promise<Project[]>
fetchProjectById(projectId: string): Promise<Project | null>
fetchProjectByTags(projectTags: string): Promise<Project[]>
}
5 changes: 3 additions & 2 deletions src/use-cases/createProjectUseCase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ describe('Create Project Use Case', () => {
const { project } = await createProjectUseCase.execute({
title: 'React Typescript',
description: 'Melhor Projeto',
tags: 'React, Node',
tags: ['react', 'node'],
link: 'https://github.com/luiseduardo3/nodets-petcanil',
userId: newUser.id,
})

expect(project.id).toEqual(expect.any(String))
expect(project.title).toEqual('React Typescript')
expect(project.tags).toEqual(['react', 'node'])
})

it('should not be able to create a project if the user was not found.', async () => {
await expect(() =>
createProjectUseCase.execute({
title: 'Project with nonexistent user',
description: 'Project without a valid user',
tags: 'Invalid, Project',
tags: ['react', 'node'],
link: 'https://github.com/example/project-with-nonexistent-user',
userId: 'non-existent-UserId',
}),
Expand Down
4 changes: 2 additions & 2 deletions src/use-cases/createProjectUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ResourceNotFoundError } from './errors/ResourceNotFoundError'
interface CreateProjectUseCaseRequest {
title: string
description: string
tags: string
tags: string[]
link: string
userId: string
}
Expand Down Expand Up @@ -36,7 +36,7 @@ export class CreateProjectUseCase {
throw new ResourceNotFoundError()
}

const lowercaseTags = tags.toLowerCase();
const lowercaseTags = tags.map((tag) => tag.toLowerCase())

const project = await this.projectRepository.create({
title,
Expand Down
3 changes: 2 additions & 1 deletion src/use-cases/getProjectsByIdUseCase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Get Project By Id Use Case', () => {
const newProject = await projectRepository.create({
title: 'React Typescript 1',
description: 'Best Project',
tags: 'React',
tags: ['react', 'node'],
link: 'https://github.com/luiseduardo3/nodets-petcanil',
user_id: 'user_id',
})
Expand All @@ -31,6 +31,7 @@ describe('Get Project By Id Use Case', () => {
expect.objectContaining({
title: 'React Typescript 1',
id: newProject.id,
tags: ['react', 'node'],
}),
)
})
Expand Down
72 changes: 0 additions & 72 deletions src/use-cases/getProjectsByTagsUseCase.spec.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/use-cases/getProjectsByTagsUseCase.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/use-cases/getProjectsByUserIdUseCase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ describe('Get Project By User Id Use Case', () => {
await projectRepository.create({
title: 'React Typescript 1',
description: 'Best Project',
tags: 'React',
tags: ['react', 'node'],
link: 'https://github.com/luiseduardo3/nodets-petcanil',
user_id: newUser.id,
})

await projectRepository.create({
title: 'React Typescript 2',
description: 'Best Project 2',
tags: 'React',
tags: ['react', 'node'],
link: 'https://github.com/luiseduardo3/nodets-petcanil',
user_id: newUser.id,
})
Expand Down

0 comments on commit e4d33e9

Please sign in to comment.