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.
            }
        }
Exemple #2
0
        public async Task <IActionResult> GetCandidatesList_byElectionId([FromBody] string 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



            try
            {
                if (String.IsNullOrEmpty(electionId))
                {
                    throw new BusinessException(_messagesLoclizer["electionId cannot be null."]);
                }
                Election e = _electionBusiness.GetById(Guid.Parse(electionId));
                if (e == null)
                {
                    throw new BusinessException(_messagesLoclizer["Election is not found."]);
                }

                //lets serialize the list of candidates of the election we've got and send it back as a reponse
                //note that I didn't retrieve candidates as they are, I selected only needed attributes bcuz when i tried serializing
                //candidates objects as they are I got this error "self referencing loop detected with type" it means json tried to serialize the candidate object
                //but it found that each candidate has an Election object, and this election object has a list of candidates and so on, so i excluded election
                //from the selection to avoid the infinite loop

                var candidates = _candidateBusiness.GetCandidate_byElection(e);
                if (candidates == null)
                {
                    throw new BusinessException(_messagesLoclizer["Candidates List not found."]);
                }

                var json = JsonConvert.SerializeObject(_candidateBusiness.ConvertCandidateList_ToCandidateViewModelList(candidates));
                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 the response returned is an ERROR, and jQuery ajax will understand that.
                HttpContext.Response.StatusCode = 500;
                return(Json(new { Message = E.Message }));
            }
        }