public IActionResult Post([FromBody] TableViewModel model) { try { if (ModelState.IsValid) { var newTable = _mapper.Map <TableViewModel, Table>(model); if (newTable.Date == DateTime.MinValue) { newTable.Date = DateTime.Now.AddDays(7); } _repository.AddEntity(newTable); if (_repository.SaveChanges()) { return(Created($"/api/games/{newTable.Id}", _mapper.Map <Table, TableViewModel>(newTable))); } } else { return(BadRequest(ModelState)); } } catch (Exception e) { _logger.LogError($"Failed to save new game: {e}"); } return(BadRequest("Failed to save new game")); }
public IActionResult Post([FromBody] GameViewModel model) { try { if (ModelState.IsValid) { var newGame = _mapper.Map <GameViewModel, Game>(model); _repository.AddEntity(newGame); if (_repository.SaveChanges()) { return(Created($"/api/games/{newGame.Id}", _mapper.Map <Game, GameViewModel>(newGame))); } } else { return(BadRequest(ModelState)); } } catch (Exception e) { _logger.LogError($"Failed to save new game: {e}"); } return(BadRequest("Failed to save new game")); }