Skip to content

Commit

Permalink
Fix(Recent courses route): Fetch recent courses with instructor and e…
Browse files Browse the repository at this point in the history
…valuations average
  • Loading branch information
Artur-Poffo committed Feb 24, 2024
1 parent c61fb84 commit 2e7bbf7
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions src/infra/http/controllers/fetch-recent-courses.ts
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
)
)
})
}

0 comments on commit 2e7bbf7

Please sign in to comment.