private IActionResult SetViewModelByVerbName(string verbName, BoardGameViewModel viewModel, int?id) { try { switch (verbName) { case nameof(Delete): _boardGameService.Delete(_boardGameService.Get(id.Value)); break; case nameof(Post): _boardGameService.Create(viewModel.ToEntity()); break; case nameof(Put): _boardGameService.Edit(viewModel.ToEntity()); break; } return(Ok()); } catch (AppException ex) { return(SendFeedback(ex.Message)); } catch (System.Exception ex) { _logger.LogError(ex.Message, ex, ex.InnerException); return(SendFeedback(_unavailable)); } }
public IActionResult Edit(BoardGameViewModel viewModel) { if (ModelState.IsValid) { return(SetViewModelByActionName(nameof(Edit), viewModel)); } return(View(viewModel)); }
public void MaxPlayersWithMinPlayersPasses(int minPlayers, int maxPlayers) { var boardGame = new BoardGameViewModel { MinPlayers = minPlayers, MaxPlayers = maxPlayers }; _boardGameValidator.ShouldNotHaveValidationErrorFor(x => x.MaxPlayers, boardGame); }
public void MaxPlayersWithMinPlayersThrowsError(int minPlayers, int maxPlayers) { var boardGame = new BoardGameViewModel { MinPlayers = 1, MaxPlayers = 0 }; _boardGameValidator.ShouldHaveValidationErrorFor(x => x.MaxPlayers, boardGame); }
public async Task <IActionResult> Edit(Guid id, BoardGameViewModel boardGameViewModel) { if (!ModelState.IsValid) { return(View(boardGameViewModel)); } var boardGame = _mapper.Map <BoardGame>(boardGameViewModel); await _boardGameRepository.Atualizar(boardGame); return(RedirectToAction(nameof(Index))); }
public JsonResult Edit(BoardGameViewModel boardGameViewModel) { if (ModelState.IsValid) { var boardGame = Mapper.Map <BoardGame>(boardGameViewModel); _boardGameService.Edit(boardGame); return(Json(null, JsonRequestBehavior.AllowGet)); } var errors = Helpers.GetErrorMessages(ModelState.Values); return(Json(errors, JsonRequestBehavior.AllowGet)); }
private IActionResult SetViewModelByActionName(string actionName, BoardGameViewModel viewModel) { HttpResponseMessage response = null; string messageFeedback = ""; try { switch (actionName) { case nameof(Delete): response = _client.DeleteAsync($"boardgame/{viewModel.Id}").Result; messageFeedback = $"BoardGame {viewModel.Title?.ToUpper()} successfully deleted."; break; case nameof(Create): var contentCreate = new StringContent(JsonConvert.SerializeObject(viewModel), Encoding.UTF8, "application/json"); response = _client.PostAsync("boardgame", contentCreate).Result; messageFeedback = $"BoardGame { viewModel.Title?.ToUpper()} successfully created."; break; case nameof(Edit): var contentEdit = new StringContent(JsonConvert.SerializeObject(viewModel), Encoding.UTF8, "application/json"); response = _client.PutAsync($"boardgame", contentEdit).Result; messageFeedback = $"BoardGame {viewModel.Title?.ToUpper()} successfully edited."; break; } if (response.IsSuccessStatusCode) { SendFeedback(false, messageFeedback); return(RedirectToAction(nameof(Index))); } else { return(SendFeedback(response)); } } catch (System.Exception ex) { _logger.LogError(ex.Message, ex, ex.InnerException); return(SendFeedback(response)); } finally { _client.Dispose(); } }
public IActionResult Delete(BoardGameViewModel viewModel) { return(SetViewModelByActionName(nameof(Delete), viewModel)); }
public static BoardGame ToEntity(this BoardGameViewModel viewModel) { IMapper mapper = BoardGameForBoardGameViewModelConfig().CreateMapper(); return(mapper.Map <BoardGame>(viewModel)); }
public IActionResult Put([FromBody] BoardGameViewModel viewModel) { return(SetViewModelByVerbName(nameof(Put), viewModel, null)); }