public Result <List <UserAchievementDto> > Handle(CompleteLessonCommand command)
        {
            var validationResult = completeLessonValidator.Validate(command);

            if (validationResult.Failure)
            {
                return(Result.Fail <List <UserAchievementDto> >(validationResult.Errors));
            }
            var user = dbContext.Users.Include(x => x.CompletedLessons)
                       .First(x => x.Username == command.Username);
            var completedLesson = user.CompletedLessons.FirstOrDefault(x => x.LessonId == command.LessonId);

            if (completedLesson != null)
            {
                return(UpdateAlreadyCompletedLesson(completedLesson, command));
            }
            var lessonResult = GetLessonOfPublishedCourse(command.LessonId);

            if (lessonResult.Failure)
            {
                return(Result.Fail <List <UserAchievementDto> >(lessonResult.Errors, lessonResult.StatusCode));
            }

            var courseId  = lessonResult.Value.Chapter.Course.Id;
            var chapterId = lessonResult.Value.Chapter.Id;

            user.AddCompletedLesson(BuildCompletedLesson(command, courseId, chapterId));

            var course = dbContext.Courses.Where(x => x.Id == courseId)
                         .Include(x => x.Chapters).ThenInclude(x => x.Lessons)
                         .First();

            var achievementTypesToUpdate = achievementTypesCalculator
                                           .CalculateAchievementTypesToUpdate(user.CompletedLessons, course, chapterId);

            var achievements           = dbContext.Achievements.ToList();
            var achievementsUpdatedNow = userAchievementsUpdater.UpdateUserAchievements(user,
                                                                                        course,
                                                                                        achievementTypesToUpdate,
                                                                                        achievements,
                                                                                        DateTime.UtcNow);

            dbContext.SaveChanges();
            return(Result.Ok(mapper.Map <List <UserAchievementDto> >(achievementsUpdatedNow)));
        }
Exemplo n.º 2
0
 public Result <CourseDto> Handle(CreateCourseCommand command)
 {
     return(createCourseValidator.Validate(command)
            .OnSuccess(() => uniqueCourseNameHandler.Handle(new CourseNameIsUniqueQuery(0, command.Name)))
            .OnSuccess(() => SaveCourse(command)));
 }