public IActionResult Edit(Guid Id) { _logger.LogInformation("Voter/Edit() action is called"); try { //In here we are going to return a view where a voter is displayed with his state but the state is in //a list of states if (Id == null) { _logger.LogError("Passed parameter 'id' is null"); throw new BusinessException(_messagesLoclizer["Passed parameter 'id' can not be null"]); } _logger.LogInformation("Calling VoterRepository.GetById() method"); var voter = _voterBusiness.GetById(Id); if (voter == null) { _logger.LogError("Voter not found"); throw new BusinessException(_messagesLoclizer["Voter not found"]); } _logger.LogInformation("Creating a VoterStateViewModel for the Voter instance"); VoterStateViewModel voterstate = new VoterStateViewModel { Id = voter.Id, FirstName = voter.FirstName, LastName = voter.LastName, States = _stateBusiness.GetAll() }; /*just in case user wanted to edit info of Neutral vote which doesn't have a state*/ if (voter.State != null) { voterstate.StateID = voter.State.Id; } _logger.LogInformation("Returning VoterStateViewModel to the View"); return(View(voterstate)); } catch (BusinessException be) { _logger.LogError(be.Message); //lets now create a suitable message for the user and store it inside a ViewBag (which is a Dynamic Object we can fill it //by whatever we want BusinessMessage bm = new BusinessMessage("Error", be.Message); ViewBag.BusinessMessage = bm; return(View()); } catch (Exception E) { _logger.LogError("Exception, " + E.Message); throw E; } }
public IActionResult Create() { try { //this method will return an empty VoterStateViewModel but with a list of all states, in a view VoterStateViewModel vs = new VoterStateViewModel { States = _stateBusiness.GetAll() }; return(View(vs)); } catch (Exception E) { throw E; } }
public async Task AddNewVoter(VoterStateViewModel vs) { try { //this method receives a VoterStateViewModel object, and based on it, it creates a voter object and stores it in the DB //after adding his corresponding user //_logger.LogInformation("Creating a new Voter instance"); Voter v = new Voter { Id = Guid.NewGuid(), FirstName = vs.FirstName, LastName = vs.LastName, StateId = vs.StateID }; //lets add the new user and use its ID in our new voter instance v.UserId = await AddNewUser_FromNewVoter(v); //_logger.LogInformation("Adding the new voter to the DB"); int updatedRows = Add(v); if (updatedRows > 0) { //row updated successfully in the DB //_logger.LogInformation("The new voter is added to the DB successfully"); } else { //row not updated in the DB throw new DataNotUpdatedException(_messagesLocalizer["Data not updated, operation failed."]); } } catch (BusinessException E) { throw E; } catch (DataNotUpdatedException E) { throw E; } catch (Exception E) { throw E; } }
public async Task <IActionResult> Create(VoterStateViewModel vs) { _logger.LogInformation("Voter/Create() action is called"); try { if (ModelState.IsValid) { _logger.LogInformation("Model is valid"); await _voterBusiness.AddNewVoter(vs); //row updated successfully in the DB _logger.LogInformation("The new voter is added to the DB successfully"); _logger.LogInformation("Redirecting to the Voter Index view"); return(RedirectToAction(nameof(Index))); } _logger.LogInformation("Model is not valid"); //so there is a business rule not met, lets throw a businessException and catch it throw new BusinessException(_messagesLoclizer["Information provided not valid"]); } catch (DataNotUpdatedException bnu) { _logger.LogError(bnu.Message); //lets now create a suitable message for the user and store it inside a ViewBag (which is a Dynamic Object we can fill it //by whatever we want BusinessMessage bm = new BusinessMessage("Error", bnu.Message); ViewBag.BusinessMessage = bm; return(View()); } catch (BusinessException be) { _logger.LogError(be.Message); //lets now create a suitable message for the user and store it inside a ViewBag (which is a Dynamic Object we can fill it //by whatever we want BusinessMessage bm = new BusinessMessage("Error", be.Message); ViewBag.BusinessMessage = bm; return(View(vs)); } catch (Exception E) { _logger.LogError("Exception, " + E.Message); throw E; } }
public IActionResult Edit(VoterStateViewModel voterstate) { _logger.LogInformation("Voter/Edit() action is called"); try { if (!ModelState.IsValid) { _logger.LogError("Model is not valid"); if (voterstate.States == null) { //in caase the object received doesn't have a list of states voterstate.States = _stateBusiness.GetAll(); } _logger.LogInformation("Returning to the view to display validation messages"); //so there is a business rule not met, lets throw a businessException and catch it throw new BusinessException(_messagesLoclizer["Information provided not valid"]); } Voter v = new Voter { Id = voterstate.Id, FirstName = voterstate.FirstName, LastName = voterstate.LastName, StateId = voterstate.StateID }; _logger.LogInformation("Calling VoterRepository.Edit() method"); int updatedRows = _voterBusiness.Edit(voterstate.Id, v); if (updatedRows > 0) { //row updated successfully in the DB _logger.LogInformation("Redirecting to Index action"); return(RedirectToAction(nameof(Index))); } else { //row not updated in the DB throw new DataNotUpdatedException(_messagesLoclizer["Data not updated, operation failed."]); } } catch (DataNotUpdatedException bnu) { _logger.LogError(bnu.Message); //lets now create a suitable message for the user and store it inside a ViewBag (which is a Dynamic Object we can fill it //by whatever we want BusinessMessage bm = new BusinessMessage("Error", bnu.Message); ViewBag.BusinessMessage = bm; return(View()); } catch (BusinessException be) { _logger.LogError(be.Message); //lets now create a suitable message for the user and store it inside a ViewBag (which is a Dynamic Object we can fill it //by whatever we want BusinessMessage bm = new BusinessMessage("Error", be.Message); ViewBag.BusinessMessage = bm; //lets refill States list voterstate.States = _stateBusiness.GetAll(); return(View(voterstate)); } catch (Exception E) { _logger.LogError("Exception, " + E.Message); throw E; } }