Skip to content

Commit

Permalink
Feat(Class, Route): Edit class details route
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 22, 2024
1 parent 65adf59 commit 5f09bca
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@

### Classes
- [x] GET /courses/:courseId/classes - Get course classes
- [ ] POST /modules/:moduleId/classes/video/:videoId - Register class
- [ ] PUT /classes/:classId - Update class details
- [x] POST /modules/:moduleId/classes/video/:videoId - Register class
- [x] PUT /classes/:classId - Update class details
- [ ] DELETE /classes/:classId - Delete class

### Tags
Expand Down
61 changes: 61 additions & 0 deletions src/infra/http/controllers/edit-class-details.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { NotAllowedError } from '@/core/errors/errors/not-allowed-error'
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error'
import { ClassNumberIsAlreadyInUseError } from '@/domain/course-management/application/use-cases/errors/class-number-is-already-in-use-error'
import { makeClassMapper } from '@/infra/database/prisma/mappers/factories/make-class-mapper'
import { makeEditClassDetailsUseCase } from '@/infra/use-cases/factories/make-edit-class-details-use-case'
import { type FastifyReply, type FastifyRequest } from 'fastify'
import { z } from 'zod'
import { ClassPresenter } from '../presenters/class-presenter'

const editClassDetailsBodySchema = z.object({
name: z.string().optional(),
description: z.string().optional(),
classNumber: z.number().optional(),
videoId: z.string().uuid().optional()
})

const editClassDetailsParamsSchema = z.object({
classId: z.string()
})

export async function editClassDetailsController(request: FastifyRequest, reply: FastifyReply) {
const { name, description, classNumber, videoId } = editClassDetailsBodySchema.parse(request.body)
const { classId } = editClassDetailsParamsSchema.parse(request.params)

const { sub: instructorId } = request.user

const editClassDetailsUseCase = makeEditClassDetailsUseCase()

const result = await editClassDetailsUseCase.exec({
name,
description,
classNumber,
videoId,
classId,
instructorId
})

if (result.isLeft()) {
const error = result.value

switch (error.constructor) {
case ResourceNotFoundError:
return await reply.status(404).send({ message: error.message })
case NotAllowedError:
return await reply.status(401).send({ message: error.message })
case ClassNumberIsAlreadyInUseError:
return await reply.status(409).send({ message: error.message })
default:
return await reply.status(500).send({ message: error.message })
}
}

const classMapper = makeClassMapper()
const { class: classToReply } = result.value

const infraClass = await classMapper.toPrisma(classToReply)

return await reply.status(200).send({
Class: ClassPresenter.toHTTP(infraClass)
})
}
3 changes: 3 additions & 0 deletions src/infra/http/routes/class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type FastifyInstance } from 'fastify'
import { addClassToModuleController } from '../controllers/add-class-to-module'
import { editClassDetailsController } from '../controllers/edit-class-details'
import { fetchCourseClassesController } from '../controllers/fetch-course-classes'
import { verifyJwt } from '../middlewares/verify-jwt'
import { verifyUserRole } from '../middlewares/verify-user-role'
Expand All @@ -9,4 +10,6 @@ export async function classRoutes(app: FastifyInstance) {

// FIXME: It should work after fixing the video repository
app.post('/modules/:moduleId/classes/video/:videoId', { onRequest: [verifyJwt, verifyUserRole('INSTRUCTOR')] }, addClassToModuleController)

app.put('/classes/:classId', { onRequest: [verifyJwt, verifyUserRole('INSTRUCTOR')] }, editClassDetailsController)
}

0 comments on commit 5f09bca

Please sign in to comment.