Ejemplo n.º 1
0
        public ActionResult Details(Guid id)
        {
            try
            {
                if (id == null)
                {
                    throw new BusinessException(_messagesLoclizer["Passed parameter 'id' can not be null"]);
                }

                Election e = _electionBusiness.GetById(id);
                if (e == null)
                {
                    throw new BusinessException(_messagesLoclizer["Election not found"]);
                }

                return(View(_electionBusiness.ConvertElection_ToElectionViewModel(e)));
            }
            catch (BusinessException be)
            {
                //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)
            {
                throw E;
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetResultsOfElection([FromBody] Guid electionId)
        {
            //this method is called using ajax calls
            //so if a business rule is not met we'll throw a businessException and catch it to create and internal server error and return its msg
            //as json


            _logger.LogInformation("Method Home/GetResultsOfElection() is called");
            try
            {
                if (electionId == null || electionId == Guid.Empty)
                {
                    throw new BusinessException(_messagesLoclizer["electionId can not be null."]);
                }

                //this method returns a list of candidates (of an election) ordered by their number of votes
                var election = _electionBusiness.GetById(electionId);
                if (election == null)
                {
                    throw new BusinessException(_messagesLoclizer["Election is not found."]);
                }

                _logger.LogInformation("Calling method CandidateBusiness.GetCandidate_byElection()");

                var candidates = _candidateBusiness.GetCandidate_byElection(election);
                _logger.LogInformation("Calling _candidateBusiness.ConvertCandidateList_ToCandidateViewModelList() method");

                List <CandidateViewModel> candidatesViewModel = _candidateBusiness.ConvertCandidateList_ToCandidateViewModelList(candidates);

                //lets serialize the list of candidatesviewmodel as json object
                JsonSerializerSettings settings = new JsonSerializerSettings {
                    DateFormatString = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
                };
                var json = JsonConvert.SerializeObject(candidatesViewModel.OrderByDescending(c => c.VotesCount), settings);
                return(Ok(json));
            }
            catch (BusinessException be)
            {
                //lets create an internal server error so that the response returned is an ERROR, and jQuery ajax will understand that.
                HttpContext.Response.StatusCode = 500;
                return(Json(new { Message = be.Message }));
            }
            catch (Exception E)
            {
                //lets create an internal server error so that jquery ajax understand that there was an error
                HttpContext.Response.StatusCode = 500;
                //leets log the exception msg to the console window
                _logger.LogError("Exception, " + E.Message);
                return(Json(new { Message = E.Message }));
                //In above code I created an internal server error so that the response returned is an ERROR, and jQuery ajax will understand that.
            }
        }