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/getProjectsByUserId.spec.ts b/src/controller/project/getProjectsByUserId.spec.ts index e84f230..b376e38 100644 --- a/src/controller/project/getProjectsByUserId.spec.ts +++ b/src/controller/project/getProjectsByUserId.spec.ts @@ -10,7 +10,7 @@ import { randomUUID } from 'crypto' let projectRepository: ProjectRepository let userRepository: UserRepository -describe.only('Get Projets By UserId E2E', () => { +describe('Get Projets By UserId E2E', () => { beforeAll(async () => { projectRepository = new PrismaProjectRepository() userRepository = new PrismaUsersRepository() diff --git a/src/controller/project/routes.ts b/src/controller/project/routes.ts index 96890ab..dfe3f93 100644 --- a/src/controller/project/routes.ts +++ b/src/controller/project/routes.ts @@ -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) } diff --git a/src/controller/session/authUser.ts b/src/controller/session/authUser.ts index c96f5fd..5dcbca3 100644 --- a/src/controller/session/authUser.ts +++ b/src/controller/session/authUser.ts @@ -2,33 +2,38 @@ import { FastifyReply, FastifyRequest } from 'fastify' import { AuthUserUseCase } from '../../use-cases/authUserUseCase' import { z } from 'zod' import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' +import { InvalidCredentialsError } from '../../use-cases/errors/InvalidCredentialsError' -export async function authUser( +export async function authUser( request: FastifyRequest, response: FastifyReply, ) { - const userRepository = new PrismaUsersRepository() const authUserUseCase = new AuthUserUseCase(userRepository) const AuthUserUseCaseSchema = z.object({ email: z.string().email(), - password: z.string() + password: z.string(), }) - const { email, password } = AuthUserUseCaseSchema.parse(request.body) + const { email, password } = AuthUserUseCaseSchema.parse(request.body) - const { user } = await authUserUseCase.execute({ email, password }) + try { + const { user } = await authUserUseCase.execute({ email, password }) - const token = await response.jwtSign( - {}, - { - sign: { - sub: user.id + const token = await response.jwtSign( + {}, + { + sign: { + sub: user.id, + }, }, - }, - ) - - - return response.status(200).send({ user, token }) -} \ No newline at end of file + ) + + return response.status(200).send({ user, token }) + } catch (e) { + if (e instanceof InvalidCredentialsError) { + return response.status(401).send() + } + } +} diff --git a/src/repositories/prisma/prisma-project-repository.ts b/src/repositories/prisma/prisma-project-repository.ts index db29887..10587b6 100644 --- a/src/repositories/prisma/prisma-project-repository.ts +++ b/src/repositories/prisma/prisma-project-repository.ts @@ -3,8 +3,12 @@ import { ProjectRepository } from '../project-repository' import { prisma } from '../../lib/prisma' export class PrismaProjectRepository implements ProjectRepository { - create(_: Prisma.ProjectUncheckedCreateInput): Promise { - throw new Error('Method not implemented.') + async create(data: Prisma.ProjectUncheckedCreateInput): Promise { + const project = await prisma.project.create({ + data, + }) + + return project } async fetchProjectsByUserId(userId: string): Promise { 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