-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enrollment - Learning #5
Open
thanhbao922003
wants to merge
1
commit into
awesome-academy:master
Choose a base branch
from
thanhbao922003:review_comment_component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,4 @@ | ||
import { Entity, | ||
Column, | ||
PrimaryGeneratedColumn, | ||
ManyToOne, | ||
JoinColumn, | ||
CreateDateColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
import { Entity,Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn, } from 'typeorm'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. các bạn trong nhóm dùng chung setting trên VScode để tránh bị re-format code khi push nha |
||
import { Review } from './Review'; | ||
|
||
@Entity('comments') | ||
|
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chưa có xử lý về mặt giao diện à e ơi