/// <summary> /// Service Method To Get Total No Of Votes /// </summary> /// <param name="ballot"></param> /// <returns></returns> public static async Task <List <TotalBallotModel> > GetTotalVotes(BallotModel ballot) { List <TotalBallotModel> Ballots = new List <TotalBallotModel>(); using (SqlConnection dbConn = new SqlConnection(selectConnection(ballot.Location))) { var Query = "SELECT CandidateID, COUNT(Voted) AS NoOFVotes from Ballot group by CandidateID order by NoOFVotes desc"; SqlDataReader reader; try { dbConn.Open(); SqlCommand cmd = new SqlCommand(Query, dbConn); reader = await cmd.ExecuteReaderAsync(); if (reader.HasRows) { while (reader.Read()) { TotalBallotModel ballotItem = new TotalBallotModel(); ballotItem.CandidateID = reader.GetInt32(0); ballotItem.TotalNoOfVotes = reader.GetInt32(1); // ballotItem.DateTallied = DateTime.Now; Ballots.Add(ballotItem); } } } catch (Exception ex) { reader = null; ActionLogService.LogAction(new ActionLogModel() { UserID = ballot.UserID, ActionPerformed = "Ballots Error : " + ex.Message, MethodName = "GetBallots", IsError = true }, ballot.Location); } finally { dbConn.Close(); ActionLogService.LogAction(new ActionLogModel() { UserID = ballot.UserID, ActionPerformed = "Get All Existing Ballots ", MethodName = "GetBallots", IsError = false }, ballot.Location); } return(Ballots); } }
// public static string conString = selectConnection("colombo"); /// <summary> /// Service Method To Get The Total Ballot Count /// </summary> /// <param name="authBaseModel"></param> /// <returns></returns> public static async Task <List <TotalBallotModel> > GetTotalBallotCount(AuthBaseModel authBaseModel) { List <TotalBallotModel> TotalBallots = new List <TotalBallotModel>(); using (SqlConnection dbConn = new SqlConnection(selectConnection(authBaseModel.Location))) { var Query = "SELECT CandidateID, COUNT(Voted) AS TotalNoOfVotes FROM Ballot WHERE Voted = 1 GROUP BY CandidateID ORDER BY TotalNoOfVotes DESC"; SqlDataReader reader; try { dbConn.Open(); SqlCommand cmd = new SqlCommand(Query, dbConn); reader = await cmd.ExecuteReaderAsync(); if (reader.HasRows) { while (reader.Read()) { TotalBallotModel Ballots = new TotalBallotModel(); Ballots.CandidateID = reader.GetInt32(0); Ballots.TotalNoOfVotes = reader.GetInt32(1); Ballots.DateTallied = DateTime.Now; TotalBallots.Add(Ballots); } } } catch (Exception ex) { reader = null; ActionLogService.LogAction(new ActionLogModel() { UserID = authBaseModel.UserID, ActionPerformed = "Get Total Ballot Count Error : " + ex.Message, MethodName = "GetTotalBallotCount", IsError = true }, authBaseModel.Location); } finally { dbConn.Close(); ActionLogService.LogAction(new ActionLogModel() { UserID = authBaseModel.UserID, ActionPerformed = "Get Total Ballot Count For All Candidates", MethodName = "GetTotalBallotCount", IsError = false }, authBaseModel.Location); } return(TotalBallots); } }
/// <summary> /// Service Method To Get Consolidated Votes /// </summary> /// <param name="loggedInUser"></param> /// <returns></returns> public static List <CandidateModel> ConsolidateVotes(AuthBaseModel loggedInUser) { List <CandidateModel> Candidates = GetCandidates(loggedInUser.Location, loggedInUser.UserID); foreach (ConnectionStringSettings c in System.Configuration.ConfigurationManager.ConnectionStrings) { //use c.Name if (c.Name != "LocalSqlServer" && c.Name != "main") { using (SqlConnection dbConn = new SqlConnection(selectConnection(c.Name))) { var Query = "SELECT CandidateID, COUNT(Voted) AS TotalNoOfVotes FROM Ballot WHERE Voted = 1 GROUP BY CandidateID ORDER BY CandidateID"; SqlDataReader reader; try { dbConn.Open(); SqlCommand cmd = new SqlCommand(Query, dbConn); reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { TotalBallotModel votedCandidate = new TotalBallotModel(); votedCandidate.CandidateID = reader.GetInt32(0); votedCandidate.TotalNoOfVotes = reader.GetInt32(1); votedCandidate.DateTallied = DateTime.Now; foreach (var candidate in Candidates) { if (candidate.ID == votedCandidate.CandidateID) { candidate.Votes = votedCandidate.TotalNoOfVotes; } } } } } catch (Exception ex) { reader = null; ActionLogService.LogAction(new ActionLogModel() { UserID = loggedInUser.UserID, ActionPerformed = " Error In Consolidating Votes : " + ex.Message, MethodName = "ConsolidateVotes", IsError = true }, loggedInUser.Location); } finally { dbConn.Close(); ActionLogService.LogAction(new ActionLogModel() { UserID = loggedInUser.UserID, ActionPerformed = "Consolidate Votes", MethodName = "ConsolidateVotes", IsError = false }, loggedInUser.Location); } } } } return(Candidates); }