-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96a0e81
commit 3d596b9
Showing
16 changed files
with
135 additions
and
80 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 @@ | ||
import { Request, Response } from 'express'; | ||
import asyncHandler from 'express-async-handler'; | ||
import { EnrollmentService } from '../service/enrollment'; | ||
|
||
const enrollmentService = new EnrollmentService(); | ||
|
||
export const enrollInCourse = asyncHandler(async (req: Request, res: Response) => { | ||
const { userId, courseId } = req.body; | ||
const enrollment = await enrollmentService.enrollUserInCourse(userId, courseId); | ||
res.status(201).json({ message: 'Enrolled successfully', enrollment }); | ||
}); | ||
|
||
export const getUserEnrollments = asyncHandler(async (req: Request, res: Response) => { | ||
const { userId } = req.params; | ||
const enrollments = await enrollmentService.getUserEnrollments(Number(userId)); | ||
res.status(200).json(enrollments); | ||
}); | ||
|
||
export const updateLessonProgress = asyncHandler(async (req: Request, res: Response) => { | ||
const { enrollmentId, lessonId } = req.params; | ||
const { progress } = req.body; | ||
const enrollmentLesson = await enrollmentService.updateLessonProgress(Number(enrollmentId), Number(lessonId), progress); | ||
res.status(200).json({ message: 'Lesson progress updated', enrollmentLesson }); | ||
}); |
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
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
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
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,12 @@ | ||
import { Router } from 'express'; | ||
import { enrollInCourse, getUserEnrollments, updateLessonProgress } from '../controller/enrollment'; | ||
|
||
const router = Router(); | ||
|
||
router.post('/', enrollInCourse); | ||
|
||
router.get('/:userId', getUserEnrollments); | ||
|
||
router.put('/:enrollmentId/lessons/:lessonId/progress', updateLessonProgress); | ||
|
||
export default router; |
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,8 @@ | ||
import { Router } from 'express'; | ||
import enrollmentRoutes from './enrollment'; | ||
|
||
const router = Router(); | ||
|
||
router.use('/enrollments', enrollmentRoutes); | ||
|
||
export default router; |
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,70 @@ | ||
import { AppDataSource } from '../repos/db'; | ||
import { Enrollment } from '../entity/Enrollment'; | ||
import { User } from '../entity/User'; | ||
import { Course } from '../entity/Course'; | ||
import { EnrollmentLesson } from '../entity/EnrollmentLesson'; | ||
import { Lesson } from '../entity/Lesson'; | ||
|
||
export class EnrollmentService { | ||
private enrollmentRepository = AppDataSource.getRepository(Enrollment); | ||
private courseRepository = AppDataSource.getRepository(Course); | ||
private userRepository = AppDataSource.getRepository(User); | ||
private enrollmentLessonRepository = AppDataSource.getRepository(EnrollmentLesson); | ||
private lessonRepository = AppDataSource.getRepository(Lesson); | ||
|
||
async enrollUserInCourse(userId: number, courseId: number): Promise<Enrollment> { | ||
const user = await this.userRepository.findOne({ where: { id: userId } }); | ||
const course = await this.courseRepository.findOne({ where: { id: courseId } }); | ||
|
||
if (!user || !course) { | ||
throw new Error('User or Course not found'); | ||
} | ||
|
||
const enrollment = this.enrollmentRepository.create({ | ||
user, | ||
course, | ||
enrollment_date: new Date(), | ||
progress: 0, | ||
}); | ||
|
||
await this.enrollmentRepository.save(enrollment); | ||
|
||
const lessons = await this.lessonRepository.find({ where: { section: { course_id: courseId } } }); | ||
const enrollmentLessons = lessons.map(lesson => this.enrollmentLessonRepository.create({ | ||
enrollment, | ||
lesson, | ||
progress: 0, | ||
})); | ||
|
||
await this.enrollmentLessonRepository.save(enrollmentLessons); | ||
|
||
return enrollment; | ||
} | ||
|
||
async getUserEnrollments(userId: number): Promise<Enrollment[]> { | ||
return this.enrollmentRepository.find({ | ||
where: { user: { id: userId } }, | ||
relations: ['course'], | ||
}); | ||
} | ||
|
||
async updateLessonProgress(enrollmentId: number, lessonId: number, progress: number): Promise<EnrollmentLesson> { | ||
const enrollmentLesson = await this.enrollmentLessonRepository.findOne({ | ||
where: { enrollment_id: enrollmentId, lesson_id: lessonId }, | ||
}); | ||
|
||
if (!enrollmentLesson) { | ||
throw new Error('EnrollmentLesson not found'); | ||
} | ||
|
||
enrollmentLesson.progress = progress; | ||
|
||
if (progress === 100) { | ||
enrollmentLesson.completion_date = new Date(); | ||
} | ||
|
||
await this.enrollmentLessonRepository.save(enrollmentLesson); | ||
|
||
return enrollmentLesson; | ||
} | ||
} |