Skip to content

Commit

Permalink
Feat(Tag, Route): Register tag route
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 22, 2024
1 parent ccd5a01 commit 3e948f5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/infra/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { classRoutes } from './http/routes/class'
import { courseRoutes } from './http/routes/course'
import { imageRoutes } from './http/routes/image'
import { moduleRoutes } from './http/routes/module'
import { tagRoutes } from './http/routes/tag'
import { userRoutes } from './http/routes/user'
import { videoRoutes } from './http/routes/video'

Expand Down Expand Up @@ -51,6 +52,7 @@ app.register(imageRoutes)
app.register(videoRoutes)
app.register(moduleRoutes)
app.register(classRoutes)
app.register(tagRoutes)

// Custom error handler

Expand Down
34 changes: 34 additions & 0 deletions src/infra/http/controllers/register-tags.ts
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()
}
8 changes: 8 additions & 0 deletions src/infra/http/routes/tag.ts
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)
}

0 comments on commit 3e948f5

Please sign in to comment.