예제 #1
0
        public Tuple <string, bool> SaveVote(int id, int electorateId, string candidateIds, int partyId, bool referendum)
        {
            using (var ctx = _dbContext = new votingBackendDbContext(builder.Options))
            {
                try
                {
                    UserVote user = ctx.UserVoteSet.Single <UserVote>(s => s.Id == id);

                    if (user.VoteSaved)
                    {
                        Tuple.Create("Vote Already Saved", false);
                    }

                    user.ElectorateId = electorateId;
                    user.CandidateIds = candidateIds;
                    user.PartyId      = partyId;
                    user.Referendum   = referendum;
                    user.VoteSaved    = true;

                    ctx.Entry(user).State = EntityState.Modified;
                    ctx.SaveChanges();

                    return(Tuple.Create("Vote Saved", true));
                }
                catch
                {
                    return(Tuple.Create("Unable to save vote", false));
                }
            }
        }
예제 #2
0
        public Tuple <string, bool> Register(string first, string last, string dob, string electoralId)
        {
            UserVote user = new UserVote()
            {
                FirstName   = first,
                LastName    = last,
                DoB         = DateTime.Parse(dob),
                ElectoralId = electoralId,
                VoteSaved   = false
            };

            using (var ctx = _dbContext = new votingBackendDbContext(builder.Options))
            {
                try
                {
                    ctx.UserVoteSet.Add(user);
                    ctx.SaveChanges();
                    if (user.Id >= 0)
                    {
                        return(Tuple.Create("Success", true));
                    }
                    else
                    {
                        return(Tuple.Create("Unable to register user", false));
                    }
                }
                catch
                {
                    return(Tuple.Create("Unable to register user", false));
                }
            }
        }
예제 #3
0
        public Tuple <string, bool> AddCandidate(string name, string detail, string image)
        {
            Candidate newItem = new Candidate()
            {
                Name   = name,
                Detail = detail,
                Image  = image
            };

            using (var ctx = _dbContext = new votingBackendDbContext(builder.Options))
            {
                try
                {
                    ctx.CandidateSet.Add(newItem);
                    ctx.SaveChanges();

                    if (newItem.Id >= 0)
                    {
                        return(Tuple.Create("Success", true));
                    }
                    else
                    {
                        return(Tuple.Create("Unable to register Candidate", false));
                    }
                }
                catch (Exception ex)
                {
                    return(Tuple.Create(ex.Message, false));
                }
            }
        }
예제 #4
0
        public Tuple <List <Party>, string, bool> GetParties()
        {
            List <Party> listItems = new List <Party>();

            using (var ctx = _dbContext = new votingBackendDbContext(builder.Options))
            {
                try
                {
                    var result = (from i in ctx.PartySet
                                  select new Party
                    {
                        Id = i.Id,
                        Name = i.Name,
                        Detail = i.Detail,
                        Image = i.Image
                    }).ToList();

                    if (result.Count > 0)
                    {
                        listItems = result;

                        return(Tuple.Create(listItems, "Success", true));
                    }
                    else
                    {
                        return(Tuple.Create(listItems, "Unable to get Parties", false));
                    }
                }
                catch (Exception ex)
                {
                    return(Tuple.Create(listItems, ex.Message, false));
                }
            }
        }
예제 #5
0
        public Tuple <string, bool> NewReferendum(string name, string detail, string images)
        {
            Referendum newItem = new Referendum()
            {
                Name   = name,
                Detail = detail,
                Images = images,
                Active = true
            };

            using (var ctx = _dbContext = new votingBackendDbContext(builder.Options))
            {
                try
                {
                    var update = (from r in ctx.ReferendumSet
                                  where r.Active == true
                                  select new Referendum
                    {
                        Id = r.Id,
                        Name = r.Name,
                        Detail = r.Detail,
                        Images = r.Images,
                        Active = r.Active
                    }).ToList();

                    foreach (var item in update)
                    {
                        item.Active = false;

                        ctx.Entry(item).State = EntityState.Modified;
                    }

                    ctx.ReferendumSet.Add(newItem);
                    ctx.SaveChanges();

                    if (newItem.Id >= 0)
                    {
                        return(Tuple.Create("Success", true));
                    }
                    else
                    {
                        return(Tuple.Create("Unable to register Referendum", false));
                    }
                }
                catch (Exception ex)
                {
                    return(Tuple.Create(ex.Message, false));
                }
            }
        }
예제 #6
0
        public Tuple <UserVote, string, bool> Authenticate(string first, string last, string dob, string electoral)
        {
            UserVote user = new UserVote();

            using (var ctx = _dbContext = new votingBackendDbContext(builder.Options))
            {
                try
                {
                    var result = (from u in ctx.UserVoteSet
                                  where u.FirstName == first && u.LastName == last && u.DoB == DateTime.Parse(dob) && u.ElectoralId == electoral
                                  select new UserVote
                    {
                        Id = u.Id,
                        FirstName = u.FirstName,
                        LastName = u.LastName,
                        DoB = u.DoB,
                        ElectoralId = u.ElectoralId,
                        ElectorateId = u.ElectorateId,
                        CandidateIds = u.CandidateIds,
                        PartyId = u.PartyId,
                        Referendum = u.Referendum,
                        VoteSaved = u.VoteSaved
                    }).ToList();

                    if (result.Count == 1)
                    {
                        if (result.FirstOrDefault() != null)
                        {
                            user = result.FirstOrDefault();

                            return(Tuple.Create(user, "Success", true));
                        }
                        else
                        {
                            return(Tuple.Create(user, "Invalid Login", false));
                        }
                    }
                    else
                    {
                        return(Tuple.Create(user, "Invalid Login", false));
                    }
                }
                catch (Exception ex)
                {
                    return(Tuple.Create(user, ex.Message, false));
                }
            }
        }
예제 #7
0
        public Tuple <Referendum, string, bool> GetReferendum()
        {
            Referendum listItems = new Referendum();

            using (var ctx = _dbContext = new votingBackendDbContext(builder.Options))
            {
                try
                {
                    var result = (from i in ctx.ReferendumSet
                                  where i.Active == true
                                  select new Referendum
                    {
                        Id = i.Id,
                        Name = i.Name,
                        Detail = i.Detail,
                        Images = i.Images
                    }).ToList();

                    if (result.Count != 0)
                    {
                        if (result.FirstOrDefault() != null)
                        {
                            listItems = result.FirstOrDefault();

                            return(Tuple.Create(listItems, "Success", true));
                        }
                        else
                        {
                            return(Tuple.Create(listItems, "Unable to get Referendum", false));
                        }
                    }
                    else
                    {
                        return(Tuple.Create(listItems, "Unable to get Referendum", false));
                    }
                }
                catch (Exception ex)
                {
                    return(Tuple.Create(listItems, ex.Message, false));
                }
            }
        }