public async Task <IHttpActionResult> CastVote(CastVoteModel vote) { if (vote == null) { return(BadRequest("Please provide valid inputs!")); } if (vote.ElectorID == 0) { return(BadRequest("Please provide valid elector ID!")); } if (vote.CandidateID == 0) { return(BadRequest("Please provide valid candidate ID!")); } if (string.IsNullOrEmpty(vote.Location)) { return(BadRequest("Please provide valid location!")); } if (await AuthService.ValidateUserAndToken(vote.Token, vote.UserID, vote.Email, vote.Location)) { if (await ElectorService.ElectorHasVoted(vote)) { return(BadRequest("Elector has already voted!")); } else { BallotModel newVote = new BallotModel() { CandidateID = vote.CandidateID, DistrictID = vote.DistrictID, CenterID = vote.CenterID, Location = vote.Location }; if (await BallotService.AddNewBallot(newVote)) { if (await ElectorService.ElectorVoted(vote)) { return(Ok("Vote Casted Successfully!")); } else { return(BadRequest("Error In Casting The Vote!")); } } else { return(BadRequest("Error In Casting The Vote!")); } } } else { return(Unauthorized()); } }
public async Task <IHttpActionResult> AddNewBallot(BallotModel ballot) { if (ballot == null) { return(BadRequest("Please provide valid inputs!")); } if (ballot.CandidateID == 0) { return(BadRequest("Please provide valid candidate ID!")); } if (string.IsNullOrEmpty(ballot.Location)) { return(BadRequest("Please provide valid location!")); } if (await AuthService.ValidateUserAndToken(ballot.Token, ballot.UserID, ballot.Email, ballot.Location)) { if (await BallotService.BallotExists(ballot)) { return(BadRequest("Ballot Already Exists")); } else { if (await BallotService.AddNewBallot(ballot)) { return(Ok("Ballot Added Successfully!")); } else { return(BadRequest("Ballot Adding Failed!")); } } } else { return(Unauthorized()); } }