/
dmuruz
/
Course_Service
Обзор
Документация
Войти
/
dmuruz
/
Course_Service
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
src/main/java/org/example/manager/UserProgressManager.java
87 строк
3 KB
Danil Muruz
MVP
24 апр 2025, 09:39
24 апр 2025, 09:39
16b1c45
Код
Авторство
О чём код?
package org.example.manager; import org.example.dao.LessonRepository; import org.example.dao.UserLessonRepository; import org.example.dto.LessonProgressDTO; import org.example.dto.UserProgressDTO; import org.example.model.Lesson; import org.example.model.LessonStatus; import org.example.model.UserLesson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.stream.Collectors; @Service public class UserProgressManager { @Autowired private UserLessonRepository userLessonRepository; @Autowired private LessonRepository lessonRepository; public UserProgressDTO getTrainingProgress(Long userId, Long courseId) { List<UserLesson> lessons = userLessonRepository.findByUserIdAndLesson_CourseId(userId, courseId); int total = lessons.size(); int completed = (int) lessons.stream().filter(ul -> ul.getStatus() == LessonStatus.COMPLETED).count(); double percent = total > 0 ? (completed * 100.0 / total) : 0.0; UserProgressDTO progressDTO = new UserProgressDTO(); progressDTO.setUserId(userId); progressDTO.setCourseId(courseId); progressDTO.setTotalLessons(total); progressDTO.setLessonsCompleted(completed); progressDTO.setProgressPercentage(percent); return progressDTO; } public List<LessonProgressDTO> getAllLessonsWithStatus(Long userId) { return lessonRepository.findAll().stream().map(lesson -> { UserLesson ul = userLessonRepository .findByUserIdAndLesson_Id(userId, lesson.getId()) .orElse(null); LessonStatus status = (ul != null ? ul.getStatus() : LessonStatus.NOT_STARTED); Integer steps = (ul != null ? ul.getCompletedSteps() : 0); return new LessonProgressDTO( lesson.getId(), lesson.getName(), lesson.getDescription(), lesson.getJsonSchema(), lesson.getDurationSteps(), lesson.getOrderNum(), lesson.getCourse().getId(), status, steps ); }).collect(Collectors.toList()); } public UserLesson updateUserLessonStatus( Long userId, Long lessonId, LessonStatus status, Integer completedSteps) { UserLesson userLesson = userLessonRepository .findByUserIdAndLesson_Id(userId, lessonId) .orElseGet(() -> { Lesson lesson = lessonRepository.findById(lessonId) .orElseThrow(() -> new RuntimeException("Lesson not found: " + lessonId) ); UserLesson ul = new UserLesson(); ul.setUserId(userId); ul.setLesson(lesson); ul.setStatus(LessonStatus.NOT_STARTED); ul.setCompletedSteps(0); return ul; }); userLesson.setStatus(status); userLesson.setCompletedSteps(completedSteps); return userLessonRepository.save(userLesson); } }