Academatica.Api/Academatica.Api.Course/Controllers/CourseController.cs
/// <summary>
/// Endpoint used to finish the practice for authenticated user.
/// </summary>
/// <param name="finishPracticeDto">Practice finish params - amount of mistakes.</param>
/// <returns>Action result with received exp and achievements.</returns>
Endpoint used to finish the practice for authenticated user.
Endpoint used to finish the practice for authenticated user .
[HttpPost]
        [Route("practice/finish")]
        public async Task<IActionResult> FinishPracticeForUser([FromBody] FinishPracticeForUserRequestDto finishPracticeDto)
        {
            if (ModelState.IsValid)
            {
                var userId = User.FindFirst("sub")?.Value;
                if (string.IsNullOrEmpty(userId))
                {
                    return BadRequest("User ID was null.");
                }
                var user = _academaticaDbContext.Users.Where(x => x.Id.ToString() == userId).FirstOrDefault();
                if (user == null)
                {
                    return NotFound("Invalid user ID.");
                }
                var buoyAdded = false;
                var statsEntry = _academaticaDbContext.UserStats.Where(x => x.UserId == user.Id).FirstOrDefault();
                if (statsEntry != null)
                {
                    statsEntry.UserExp += statsEntry.UserExp + 50 <= 15000 ? 50u : 0;
                    statsEntry.UserExpThisWeek += 50u;
                    ExpChangePublishDto expChangePublishDto = new ExpChangePublishDto()
                    {
                        UserId = user.Id,
                        ExpThisWeek = statsEntry.UserExpThisWeek,
                        Event = "EXP_CHANGE"
                    };
                    _messageBusClient.PublishExpChange(expChangePublishDto);
                    if (statsEntry.BuoysLeft < 5 && !finishPracticeDto.IsCustomPractice)
                    {
                        buoyAdded = true;
                        statsEntry.BuoysLeft++;
                    }
                }
                var topicId = finishPracticeDto.TopicId;
                if (!string.IsNullOrEmpty(finishPracticeDto.ClassId))
                {
                    var practiceClass = _academaticaDbContext.Classes.Where(x => x.Id == finishPracticeDto.ClassId).FirstOrDefault();
                    if (practiceClass != null)
                    {
                        topicId = practiceClass.TopicId;
                    }
                }
                if (!string.IsNullOrEmpty(finishPracticeDto.TopicId))
                {
                    var finishedPracticeTopic = _academaticaDbContext.Topics.Where(x => x.Id == finishPracticeDto.TopicId).FirstOrDefault();
                    if (finishedPracticeTopic == null)
                    {
                        return NotFound("Invalid class topic.");
                    }
                    var userTopicMistakes = _academaticaDbContext.UserTopicMistakes.Where(x => x.TopicId == finishedPracticeTopic.Id).FirstOrDefault();
                    if (userTopicMistakes != null)
                    {
                        userTopicMistakes.MistakeCount = (ulong)finishPracticeDto.MistakeCount;
                    }
                }
                await _academaticaDbContext.SaveChangesAsync();
                return Ok(new FinishPracticeResponseDto()
                {
                    Exp = 50,
                    BuoyAdded = buoyAdded
                });
            } else
            {
                var message = string.Join(" | ", ModelState.Values
                                    .SelectMany(v => v.Errors)
                                    .Select(e => e.ErrorMessage));
                return BadRequest(message);
            }
        }
[ HttpPost ] [ Route ( " " ) ] public async Task < IActionResult > FinishPracticeForUser ( [ FromBody ] FinishPracticeForUserRequestDto finishPracticeDto ) { if ( ModelState . IsValid ) { var userId = User . FindFirst ( " " ) ? . Value ; if ( string . IsNullOrEmpty ( userId ) ) { return BadRequest ( " " ) ; } var user = _academaticaDbContext . Users . Where ( x => x . Id . ToString ( ) == userId ) . FirstOrDefault ( ) ; if ( user == null ) { return NotFound ( " " ) ; } var buoyAdded = false ; var statsEntry = _academaticaDbContext . UserStats . Where ( x => x . UserId == user . Id ) . FirstOrDefault ( ) ; if ( statsEntry != null ) { statsEntry . UserExp += statsEntry . UserExp + 50 <= 15000 ? 50u : 0 ; statsEntry . UserExpThisWeek += 50u ; ExpChangePublishDto expChangePublishDto = new ExpChangePublishDto ( ) { UserId = user . Id , ExpThisWeek = statsEntry . UserExpThisWeek , Event = " " } ; _messageBusClient . PublishExpChange ( expChangePublishDto ) ; if ( statsEntry . BuoysLeft < 5 && ! finishPracticeDto . IsCustomPractice ) { buoyAdded = true ; statsEntry . BuoysLeft ++ ; } } var topicId = finishPracticeDto . TopicId ; if ( ! string . IsNullOrEmpty ( finishPracticeDto . ClassId ) ) { var practiceClass = _academaticaDbContext . Classes . Where ( x => x . Id == finishPracticeDto . ClassId ) . FirstOrDefault ( ) ; if ( practiceClass != null ) { topicId = practiceClass . TopicId ; } } if ( ! string . IsNullOrEmpty ( finishPracticeDto . TopicId ) ) { var finishedPracticeTopic = _academaticaDbContext . Topics . Where ( x => x . Id == finishPracticeDto . TopicId ) . FirstOrDefault ( ) ; if ( finishedPracticeTopic == null ) { return NotFound ( " " ) ; } var userTopicMistakes = _academaticaDbContext . UserTopicMistakes . Where ( x => x . TopicId == finishedPracticeTopic . Id ) . FirstOrDefault ( ) ; if ( userTopicMistakes != null ) { userTopicMistakes . MistakeCount = ( ulong ) finishPracticeDto . MistakeCount ; } } await _academaticaDbContext . SaveChangesAsync ( ) ; return Ok ( new FinishPracticeResponseDto ( ) { Exp = 50 , BuoyAdded = buoyAdded } ) ; } else { var message = string . Join ( " " , ModelState . Values . SelectMany ( v => v . Errors ) . Select ( e => e . ErrorMessage ) ) ; return BadRequest ( message ) ; } }
Endpoint used to finish the practice for authenticated user.
Endpoint used to finish the practice for authenticated user .
Action result with received exp and achievements.
Action result with received exp and achievements .
FinishPracticeForUserRequestDto
[FromBody]
finishPracticeDto
Practice finish params - amount of mistakes.
Practice finish params - amount of mistakes .