Esempio n. 1
0
        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());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Service Method To Get All Ballots
        /// </summary>
        /// <param name="ballot"></param>
        /// <returns></returns>
        public static async Task <List <BallotModel> > GetBallots(BallotModel ballot)
        {
            List <BallotModel> Ballots = new List <BallotModel>();

            using (SqlConnection dbConn = new SqlConnection(selectConnection(ballot.Location)))
            {
                var           Query = "SELECT * from Ballot";
                SqlDataReader reader;

                try
                {
                    dbConn.Open();
                    SqlCommand cmd = new SqlCommand(Query, dbConn);
                    reader = await cmd.ExecuteReaderAsync();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            BallotModel ballotItem = new BallotModel();
                            ballotItem.ID          = reader.GetInt32(0);
                            ballotItem.CandidateID = reader.GetInt32(1);
                            ballotItem.DistrictID  = reader.GetInt32(2);
                            ballotItem.CenterID    = reader.GetInt32(3);
                            ballotItem.Voted       = reader.GetBoolean(4);
                            ballotItem.DateCreated = reader.GetDateTime(5);

                            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 async Task SetBallotCodeAsync(BallotModel ballot)
        {
            await Task.CompletedTask;

            using (var tableAdapter = new BallotTableAdapter())
            {
                tableAdapter.SetBallotCodeQuery(ballot.Code, ballot.Id);
            }
        }
Esempio n. 4
0
        /// <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);
            }
        }
Esempio n. 5
0
        private async Task SetBallotAsync(VoterModel voter)
        {
            _ballot = await _ballotService.GetBallotAsync(_currentElection, _voter);

            tbkBallotCode.Text = _ballot.Code;
            tbkVoterID.Text    = _voter.Vin;
            tbkFullName.Text   = _ballot.EnteredAt.ToString("yyyy-MM-dd hh:mm:ss tt");

            await LoadCandidatesAsync();
        }
        public async Task SetBallotCodeAsync(BallotModel model)
        {
            using (var context = new StudentElectionContext())
            {
                var ballot = await context.Ballots.SingleOrDefaultAsync(b => b.Id == model.Id);

                ballot.Code = model.Code;

                await context.SaveChangesAsync();
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Service Method To Update A Ballot
        /// </summary>
        /// <param name="ballot"></param>
        /// <returns></returns>
        public static async Task <bool> UpdateBallot(BallotModel ballot)
        {
            String SQL = @"UPDATE Ballot SET CandidateID = '" + ballot.CandidateID + "'" +
                         " DistrictID = '" + ballot.DistrictID + "'" +
                         " CenterID = '" + ballot.CenterID + "'" +
                         " Voted = '" + ballot.Voted + "'" +
                         " WHERE ID = '" + ballot.ID + "')";

            using (SqlConnection dbConn = new SqlConnection(selectConnection(ballot.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          = ballot.UserID,
                        ActionPerformed = "Ballot Update Error : " + ex.Message,
                        MethodName      = "UpdateBallot",
                        IsError         = true
                    },
                                               ballot.Location);
                    return(false);
                }
                finally
                {
                    dbConn.Close();
                    ActionLogService.LogAction(new ActionLogModel()
                    {
                        UserID          = ballot.UserID,
                        ActionPerformed = "Ballot Updated",
                        MethodName      = "UpdateBallot",
                        IsError         = false
                    },
                                               ballot.Location);
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Service Method to Add New Ballot
        /// </summary>
        /// <param name="vote"></param>
        /// <returns></returns>
        public static async Task <bool> AddNewBallot(BallotModel vote)
        {
            var dateAdded = DateTime.Now;

            vote.Voted = true;
            String SQL = "INSERT INTO Ballot(CandidateID, DistrictID, CenterID, Voted, DateCreated)" +
                         "VALUES('" + vote.CandidateID + "','" + vote.DistrictID + "','" + vote.CenterID + "','" + vote.Voted + "',GetDate())";

            using (SqlConnection dbConn = new SqlConnection(selectConnection(vote.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          = vote.UserID,
                        ActionPerformed = "Add New Ballot Error : " + ex.Message,
                        MethodName      = "AddNewBallot",
                        IsError         = true
                    },
                                               vote.Location);
                    return(false);
                }
                finally
                {
                    dbConn.Close();
                    ActionLogService.LogAction(new ActionLogModel()
                    {
                        UserID          = vote.UserID,
                        ActionPerformed = "New Ballot Added",
                        MethodName      = "AddNewBallot",
                        IsError         = false
                    },
                                               vote.Location);
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Service Method To Check If A Ballot Exists
        /// </summary>
        /// <param name="ballot"></param>
        /// <returns></returns>
        public static async Task <bool> BallotExists(BallotModel ballot)
        {
            using (SqlConnection dbConn = new SqlConnection(selectConnection(ballot.Location)))
            {
                var           isExistingBallotQuery = "SELECT * from Ballot WHERE CandidateID ='" + ballot.CandidateID + "' AND DistrictID = '" + ballot.DistrictID + "' AND CenterID = '" + ballot.CenterID + "'";
                SqlDataReader reader;

                try
                {
                    dbConn.Open();
                    SqlCommand cmd = new SqlCommand(isExistingBallotQuery, dbConn);
                    reader = await cmd.ExecuteReaderAsync();

                    if (reader.HasRows)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    reader = null;
                    ActionLogService.LogAction(new ActionLogModel()
                    {
                        UserID          = ballot.UserID,
                        ActionPerformed = "Ballot Exists Error : " + ex.Message,
                        MethodName      = "BallotExists",
                        IsError         = true
                    },
                                               ballot.Location);
                    return(false);
                }
                finally
                {
                    dbConn.Close();
                    ActionLogService.LogAction(new ActionLogModel()
                    {
                        UserID          = ballot.UserID,
                        ActionPerformed = "Check If Ballot Exists ",
                        MethodName      = "BallotExists",
                        IsError         = false
                    },
                                               ballot.Location);
                }
            }
        }
        public async Task <BallotModel> GetBallotAsync(int ballotId)
        {
            using (var context = new StudentElectionContext())
            {
                var ballot = await context.Ballots.SingleOrDefaultAsync(b => b.Id == ballotId);

                if (ballot == null)
                {
                    return(null);
                }

                var model = new BallotModel();
                _mapper.Map(ballot, model);

                return(model);
            }
        }
        public async Task <BallotModel> GetBallotByVinAsync(int electionId, string vin)
        {
            using (var context = new StudentElectionContext())
            {
                var ballot = await context.Ballots
                             .SingleOrDefaultAsync(b => b.Voter.ElectionId == electionId && b.Voter.Vin == vin);

                if (ballot == null)
                {
                    return(null);
                }

                var model = new BallotModel();
                _mapper.Map(ballot, model);

                return(model);
            }
        }
        public async Task <BallotModel> GetBallotByVinAsync(int electionId, string vin)
        {
            await Task.CompletedTask;

            using (var tableAdapter = new BallotTableAdapter())
            {
                var row = tableAdapter.GetBallotsByVin(electionId, vin).SingleOrDefault();
                if (row == null)
                {
                    return(null);
                }

                var model = new BallotModel();
                _mapper.Map(row, model);

                return(model);
            }
        }
        public async Task <BallotModel> GetBallotAsync(int ballotId)
        {
            await Task.CompletedTask;

            using (var tableAdapter = new BallotTableAdapter())
            {
                var row = tableAdapter.GetBallotsById(ballotId).SingleOrDefault();
                if (row == null)
                {
                    return(null);
                }

                var model = new BallotModel();
                _mapper.Map(row, model);

                return(model);
            }
        }
Esempio n. 14
0
        public async Task <IHttpActionResult> GetTotalNoOfVotes(BallotModel vote)
        {
            if (await AuthService.ValidateUserAndToken(vote.Token, vote.UserID, vote.Email, vote.Location))
            {
                var votes = await BallotService.GetTotalVotes(vote);

                if (votes.Count > 0)
                {
                    return(Ok(votes));
                }
                else
                {
                    return(BadRequest("No Votes Exists!"));
                }
            }
            else
            {
                return(Unauthorized());
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Maps the suffrage to bulletin.
        /// </summary>
        /// <param name="suffrages">The suffrages.</param>
        /// <returns></returns>
        private static IEnumerable <BallotModel> MapSuffrageToBulletin(List <Suffrage> suffrages)
        {
            var bulletins = new List <BallotModel>();

            foreach (var suffrage in suffrages)
            {
                var bulletin = new BallotModel();
                foreach (var acte in suffrage.Act)
                {
                    bulletin.Acts.Add(new ActModel
                    {
                        IdOption = acte.VotingProcessOption.IdOption,
                        Value    = acte.Value
                    });
                }
                bulletins.Add(bulletin);
            }

            return(bulletins);
        }
Esempio n. 16
0
        public async Task <IHttpActionResult> GetBallots(BallotModel ballot)
        {
            if (await AuthService.ValidateUserAndToken(ballot.Token, ballot.UserID, ballot.Email, ballot.Location))
            {
                var ballots = await BallotService.GetBallots(ballot);

                if (ballots.Count > 0)
                {
                    return(Ok(ballots));
                }
                else
                {
                    return(BadRequest("No Ballots Exists!"));
                }
            }
            else
            {
                return(Unauthorized());
            }
        }
Esempio n. 17
0
        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());
            }
        }
Esempio n. 18
0
        public async Task <IHttpActionResult> UpdateBallot(BallotModel ballot)
        {
            if (ballot == null)
            {
                return(BadRequest("Please provide valid inputs!"));
            }

            if (ballot.ID == 0)
            {
                return(BadRequest("Please provide valid ballot 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))
                {
                    if (await BallotService.UpdateBallot(ballot))
                    {
                        return(Ok("Ballot Updated Successfully!"));
                    }
                    else
                    {
                        return(BadRequest("Failed To Update Ballot!"));
                    }
                }
                else
                {
                    return(BadRequest("No Such Ballot Exists!"));
                }
            }
            else
            {
                return(Unauthorized());
            }
        }
 private string GetBallotCode(string serverTag, BallotModel ballot)
 {
     return(string.Format("{0}{1:000000}", serverTag, ballot.Id));
 }