-
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(Student, Enrollment, Route): Fetch student courses route
- Loading branch information
1 parent
fabc655
commit 67a2bf7
Showing
4 changed files
with
103 additions
and
1 deletion.
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,93 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { type CourseWithInstructorAndEvaluationDTO } from '@/domain/course-management/enterprise/entities/dtos/course-with-instructor-and-evaluation' | ||
import { CourseDtoMapper } from '@/domain/course-management/enterprise/entities/dtos/mappers/course-dto-mapper' | ||
import { makeGetCourseEvaluationsAverageUseCase } from '@/infra/use-cases/factories/make-get-course-evaluations-average-use-case' | ||
import { makeGetStudentWithCoursesUseCase } from '@/infra/use-cases/factories/make-get-student-with-courses-use-case' | ||
import { makeGetUserInfoUseCase } from '@/infra/use-cases/factories/make-get-user-info-use-case' | ||
import { type FastifyReply, type FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
import { CoursesWithInstructorAndEvaluationPresenter } from '../presenters/courses-with-instructor-and-evaluation-presenter' | ||
|
||
const fetchStudentCoursesParamsSchema = z.object({ | ||
studentId: z.string().uuid() | ||
}) | ||
|
||
export async function fetchStudentCoursesController(request: FastifyRequest, reply: FastifyReply) { | ||
const { studentId } = fetchStudentCoursesParamsSchema.parse(request.params) | ||
|
||
const fetchStudentCoursesUseCase = makeGetStudentWithCoursesUseCase() | ||
const getUserInfoUseCase = makeGetUserInfoUseCase() | ||
const getCourseEvaluationsAverageUseCase = makeGetCourseEvaluationsAverageUseCase() | ||
|
||
const result = await fetchStudentCoursesUseCase.exec({ | ||
studentId | ||
}) | ||
|
||
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 courses = result.value.studentWithCourses.courses | ||
|
||
const coursesWithInstructorAndEvaluation: CourseWithInstructorAndEvaluationDTO[] = [] | ||
|
||
await Promise.all( | ||
courses.map(async (course) => { | ||
const instructorResult = await getUserInfoUseCase.exec({ | ||
id: course.instructorId.toString() | ||
}) | ||
const courseEvaluationAverageResult = await getCourseEvaluationsAverageUseCase.exec({ | ||
courseId: course.id.toString() | ||
}) | ||
|
||
if (instructorResult.isLeft()) { | ||
const error = instructorResult.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 }) | ||
} | ||
} | ||
|
||
if (courseEvaluationAverageResult.isLeft()) { | ||
return await reply.status(500).send() | ||
} | ||
|
||
const { user } = instructorResult.value | ||
const { evaluationsAverage } = courseEvaluationAverageResult.value | ||
|
||
const courseWithInstructorAndEvaluation: CourseWithInstructorAndEvaluationDTO = { | ||
course: CourseDtoMapper.toDTO(course), | ||
instructor: { | ||
id: user.id, | ||
name: user.name, | ||
email: user.email, | ||
age: user.age, | ||
registeredAt: user.registeredAt, | ||
summary: user.summary, | ||
bannerImageKey: user.bannerImageKey, | ||
profileImageKey: user.profileImageKey | ||
}, | ||
evaluationsAverage | ||
} | ||
|
||
coursesWithInstructorAndEvaluation.push(courseWithInstructorAndEvaluation) | ||
}) | ||
) | ||
|
||
return await reply.status(200).send({ | ||
courses: coursesWithInstructorAndEvaluation.map(courseWithInstructorAndEvaluation => | ||
CoursesWithInstructorAndEvaluationPresenter.toHTTP(courseWithInstructorAndEvaluation | ||
) | ||
) | ||
}) | ||
} |
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 { fetchStudentCoursesController } from '../controllers/fetch-student-courses' | ||
import { verifyJwt } from '../middlewares/verify-jwt' | ||
|
||
export async function studentRoutes(app: FastifyInstance) { | ||
app.get('/students/:studentId/enrollments', { onRequest: [verifyJwt] }, fetchStudentCoursesController) | ||
} |