Skip to content

Commit

Permalink
Feat(Course, Enrollment, Route): Fetch course students route
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 23, 2024
1 parent 0cc96f9 commit fabc655
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
- [x] POST /sessions - User authentication

### Students
- [ ] GET /courses/:courseId/students - Get students enrolled in course
- [x] GET /courses/:courseId/students - Get students enrolled in course
- [ ] GET /students/:studentId/enrollments - Get student courses with instructor and evaluations

### Instructors
Expand Down
45 changes: 45 additions & 0 deletions src/infra/http/controllers/fetch-course-students.ts
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 Student } from '@/domain/course-management/enterprise/entities/student'
import { makeStudentMapper } from '@/infra/database/prisma/mappers/factories/make-student-mapper'
import { makeGetCourseWithStudentsUseCase } from '@/infra/use-cases/factories/make-get-course-with-students-use-case'
import { type FastifyReply, type FastifyRequest } from 'fastify'
import { z } from 'zod'
import { UserPresenter } from '../presenters/user-presenter'

const fetchCourseStudentsParamsSchema = z.object({
courseId: z.string().uuid()
})

export async function fetchCourseStudentsController(request: FastifyRequest, reply: FastifyReply) {
const { courseId } = fetchCourseStudentsParamsSchema.parse(request.params)

const fetchCourseStudentsUseCase = makeGetCourseWithStudentsUseCase()

const result = await fetchCourseStudentsUseCase.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 studentMapper = makeStudentMapper()
const students = result.value.courseWithStudents.students

const infraStudents = await Promise.all(
students.map(async (student: Student) => {
return await studentMapper.toPrisma(student)
})
)

return await reply.status(200).send({
students: infraStudents.map(infraStudent => UserPresenter.toHTTP(infraStudent))
})
}
2 changes: 2 additions & 0 deletions src/infra/http/routes/course.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type FastifyInstance } from 'fastify'
import { deleteCourseController } from '../controllers/delete-course'
import { editCourseDetailsController } from '../controllers/edit-course-details'
import { fetchCourseStudentsController } from '../controllers/fetch-course-students'
import { fetchRecentCoursesController } from '../controllers/fetch-recent-courses'
import { getCourseDetailsController } from '../controllers/get-course-details'
import { getCourseMetricsController } from '../controllers/get-course-metrics'
Expand All @@ -17,6 +18,7 @@ export async function courseRoutes(app: FastifyInstance) {
app.get('/courses/filter/name', queryCoursesByNameController)
app.get('/courses/filter/tags', queryCoursesByTagController)
app.get('/courses/:courseId/stats', getCourseStatsController)
app.get('/courses/:courseId/students', fetchCourseStudentsController)

app.get('/courses/:courseId/metrics', { onRequest: [verifyJwt, verifyUserRole('INSTRUCTOR')] }, getCourseMetricsController)

Expand Down

0 comments on commit fabc655

Please sign in to comment.