From 6c21fae039ff28846f9a7dcdf290cefb134ea642 Mon Sep 17 00:00:00 2001 From: max Date: Mon, 29 Jan 2024 15:48:35 -0300 Subject: [PATCH] Controller and route for adding a project. --- src/app.ts | 2 + src/controller/project/createProject.ts | 51 +++++++++++++++++++ src/controller/project/routes.ts | 6 +++ .../prisma/prisma-project-repository.ts | 13 +++++ ...e.spec.ts => createProjectUseCase.spec.ts} | 2 +- ...jectUseCase.ts => createProjectUseCase.ts} | 0 6 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/controller/project/createProject.ts create mode 100644 src/controller/project/routes.ts create mode 100644 src/repositories/prisma/prisma-project-repository.ts rename src/use-cases/{addProjectUseCase.spec.ts => createProjectUseCase.spec.ts} (96%) rename src/use-cases/{addProjectUseCase.ts => createProjectUseCase.ts} (100%) diff --git a/src/app.ts b/src/app.ts index 2256c6b..f3a6990 100644 --- a/src/app.ts +++ b/src/app.ts @@ -6,6 +6,7 @@ import { authRoutes } from './controller/session/routes' import fastifyJwt from '@fastify/jwt' import cors from '@fastify/cors' import { logMiddleware } from './controller/middlewares/logMiddleware' +import { projectRoutes } from './controller/project/routes' export const app = fastify() @@ -13,6 +14,7 @@ app.register(cors, { origin: [env.FRONTEND_URL], }) app.register(userRoutes) +app.register(projectRoutes) app.addHook('preHandler', logMiddleware) app.register(authRoutes) diff --git a/src/controller/project/createProject.ts b/src/controller/project/createProject.ts new file mode 100644 index 0000000..7c0d885 --- /dev/null +++ b/src/controller/project/createProject.ts @@ -0,0 +1,51 @@ +import { FastifyReply, FastifyRequest } from 'fastify' +import { z } from 'zod' +import { CreateProjectUseCase } from '../../use-cases/createProjectUseCase' +import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository' +import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' +import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError' + +export async function createProject( + request: FastifyRequest, + response: FastifyReply, +) { + const createProjectBodySchema = z.object({ + title: z.string(), + tags: z.string(), + link: z.string(), + description: z.string(), + }) + + const createProjectParamsSchema = z.object({ + userId: z.string().uuid(), + }) + + const { title, tags, link, description } = createProjectBodySchema.parse( + request.body, + ) + const { userId } = createProjectParamsSchema.parse(request.params) + + const userRepository = new PrismaUsersRepository() + const projectRepository = new PrismaProjectRepository() + const createProjectUseCase = new CreateProjectUseCase( + projectRepository, + userRepository, + ) + try { + await createProjectUseCase.execute({ + userId, + title, + tags, + link, + description, + }) + } catch (error) { + if (error instanceof ResourceNotFoundError) { + return response.status(404).send({ message: error.message }) + } + + throw error + } + + return response.status(201).send() +} diff --git a/src/controller/project/routes.ts b/src/controller/project/routes.ts new file mode 100644 index 0000000..5f16a56 --- /dev/null +++ b/src/controller/project/routes.ts @@ -0,0 +1,6 @@ +import { FastifyInstance } from 'fastify' +import { createProject } from './createProject' + +export async function projectRoutes(app: FastifyInstance) { + app.post('/user/:userId/project', createProject) +} diff --git a/src/repositories/prisma/prisma-project-repository.ts b/src/repositories/prisma/prisma-project-repository.ts new file mode 100644 index 0000000..e3b737c --- /dev/null +++ b/src/repositories/prisma/prisma-project-repository.ts @@ -0,0 +1,13 @@ +import { Prisma, Project } from '@prisma/client' +import { ProjectRepository } from '../project-repository' +import { prisma } from '../../lib/prisma' + +export class PrismaProjectRepository implements ProjectRepository { + async create(data: Prisma.ProjectUncheckedCreateInput): Promise { + const project = await prisma.project.create({ + data, + }) + + return project + } +} diff --git a/src/use-cases/addProjectUseCase.spec.ts b/src/use-cases/createProjectUseCase.spec.ts similarity index 96% rename from src/use-cases/addProjectUseCase.spec.ts rename to src/use-cases/createProjectUseCase.spec.ts index d66942d..d87706d 100644 --- a/src/use-cases/addProjectUseCase.spec.ts +++ b/src/use-cases/createProjectUseCase.spec.ts @@ -1,6 +1,6 @@ import { expect, describe, it, beforeEach } from 'vitest' -import { CreateProjectUseCase } from './addProjectUseCase' +import { CreateProjectUseCase } from './createProjectUseCase' import { InMemoryProjectRepository } from '../repositories/in-memory-db/inMemoryProjectRepository' import { InMemoryUserRepository } from '../repositories/in-memory-db/inMemoryUserRepository' diff --git a/src/use-cases/addProjectUseCase.ts b/src/use-cases/createProjectUseCase.ts similarity index 100% rename from src/use-cases/addProjectUseCase.ts rename to src/use-cases/createProjectUseCase.ts