-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Controller and route for adding a project.
- Loading branch information
Showing
6 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/use-cases/addProjectUseCase.spec.ts → src/use-cases/createProjectUseCase.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.