-
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(Course, Class, Route): Fetch course classes route
- Loading branch information
1 parent
696409e
commit 7e95923
Showing
6 changed files
with
70 additions
and
2 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
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,45 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { type Class } from '@/domain/course-management/enterprise/entities/class' | ||
import { makeClassMapper } from '@/infra/database/prisma/mappers/factories/make-class-mapper' | ||
import { makeFetchCourseClassesUseCase } from '@/infra/use-cases/factories/make-fetch-course-classes-use-case' | ||
import { type FastifyReply, type FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
import { ClassPresenter } from '../presenters/class-presenter' | ||
|
||
const fetchCourseClassesParamsSchema = z.object({ | ||
courseId: z.string() | ||
}) | ||
|
||
export async function fetchCourseClassesController(request: FastifyRequest, reply: FastifyReply) { | ||
const { courseId } = fetchCourseClassesParamsSchema.parse(request.params) | ||
|
||
const fetchCourseClassesUseCase = makeFetchCourseClassesUseCase() | ||
|
||
const result = await fetchCourseClassesUseCase.exec({ | ||
courseId | ||
}) | ||
|
||
if (result.isLeft()) { | ||
const error = result.value | ||
|
||
switch (error.constructor) { | ||
case ResourceNotFoundError: | ||
return await reply.status(404).send({ message: error.message }) | ||
default: | ||
return await reply.status(500).send({ message: error.message }) | ||
} | ||
} | ||
|
||
const classMapper = makeClassMapper() | ||
const { classes } = result.value | ||
|
||
const infraClasses = await Promise.all( | ||
classes.map(async (classToMap: Class) => { | ||
return await classMapper.toPrisma(classToMap) | ||
}) | ||
) | ||
|
||
return await reply.status(200).send({ | ||
classes: infraClasses.map(infraClass => ClassPresenter.toHTTP(infraClass)) | ||
}) | ||
} |
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,14 @@ | ||
import { type Prisma } from '@prisma/client' | ||
|
||
export class ClassPresenter { | ||
static toHTTP(classToPresent: Prisma.ClassUncheckedCreateInput) { | ||
return { | ||
id: classToPresent.id, | ||
name: classToPresent.name, | ||
description: classToPresent.description, | ||
classNumber: classToPresent.classNumber, | ||
moduleId: classToPresent.moduleId, | ||
videoId: classToPresent.videoId | ||
} | ||
} | ||
} |
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,7 @@ | ||
import { type FastifyInstance } from 'fastify' | ||
import { fetchCourseClassesController } from '../controllers/fetch-course-classes' | ||
import { verifyJwt } from '../middlewares/verify-jwt' | ||
|
||
export async function classRoutes(app: FastifyInstance) { | ||
app.get('/courses/:courseId/classes', { onRequest: [verifyJwt] }, fetchCourseClassesController) | ||
} |