-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(Tag, Route): Register tag route
- Loading branch information
1 parent
ccd5a01
commit 3e948f5
Showing
3 changed files
with
44 additions
and
0 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
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,34 @@ | ||
import { RepeatedTagError } from '@/domain/course-management/application/use-cases/errors/repeated-tag-error' | ||
import { TagAlreadyExistsError } from '@/domain/course-management/application/use-cases/errors/tag-already-exists-error' | ||
import { makeRegisterTagUseCase } from '@/infra/use-cases/factories/make-register-tag-use-case' | ||
import { type FastifyReply, type FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
|
||
const registerTagsBodySchema = z.object({ | ||
tags: z.array(z.string()) | ||
}) | ||
|
||
export async function registerTagsController(request: FastifyRequest, reply: FastifyReply) { | ||
const { tags } = registerTagsBodySchema.parse(request.body) | ||
|
||
const registerTagUseCase = makeRegisterTagUseCase() | ||
|
||
const result = await registerTagUseCase.exec({ | ||
tags | ||
}) | ||
|
||
if (result.isLeft()) { | ||
const error = result.value | ||
|
||
switch (error.constructor) { | ||
case RepeatedTagError: | ||
return await reply.status(409).send({ message: error.message }) | ||
case TagAlreadyExistsError: | ||
return await reply.status(409).send({ message: error.message }) | ||
default: | ||
return await reply.status(500).send({ message: error.message }) | ||
} | ||
} | ||
|
||
return await reply.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,8 @@ | ||
import { type FastifyInstance } from 'fastify' | ||
import { registerTagsController } from '../controllers/register-tags' | ||
import { verifyJwt } from '../middlewares/verify-jwt' | ||
import { verifyUserRole } from '../middlewares/verify-user-role' | ||
|
||
export async function tagRoutes(app: FastifyInstance) { | ||
app.post('/tags', { onRequest: [verifyJwt, verifyUserRole('INSTRUCTOR')] }, registerTagsController) | ||
} |