public async Task <ActionResult <GamesViewModel> > Post([FromBody] GamesViewModel gamesViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //var isvalidGame = _gameRepository.SearchNameGameAsync(gamesViewModel.Name,true);
                    //if (isvalidGame != null)
                    //{
                    //    return BadRequest("Game is already in database");
                    //}

                    //using Automapper
                    var newGame = _mapper.Map <GamesViewModel, Games>(gamesViewModel);

                    if (newGame.CreationDate == DateTime.MinValue)
                    {
                        newGame.CreationDate = DateTime.Now;
                    }
                    _gameRepository.AddEntity(newGame);
                    if (await _gameRepository.SaveAll())
                    {
                        //using Automapper
                        return(Created($"/api/GameAPI/{newGame.Name}", _mapper.Map <Games, GamesViewModel>(newGame)));
                    }
                }
            }

            catch
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
            return(BadRequest());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] GameSystemAPIViewModel gameSystem)
        {
            //add it to database
            try
            {
                if (ModelState.IsValid)
                {
                    //var newGameSystem = new GameSystem()
                    //{
                    //    CreationDate = gameSystem.CreationDate,
                    //    GameSystemID = gameSystem.GameSystemAPIID,
                    //    SystemName = gameSystem.SystemNameAPI,
                    //    GameLibrary = gameSystem.GameLibrary
                    //};

                    //using Automapper
                    var newGameSystem = mapper.Map <GameSystemAPIViewModel, GameSystem>(gameSystem);

                    if (newGameSystem.CreationDate == DateTime.MinValue)
                    {
                        newGameSystem.CreationDate = DateTime.Now;
                    }
                    //var currentUser = await userManager.FindByNameAsync(User.Identity.Name);
                    //newGameSystem.user = currentUser;
                    gameRepository.AddEntity(newGameSystem);
                    if (await gameRepository.SaveAll())
                    {
                        //var vm = new GameSystemAPIViewModel()
                        //{
                        //    CreationDate = newGameSystem.CreationDate,
                        //    GameSystemAPIID = newGameSystem.GameSystemID,
                        //    SystemNameAPI = newGameSystem.SystemName,
                        //    GameLibrary = gameSystem.GameLibrary
                        //};

                        //using Automapper
                        return(Created($"/api/GameSystem/{newGameSystem.GameSystemID}", mapper.Map <GameSystem, GameSystemAPIViewModel>(newGameSystem)));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"Failed to save game system:{ex}");
            }
            return(BadRequest("Failed to save game system"));
        }