Ejemplo n.º 1
0
        public IActionResult CreateScoreForUser(Guid userId,
                                                [FromBody] ScoreForCreationDto score)
        {
            //Check if the request is properly deserialized
            if (score == null)
            {
                return(BadRequest());
            }

            //to validate constraints on input
            //if(!ModelState.IsValid)
            //{
            //    return new UnprocessableEntityObjectResult(ModelState);
            //}

            if (!_libraryRepository.UserExists(userId))
            {
                return(NotFound());
            }

            var scoreEntity = Mapper.Map <Score>(score);

            _libraryRepository.AddScoreForUser(userId, scoreEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Creating a score for user {userId} failed on save.");
            }

            var scoreToReturn = Mapper.Map <ScoreDto>(scoreEntity);

            return(CreatedAtRoute("GetScoreForUser",
                                  new { userId = userId, id = scoreToReturn.Id },
                                  CreateLinksForScore(scoreToReturn)));
        }