-
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.
Merge branch 'master' into SFD-65-controller-get-projects-from-user
- Loading branch information
Showing
6 changed files
with
81 additions
and
19 deletions.
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
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 |
---|---|---|
@@ -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) | ||
} |
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
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.