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()); } }
/// <summary> /// Service Method To Check If The Elector Has Voted /// </summary> /// <param name="elector"></param> /// <returns></returns> public static async Task <bool> ElectorHasVoted(CastVoteModel elector) { using (SqlConnection dbConn = new SqlConnection(selectConnection(elector.Location))) { var isExistingUserQuery = "SELECT * from Elector WHERE ID ='" + elector.ElectorID + "' AND HasVoted = 1 "; SqlDataReader reader; try { dbConn.Open(); SqlCommand cmd = new SqlCommand(isExistingUserQuery, dbConn); reader = await cmd.ExecuteReaderAsync(); if (reader.HasRows) { return(true); } else { return(false); } } catch (Exception ex) { reader = null; ActionLogService.LogAction(new ActionLogModel() { UserID = elector.UserID, ActionPerformed = "Elector Has Voted Error : " + ex.Message, MethodName = "ElectorHasVoted", IsError = true }, elector.Location); return(false); } finally { dbConn.Close(); ActionLogService.LogAction(new ActionLogModel() { UserID = elector.UserID, ActionPerformed = "Check If Elector Has Voted ", MethodName = "ElectorHasVoted", IsError = false }, elector.Location); } } }
/// <summary> /// Service Method To Updated when the user has Voted /// </summary> /// <param name="elector"></param> /// <returns></returns> public static async Task <bool> ElectorVoted(CastVoteModel elector) { String SQL = @"UPDATE Elector SET HasVoted = 1" + " WHERE ID = '" + elector.ElectorID + "'"; using (SqlConnection dbConn = new SqlConnection(selectConnection(elector.Location))) { try { dbConn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = SQL; cmd.Connection = dbConn; await cmd.ExecuteNonQueryAsync(); dbConn.Close(); return(true); } catch (Exception ex) { ActionLogService.LogAction(new ActionLogModel() { UserID = elector.UserID, ActionPerformed = "Elector Voted Error : " + ex.Message, MethodName = "ElectorVoted", IsError = true }, elector.Location); return(false); } finally { dbConn.Close(); ActionLogService.LogAction(new ActionLogModel() { UserID = elector.UserID, ActionPerformed = "Elector Voted", MethodName = "ElectorVoted", IsError = false }, elector.Location); } } }