-
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.
Fix(Recent courses route): Fetch recent courses with instructor and e…
…valuations average
- Loading branch information
1 parent
c61fb84
commit 2e7bbf7
Showing
1 changed file
with
57 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,77 @@ | ||
import { type Course } from '@/domain/course-management/enterprise/entities/course' | ||
import { makeCourseMapper } from '@/infra/database/prisma/mappers/factories/make-course-mapper' | ||
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 { makeFetchRecentCoursesUseCase } from '@/infra/use-cases/factories/make-fetch-recent-courses-use-case' | ||
import { makeGetCourseEvaluationsAverageUseCase } from '@/infra/use-cases/factories/make-get-course-evaluations-average-use-case' | ||
import { makeGetUserInfoUseCase } from '@/infra/use-cases/factories/make-get-user-info-use-case' | ||
import { type FastifyReply, type FastifyRequest } from 'fastify' | ||
import { CoursePresenter } from '../presenters/course-presenter' | ||
import { CoursesWithInstructorAndEvaluationPresenter } from '../presenters/courses-with-instructor-and-evaluation-presenter' | ||
|
||
export async function fetchRecentCoursesController(request: FastifyRequest, reply: FastifyReply) { | ||
const fetchRecentCoursesUseCase = makeFetchRecentCoursesUseCase() | ||
const getUserInfoUseCase = makeGetUserInfoUseCase() | ||
const getCourseEvaluationsAverageUseCase = makeGetCourseEvaluationsAverageUseCase() | ||
|
||
const result = await fetchRecentCoursesUseCase.exec() | ||
|
||
if (result.isLeft()) { | ||
return await reply.status(500).send() | ||
} | ||
|
||
const courseMapper = makeCourseMapper() | ||
const { courses } = result.value | ||
|
||
const infraCourses = await Promise.all( | ||
courses.map(async (course: Course) => { | ||
return await courseMapper.toPrisma(course) | ||
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: infraCourses.map(infraCourse => CoursePresenter.toHTTP(infraCourse)) | ||
courses: coursesWithInstructorAndEvaluation.map(courseWithInstructorAndEvaluation => | ||
CoursesWithInstructorAndEvaluationPresenter.toHTTP(courseWithInstructorAndEvaluation | ||
) | ||
) | ||
}) | ||
} |