public async Task <IActionResult> UpdateAsync(ModifyGameViewModel modifyGameViewModel) { if (!ModelState.IsValid) { var viewModel = await _gameViewModelFactory.CreateAsync(modifyGameViewModel); return(View("Update", viewModel)); } var dto = _mapper.Map <ModifyGameDto>(modifyGameViewModel); try { await _gameService.UpdateAsync(dto); } catch (ValidationException <GameRoot> e) { const string prefix = nameof(ModifyGameViewModel); ModelState.AddModelError($"{prefix}.{e.Key}", e.Message); var viewModel = await _gameViewModelFactory.CreateAsync(modifyGameViewModel); _logger.LogWarning(e.Message); return(View("Update", viewModel)); } _logger.LogDebug($"The game with id {modifyGameViewModel.Id} was updated in database"); return(RedirectToAction(nameof(GetAllAsync))); }
private static ModifyGameViewModel CreateGameViewModel() { var testViewModel = new ModifyGameViewModel { Name = "Name", Description = "Description" }; return(testViewModel); }
public async Task <IActionResult> UpdateAsync(ModifyGameViewModel modifyGameViewModel) { var dto = _mapper.Map <ModifyGameDto>(modifyGameViewModel); try { await _gameService.UpdateAsync(dto); } catch (ValidationException <GameRoot> e) { const string prefix = nameof(ModifyGameViewModel); ModelState.AddModelError($"{prefix}.{e.Key}", e.Message); _logger.LogWarning(e.Message); return(BadRequest(ModelState)); } _logger.LogDebug($"The game with id {modifyGameViewModel.Id} was updated in database"); return(NoContent()); }
public async Task <IActionResult> CreateAsync(ModifyGameViewModel modifyGameViewModel) { modifyGameViewModel.Id = Guid.NewGuid().ToString(); var dto = _mapper.Map <ModifyGameDto>(modifyGameViewModel); try { await _gameService.CreateAsync(dto); } catch (ValidationException <GameRoot> e) { const string prefix = nameof(ModifyGameViewModel); ModelState.AddModelError($"{prefix}.{e.Key}", e.Message); _logger.LogWarning(e.Message); return(BadRequest(ModelState)); } _logger.LogDebug($"A new game with name: {modifyGameViewModel.Name} was added to database"); return(CreatedAtAction(nameof(GetByIdAsync), new{ id = modifyGameViewModel.Id }, modifyGameViewModel)); }
private static ModifyGameViewModel GetModifyGameViewModel() { var viewModel = new ModifyGameViewModel(); return(viewModel); }