public async Task UserObtainsCompleteCourseAchievementAsync()
        {
            //arrange
            var courseId        = 2;
            var username        = Guid.NewGuid().ToString();
            var user            = AddUser(username);
            var client          = GetHttpClientWithUserAuthorization(user);
            var completedLesson = new CompleteLessonCommand
            {
                StartTime = DateTime.UtcNow.AddHours(-2),
                EndTime   = DateTime.UtcNow.AddHours(-1)
            };
            var beforeCompletion   = DateTime.UtcNow;
            var lessonIdsForCourse = dbContext.Lessons.Where(x => x.Chapter.Course.Id == courseId)
                                     .Select(x => x.Id).ToList();

            //act
            foreach (var lessonId in lessonIdsForCourse)
            {
                await client.PostAsJsonAsync($"/api/lessons/{lessonId}/actions/complete", completedLesson);
            }
            ;
            //assert
            var db                   = GetService <IMimoDbContext>();
            var updatedUser          = db.Users.Include(x => x.Achievements).First(x => x.Username == username);
            var completedAchievement = updatedUser.Achievements
                                       .Single(x => x.Achievement.Type == AchievementType.CompleteCourse);

            Assert.Equal(1, completedAchievement.Progress);
            Assert.Equal(courseId, completedAchievement.Course.Id);
            Assert.True(completedAchievement.IsCompleted);
            Assert.True(completedAchievement.CompletedOn > beforeCompletion);
        }
Exemplo n.º 2
0
        public CompleteLessonCommandHandlerTests()
        {
            command = new CompleteLessonCommand
            {
                Username  = "******",
                LessonId  = lessonId,
                StartTime = DateTime.UtcNow.AddMinutes(-30),
                EndTime   = DateTime.UtcNow
            };
            dbContextMock = new Mock <IMimoDbContext>();
            var achievementsDbSetMock = Helpers.GetMockDbSet(new List <Achievement> {
                new Achievement()
            }.AsQueryable());

            dbContextMock.SetupGet(x => x.Achievements)
            .Returns(achievementsDbSetMock.Object);
            mapperMock     = new Mock <IMapper>();
            updaterMock    = new Mock <IUserAchievementsUpdater>();
            calculatorMock = new Mock <IAchievementTypesToUpdateCalculator>();
            validatorMock  = new Mock <IResultValidator <CompleteLessonCommand> >();
            validatorMock.Setup(x => x.Validate(It.IsAny <CompleteLessonCommand>()))
            .Returns(Result.Ok());
            handler = new CompleteLessonCommandHandler(dbContextMock.Object,
                                                       mapperMock.Object,
                                                       updaterMock.Object,
                                                       calculatorMock.Object,
                                                       validatorMock.Object);
        }
        public async Task UserObtainsComplete5LessonsAchievementAsync()
        {
            //arrange
            var username        = Guid.NewGuid().ToString();
            var user            = AddUser(username);
            var client          = GetHttpClientWithUserAuthorization(user);
            var completedLesson = new CompleteLessonCommand
            {
                StartTime = DateTime.UtcNow.AddHours(-2),
                EndTime   = DateTime.UtcNow.AddHours(-1)
            };
            var beforeCompletion = DateTime.UtcNow;

            //act
            foreach (var lessonId in new List <int> {
                2, 6, 4, 3, 2, 9
            })
            {
                await client.PostAsJsonAsync($"/api/lessons/{lessonId}/actions/complete", completedLesson);
            }
            ;
            //assert
            var db                   = GetService <IMimoDbContext>();
            var updatedUser          = db.Users.Include(x => x.Achievements).First(x => x.Username == username);
            var completedAchievement = updatedUser.Achievements
                                       .FirstOrDefault(x => x.Achievement.Type == AchievementType.CompleteLesson &&
                                                       x.Achievement.Target == 5);

            Assert.Equal(5, completedAchievement.Progress);
            Assert.True(completedAchievement.IsCompleted);
            Assert.True(completedAchievement.CompletedOn > beforeCompletion);
        }
 private UserLesson BuildCompletedLesson(CompleteLessonCommand command, int courseId, int chapterId)
 {
     return(new UserLesson
     {
         LastStartedOn = command.StartTime.Value,
         LastFinishedOn = command.EndTime.Value,
         LessonId = command.LessonId,
         ChapterId = chapterId,
         CourseId = courseId,
         NoOfCompletions = 1
     });
 }
        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)));
        }
        private Result <List <UserAchievementDto> > UpdateAlreadyCompletedLesson(UserLesson completedLesson, CompleteLessonCommand command)
        {
            completedLesson.LastStartedOn  = command.StartTime.Value;
            completedLesson.LastFinishedOn = command.EndTime.Value;
            completedLesson.NoOfCompletions++;

            dbContext.SaveChanges();
            return(Result.Ok(new List <UserAchievementDto>()));
        }
Exemplo n.º 7
0
 public ActionResult <List <UserAchievementDto> > CompleteLesson(int lessonId, [FromBody] CompleteLessonCommand command)
 {
     command.LessonId = lessonId;
     command.Username = contextAccesor.HttpContext.User.Identity.Name;
     return(completeLessonHandler.Handle(command)
            .OnBoth((result) => responseHandler.GetResponse(result)));
 }