Example #1
0
        public async Task <IActionResult> CompleteLesson(int userId, [FromBody] CreateLessonCompletionRequest createLessonCompletionRequest)
        {
            if (createLessonCompletionRequest == null)
            {
                return(BadRequest("Request body is missing."));
            }

            var errors = createLessonCompletionRequest.Validate();

            if (errors.Any())
            {
                return(BadRequest(errors));
            }

            var user = await _context.Users.FindAsync(userId);

            if (user == null)
            {
                return(NotFound($"Unknown user [{userId}]."));
            }

            var lesson = await _context.Lessons.FindAsync(createLessonCompletionRequest.LessonId);

            if (lesson == null)
            {
                return(NotFound($"Unknown lesson [{createLessonCompletionRequest.LessonId}]."));
            }

            var lessonCompletion = new LessonCompletion
            {
                Lesson            = lesson,
                LessonId          = lesson.Id,
                User              = user,
                UserId            = user.Id,
                LessonStartedAt   = createLessonCompletionRequest.LessonStartedAt,
                LessonCompletedAt = createLessonCompletionRequest.LessonCompletedAt
            };

            var res = await _context.LessonCompletions.AddAsync(lessonCompletion);

            await _context.SaveChangesAsync();

            await _achievementService.UpdateAchievementsOfUserAsync(userId);

            return(Ok(res.Entity.Id));
        }