Example #1
0
        public IActionResult Post([FromBody] PlayerViewModel player)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var gripStyle = _playerRepository.GetGripStyle(player.GripStyleId);

                    var skillLevel = _playerRepository.GetSkillLevel(player.SkillLevelId);

                    var newPlayer = new Player()
                    {
                        Id         = player.PlayerId,
                        Name       = player.PlayerName,
                        NickName   = player.PlayerNickname,
                        GripStyle  = gripStyle,
                        SkillLevel = skillLevel,
                        Date       = DateTime.Now
                    };

                    _playerRepository.AddEntity(newPlayer);
                    if (_playerRepository.SaveAll())
                    {
                        var vm = new PlayerViewModel()
                        {
                            PlayerId       = newPlayer.Id,
                            PlayerName     = newPlayer.Name,
                            PlayerNickname = newPlayer.NickName,
                            GripStyleId    = newPlayer.GripStyle.Id,
                            SkillLevelId   = newPlayer.SkillLevel.Id,
                            CreatedDate    = newPlayer.Date
                        };

                        return(Created($"/api/player/{vm.PlayerId}", vm));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save new player {ex}");
            }

            return(BadRequest("Failed to save new player"));
        }