Beispiel #1
0
        public async Task <IActionResult> AddCandidate([FromBody] CandidateElectionRelation mydata)
        {
            //this method is called using ajax calls
            //it is used when editing/creating an election
            //it takes {voterId, electionId} and create new candidate from it and add it to db ... used when creating/editing an old election
            //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
            {
                Candidate newCandidate = _candidateBusiness.AddNewCandidate(mydata.voterId, mydata.electionId);
                //row updated successfully in the DB
                //now lets return json data so that it is understandable by jQuery
                var json = JsonConvert.SerializeObject(new
                {
                    candidateId = newCandidate.Id
                });
                return(Ok(json));
            }
            catch (DataNotUpdatedException bnu)
            {
                //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)
            {
                //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 and return 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 }));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> RemoveCandidateByElectionAndVoter([FromBody] CandidateElectionRelation mydata)
        {
            //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



            //this function removes a candidate from the db using its electionID and its voterBeing ID
            //.. used when creating an election
            try
            {
                _candidateBusiness.RemoveCandidateByElectionIdAndVoterId(mydata.electionId, mydata.voterId);

                //row updated successfully in the DB
                return(Json(new { success = true }));
            }
            catch (DataNotUpdatedException bnu)
            {
                //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)
            {
                //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 and return 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 }));
            }
        }