예제 #1
0
        public async Task <long> Handle(GetLastVisitedLessonQuery request, CancellationToken cancellationToken)
        {
            var lastVisited = await _context.LastVisitedLessons
                              .AsNoTracking()
                              .Where(x => x.CourseId == request.CourseId && x.UserId == _currentUser.UserId)
                              .FirstOrDefaultAsync(cancellationToken);

            if (lastVisited == null)
            {
                lastVisited = new LastVisitedLessonEntity
                {
                    CourseId = request.CourseId,
                    UserId   = _currentUser.UserId,
                    LessonId = (await _context.Courses
                                .Where(x => x.Id == request.CourseId)
                                .Include(x => x.Modules)
                                .ThenInclude(y => y.Lessons)
                                .FirstOrDefaultAsync(cancellationToken)).Modules.First().Lessons.First().Id
                };

                await _context.LastVisitedLessons.AddAsync(lastVisited, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);
            }

            return(lastVisited.LessonId);
        }
예제 #2
0
        public async Task <LessonWithoutAnswersResponse> Handle(GetLessonWithoutAnswersQuery request, CancellationToken cancellationToken)
        {
            var courseId = (await _context.Lessons
                            .AsNoTracking()
                            .Where(x => x.Id == request.LessonId)
                            .Include(x => x.Module)
                            .FirstOrDefaultAsync(cancellationToken)).Module.CourseId;

            var subscribed =
                await _context.Subscriptions.AnyAsync(
                    x => x.SubscriberId == _currentUser.UserId && x.CourseId == courseId, cancellationToken);

            if (!subscribed)
            {
                throw new NotImplementedException();
            }

            var lesson = await _context.Lessons
                         .AsNoTracking()
                         .Where(x => x.Id == request.LessonId)
                         .Include(x => x.Sections).ThenInclude(z => z.Quiz).ThenInclude(y => y.Questions).ThenInclude(c => c.Answers)
                         .Include(x => x.Sections).ThenInclude(z => z.Quiz).ThenInclude(y => y.Attempts)
                         .Include(x => x.Comments).ThenInclude(z => z.Author)
                         .Include(x => x.Comments).ThenInclude(z => z.UsersLikes)
                         .Include(x => x.Comments).ThenInclude(z => z.Replies).ThenInclude(y => y.Author)
                         .Include(x => x.Comments).ThenInclude(z => z.Replies).ThenInclude(y => y.UsersLikes)
                         .FirstOrDefaultAsync(cancellationToken);

            if (lesson == null)
            {
                throw new NotImplementedException();
            }

            var progress = await _context.Progresses
                           .AsNoTracking()
                           .Where(x => x.LessonId == request.LessonId && x.UserId == _currentUser.UserId)
                           .FirstOrDefaultAsync(cancellationToken);

            if (progress == null)
            {
                throw new NotImplementedException();
            }

            progress.IsVisited = true;
            _context.Progresses.Update(progress);

            var lastVisited = await _context.LastVisitedLessons
                              .AsNoTracking()
                              .Where(x => x.CourseId == courseId && x.UserId == _currentUser.UserId)
                              .FirstOrDefaultAsync(cancellationToken);

            if (lastVisited == null)
            {
                lastVisited = new LastVisitedLessonEntity
                {
                    CourseId = courseId,
                    UserId   = _currentUser.UserId,
                    LessonId = lesson.Id
                };

                await _context.LastVisitedLessons.AddAsync(lastVisited, cancellationToken);
            }
            else
            {
                lastVisited.LessonId = lesson.Id;
                _context.LastVisitedLessons.Update(lastVisited);
            }

            var sections = lesson.Sections.Where(x => x.Type == LessonSectionType.Quiz).ToList();

            foreach (var section in sections)
            {
                section.Quiz.MaxAttempts -= section.Quiz.Attempts.Count;
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <LessonEntity, LessonWithoutAnswersResponse>(lesson));
        }