public bool CreateProgress(ChapterProgress progress) { try { DbContext.ChapterProgress.Add(progress); DbContext.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public bool SubmitProgress(Guid userId, ProgressDTO progressDTO) { ChapterProgress progress = chapterRepository.GetChapterProgress(userId, progressDTO.ChapterId); if (progress == null) { return(chapterRepository.CreateProgress(new ChapterProgress { UserId = userId, ChapterId = progressDTO.ChapterId, Score = progressDTO.Score })); } else { progress.Score = Math.Max(progress.Score, progressDTO.Score); return(chapterRepository.UpdateProgress(progress)); } }
public List <ChapterInProgressDTO> ListChaptersInProgress(Guid userId, Guid courseId) { List <Chapter> chapters = chapterRepository.ListChaptersByCourseId(courseId); List <ChapterProgress> chapterProgresses = chapterRepository.ListChapterProgresses(userId, chapters); List <ChapterInProgressDTO> result = new List <ChapterInProgressDTO>(); for (int i = 0; i < chapters.Count; i++) { ChapterProgress progress = chapterProgresses.Find(x => x.ChapterId == chapters[i].Id); int total = chapterRepository.GetChapterScore(chapters[i].Id); result.Add(new ChapterInProgressDTO { Id = chapters[i].Id, Name = chapters[i].Name, CurrentScore = progress == null ? 0 : Math.Min(progress.Score, total), TotalScore = total }); } ; return(result); }