Exemplo n.º 1
0
        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));
            }
        }
Exemplo n.º 2
0
        public IActionResult Edit(BoardGameViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                return(SetViewModelByActionName(nameof(Edit), viewModel));
            }

            return(View(viewModel));
        }
Exemplo n.º 3
0
        public void MaxPlayersWithMinPlayersPasses(int minPlayers, int maxPlayers)
        {
            var boardGame = new BoardGameViewModel
            {
                MinPlayers = minPlayers,
                MaxPlayers = maxPlayers
            };

            _boardGameValidator.ShouldNotHaveValidationErrorFor(x => x.MaxPlayers, boardGame);
        }
Exemplo n.º 4
0
        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)));
        }
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
        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();
            }
        }
Exemplo n.º 8
0
 public IActionResult Delete(BoardGameViewModel viewModel)
 {
     return(SetViewModelByActionName(nameof(Delete), viewModel));
 }
Exemplo n.º 9
0
        public static BoardGame ToEntity(this BoardGameViewModel viewModel)
        {
            IMapper mapper = BoardGameForBoardGameViewModelConfig().CreateMapper();

            return(mapper.Map <BoardGame>(viewModel));
        }
Exemplo n.º 10
0
 public IActionResult Put([FromBody] BoardGameViewModel viewModel)
 {
     return(SetViewModelByVerbName(nameof(Put), viewModel, null));
 }