Skip to content

Commit

Permalink
Controller and route for adding a project.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxyuri13 committed Jan 29, 2024
1 parent 7eb8d19 commit 6c21fae
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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()

app.register(cors, {
origin: [env.FRONTEND_URL],
})
app.register(userRoutes)
app.register(projectRoutes)
app.addHook('preHandler', logMiddleware)

app.register(authRoutes)
Expand Down
51 changes: 51 additions & 0 deletions src/controller/project/createProject.ts
Original file line number Diff line number Diff line change
@@ -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()
}
6 changes: 6 additions & 0 deletions src/controller/project/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { FastifyInstance } from 'fastify'
import { createProject } from './createProject'

export async function projectRoutes(app: FastifyInstance) {
app.post('/user/:userId/project', createProject)
}
13 changes: 13 additions & 0 deletions src/repositories/prisma/prisma-project-repository.ts
Original file line number Diff line number Diff line change
@@ -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<Project> {
const project = await prisma.project.create({
data,
})

return project
}
}
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
File renamed without changes.

0 comments on commit 6c21fae

Please sign in to comment.