public OperationResult<IEnumerable<PetitionLevel>> GetPetitionLevels()
        {
            OperationResult<IEnumerable<PetitionLevel>> result;

            try
            {
                using (var db = new EDEntities())
                {
                    var levels = db.PetitionLevels.ToList();
                    result = OperationResult<IEnumerable<PetitionLevel>>.Success(levels.Select(l => new PetitionLevel(l)));
                }
            }
            catch (Exception exc)
            {
                result = OperationResult<IEnumerable<PetitionLevel>>.ExceptionResult(exc);
            }

            return result;
        }
Ejemplo n.º 2
0
        public OperationResult ClearVotes()
        {
            OperationResult result;

            try
            {
                using (var db = new EDEntities())
                {
                    db.AgreementVotes.RemoveRange(db.AgreementVotes);
                    db.SaveChanges();
                    result = OperationResult.Success(1, "All agreement votes has beed deleted.");
                }
            }
            catch (Exception exception)
            {
                result = OperationResult.ExceptionResult(exception);
            }

            return result;
        }
Ejemplo n.º 3
0
        public OperationResult ClearVote(int agreementID)
        {
            OperationResult result;

            try
            {
                using (var db = new EDEntities())
                {
                    db.AgreementVotes.RemoveRange(db.AgreementVotes.Where(v => v.AgreementID == agreementID));
                    db.SaveChanges();
                    result = OperationResult.Success(1, string.Format("Agreement votes for #{0} has beed deleted.", agreementID));
                }
            }
            catch (Exception exception)
            {
                result = OperationResult.ExceptionResult(exception);
            }

            return result;
        }
Ejemplo n.º 4
0
        public clientEntities.Agreement GetAgreement(int agreementID)
        {
            //return clientEntities.Agreement.GetAgreement(agreementID);
            using (var db = new EDEntities())
            {
                var result = from p in db.Agreements
                             where p.ID == agreementID
                             select p;

                var first = result.FirstOrDefault();
                if (first == default(Agreement))
                {
                    throw new ApplicationException("Agreement not found {ID = " + agreementID.ToString() + "}");
                }

                var votes = (from p in db.AgreementVotes
                             where p.AgreementID == first.ID
                             select p);

                clientEntities.Agreement agreement = new clientEntities.Agreement(first) { NumberOfVotes = votes.Count() };
                return agreement;
            }
        }
        public OperationResult<Model.BusinessEntities.PetitionEmailVote> ConfirmEmailVoteRequest(string hash)
        {
            OperationResult<Model.BusinessEntities.PetitionEmailVote> result;

            try
            {
                using (var db = new EDEntities())
                {
                    var emailVote = db.PetitionEmailVotes.SingleOrDefault(p => p.Hash == hash);

                    if (emailVote == default(PetitionEmailVote))
                    {
                        result = OperationResult<Model.BusinessEntities.PetitionEmailVote>.Fail(-3, "Петиція не знайдена.");
                        return result;
                    }

                    if (emailVote.IsConfirmed)
                    {
                        result = OperationResult<Model.BusinessEntities.PetitionEmailVote>.Fail(-2, "Ви вже проголосували за цю петицію.");
                        return result;
                    }

                    emailVote.IsConfirmed = true;
                    db.SaveChanges();

                    var emailVoteBusiness = new Model.BusinessEntities.PetitionEmailVote(emailVote);
                    emailVoteBusiness.Petition = new Model.BusinessEntities.Petition(db.Petitions.SingleOrDefault(p => p.ID == emailVote.PetitionID));
                    result = OperationResult<Model.BusinessEntities.PetitionEmailVote>.Success(1, "Ви успішно проголосували за петицію.", emailVoteBusiness);
                }
            }
            catch (Exception ex)
            {
                result = OperationResult<Model.BusinessEntities.PetitionEmailVote>.Fail(-1, ex.Message);
            }

            return result;
        }
Ejemplo n.º 6
0
        public OperationResult<IEnumerable<Entity>> GetPetitionCategories()
        {
            OperationResult<IEnumerable<Entity>> result;

            try
            {
                using (var db = new EDEntities())
                {
                    var petitionCategoryEntityGroup = db.EntityGroups.SingleOrDefault(g => g.Name == "Category");
                    var categories = db.Entities
                        .Where(e => e.EntityGroup.ID == petitionCategoryEntityGroup.ID)
                        //.Include("EntityGroup")
                        .ToList();

                    result = OperationResult<IEnumerable<Entity>>.Success(categories.Select(c => new Entity(c)));
                }
            }
            catch (Exception exc)
            {
                result = OperationResult<IEnumerable<Entity>>.ExceptionResult(exc);
            }

            return result;
        }
Ejemplo n.º 7
0
        private List<clientEntities.Agreement> GetAgreements()
        {
            var list = new List<clientEntities.Agreement>();

            using (var db = new EDEntities())
            {
                var result = from p in db.Agreements
                             select p;

                foreach (var item in result)
                {
                    list.Add(new clientEntities.Agreement(item) { NumberOfVotes = db.AgreementVotes.Count(v => v.AgreementID == item.ID) });
                }
            }

            return list;
        }
Ejemplo n.º 8
0
        //public AgreementVoteResponse Vote(Entities.Agreement agreement, Entities.Participant participant, string agreementText, string agreementHash, string signatureHash, string certificateThumbPrint, int issuer)
        //{
        //using (var db = new DemocracyEntities())
        //{
        //    var result = from p in db.AgreementVotes
        //                 where (p.AgreementID == agreement.ID || p.AgreementHash == agreementHash)
        //                    && (p.ParticipantID == participant.ID || p.SignatureHash == signatureHash || p.CertificateThumbPrint == certificateThumbPrint)
        //                 select p;
        //    if (result.Any())
        //    {
        //        AgreementVoteResponse avr = new AgreementVoteResponse();
        //        avr.ParticipantHasAlreadyVoted();
        //        return avr;
        //    }
        //    using (TransactionScope scope = new TransactionScope())
        //    {
        //        var participantID = SaveParticipant(participant, db);
        //        participant.ID = participantID;
        //        AgreementVote av = new AgreementVote()
        //                                {
        //                                    AgreementID = agreement.ID,
        //                                    ParticipantID = participant.ID,
        //                                    AgreementText = agreementText,
        //                                    AgreementHash = agreementHash,
        //                                    SignatureHash = signatureHash,
        //                                    CertificateThumbPrint = certificateThumbPrint,
        //                                    Issuer = issuer,
        //                                    CreatedDate = DateTime.UtcNow
        //                                };
        //        db.AgreementVotes.Add(av);
        //        db.SaveChanges();
        //        scope.Complete();
        //        AgreementVoteResponse avr = new AgreementVoteResponse();
        //        avr.SuccessVote();
        //        return avr;
        //    }
        //}
        //}
        //private Entities.Participant GetUnknownUser()
        //{
        //    return new Entities.Participant
        //           {
        //               FirstName = "Unknown",
        //               MiddleName = "Unknown",
        //               LastName = "Unknown",
        //               DOB = new DateTime(1991, 8, 24),
        //               Passport = "Unknown",
        //               TaxID = -1,
        //               adress = new Entities.Adress { City = "Kyiv", Country = "Ukraine", Street = "Khreshchatyk", ZIPCode = "03001" },
        //               contact = new Entities.Contact { Phone = "+380441111111", email = "*****@*****.**" }
        //           };
        //}
        ///// <summary>Get users by TAX ID or passport.</summary>
        ///// <remarks>This method has been created to workaround Roslyn bug</remarks>
        //private IEnumerable<Participant> GetParticipants(DemocracyEntities db, long? taxID, string passport)
        //{
        //    var participants = new List<Participant>();
        //    foreach (var participant in db.Participants)
        //    {
        //        if (participant.TaxID == taxID || participant.Passport.ToUpper() == passport.ToUpper())
        //        {
        //            participants.Add(participant);
        //        }
        //    }
        //    return participants;
        //}
        public OperationResult Vote(AgreementVote vote)
        {
            OperationResult result;

            using (var db = new EDEntities())
            {
                var certificate = db.Certificates.SingleOrDefault(c => c.SerialNumber == vote.Certificate.SerialNumber);
                if (certificate == null)
                {
                    result = OperationResult.Fail(-4, "Certificate is not registered.");
                    return result;
                }

                vote.CertificateID = certificate.ID;
                vote.PersonID = certificate.PersonID;
                vote.CreatedDate = DateTime.Now;

                vote.Agreement = null;
                vote.Certificate = null;
                vote.Person = null;

                //var votes = (from p in db.AgreementVotes
                //			 where p.AgreementID == vote.AgreementID
                //				&& (p.PersonID == vote.PersonID || p.CertificateID == vote.CertificateID || p.Certificate.SerialNumber == vote.Certificate.SerialNumber)
                //			 select p);

                //if (votes.Any())
                //	throw new ApplicationException("The agreement was voted");

                db.AgreementVotes.Add(vote);
                db.SaveChanges();

                result = OperationResult.Success(1, "Agreement vote has been registered.");
            }

            return result;
        }
Ejemplo n.º 9
0
 internal Person GetAnonymousUser(EDEntities db)
 {
     var anonymousUser = db.People.SingleOrDefault(u => u.Login == "testuser") ?? new Person();
     return anonymousUser;
 }
Ejemplo n.º 10
0
        public OperationResult Vote(ClientPetitionVote petitionVote, string certificateSerialNumber)
        {
            var vote = new PetitionVote()
            {
                PetitionID = petitionVote.PetitionID,
                Certificate = new Certificate() { SerialNumber = certificateSerialNumber },
                SignedData = petitionVote.SignedData,
                SignedHash = petitionVote.Signature
            };

            OperationResult result;

            using (var db = new EDEntities())
            {
                var certificate = db.Certificates.SingleOrDefault(c => c.SerialNumber == vote.Certificate.SerialNumber);
                if (certificate == null)
                {
                    result = OperationResult.Fail(-4, "Certificate is not registered.");
                    return result;
                }

                vote.CertificateID = certificate.ID;
                vote.PersonID = certificate.PersonID;
                vote.CreatedDate = DateTime.Now;

                vote.Petition = null;
                vote.Certificate = null;
                vote.Person = null;

                ////var votes = (from p in db.PetitionVotes
                ////			 where p.PetitionID == vote.PetitionID
                ////				&& (p.PersonID == vote.PersonID || p.CertificateID == vote.CertificateID || p.Certificate.SerialNumber == vote.Certificate.SerialNumber)
                ////			 select p);
                //var votes = db.PetitionVotes.Where(p =>
                //	p.PetitionID == vote.PetitionID &&
                //	(p.PersonID == vote.PersonID || p.CertificateID == vote.CertificateID ||
                //	 p.Certificate.SerialNumber == vote.Certificate.SerialNumber));

                //if (votes.Any())
                //{
                //	result = OperationResult.Fail(-5, "You already voted for this petition.");
                //	return result;
                //}

                db.PetitionVotes.Add(vote);
                db.SaveChanges();

                result = OperationResult.Success(1, "Agreement vote has been registered.");
            }

            return result;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Create new petitions.
        /// </summary>
        /// <param name="newPetition"></param>
        /// <returns></returns>
        public OperationResult<clientEntities.Petition> AddNewPetition(clientEntities.Petition newPetition)
        {
            OperationResult<clientEntities.Petition> result;

            try
            {
                using (var db = new EDEntities())
                {
                    var script = string.Empty;
                    db.Database.Log = s => script += s;

                    // TODO: add petition already exists check
                    var petition =
                        new Petition()
                        {
                            Subject = newPetition.Subject,
                            Text = newPetition.Text,
                            Requirements = newPetition.Requirements,
                            KeyWords = newPetition.KeyWordsAsSingleString(),
                            EffectiveFrom = newPetition.EffectiveFrom == default(DateTime) ? DateTime.Now : newPetition.EffectiveFrom,
                            EffectiveTo = newPetition.EffectiveTo == default(DateTime) ? DateTime.Now.AddDays(7) : newPetition.EffectiveTo,
                            CreatedDate = DateTime.Now,
                            Limit = newPetition.Limit,
                            AddressedTo = newPetition.AddressedTo,
                            Email = newPetition.Email
                        };

                    // CreatedBy
                    var creator = db.People.SingleOrDefault(p => p.Login == newPetition.CreatedBy.Login) ?? this.GetAnonymousUser(db);
                    petition.CreatedBy = creator.ID;
                    petition.Person = null;

                    // Category
                    if (newPetition.Category == null)
                    {
                        result = OperationResult<clientEntities.Petition>.Fail(-2, "Unable to get any category info.");
                        return result;
                    }

                    var petitionCategory = db.Entities.SingleOrDefault(c => c.Name == newPetition.Category.Name);
                    if (petitionCategory == null)
                    {
                        result = OperationResult<clientEntities.Petition>.Fail(-2, string.Format("Unknown petition category - {0}.", newPetition.Category.Name));
                        return result;
                    }
                    else
                    {
                        petition.CategoryID = petitionCategory.ID;
                        petition.Category = null;
                    }

                    // Level
                    if (newPetition.Level == null)
                    {
                        result = OperationResult<clientEntities.Petition>.Fail(-3, "Unable to get any petition level info.");
                        return result;
                    }

                    var level = db.PetitionLevels.SingleOrDefault(l => l.ID == newPetition.Level.ID);
                    if (level == null)
                    {
                        result = OperationResult<clientEntities.Petition>.Fail(-3, "Unknown petition level.");
                        return result;
                    }
                    else
                    {
                        petition.LevelID = level.ID;
                        petition.PetitionLevel = null;
                    }

                    var addedPetition = db.Petitions.Add(petition);
                    db.SaveChanges();

                    result = OperationResult<clientEntities.Petition>.Success(
                        1,
                        "The petition has successfully been created.",
                        new clientEntities.Petition(addedPetition));
                }
            }
            catch (Exception exc)
            {
                result = OperationResult<clientEntities.Petition>.ExceptionResult(exc);
            }

            return result;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Search petition by specific word.
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public OperationResult<IEnumerable<clientEntities.Petition>> Search(string text)
        {
            OperationResult<IEnumerable<clientEntities.Petition>> result;

            var script = string.Empty;

            try
            {
                using (var db = new EDEntities())
                {

                    db.Database.Log = s => script += s;
                    var petitions = from p in db.Petitions
                                    where p.KeyWords.ToUpper().Contains(text.ToUpper())
                                       || p.Requirements.ToUpper().Contains(text.ToUpper())
                                       || p.Subject.ToUpper().Contains(text.ToUpper())
                                       || p.Text.ToUpper().Contains(text.ToUpper())
                                    select p;

                    var clientPetitions = this.GetPetitionsUnderLimit(db, petitions);
                    result = OperationResult<IEnumerable<clientEntities.Petition>>.Success(clientPetitions);
                }
            }
            catch (Exception exc)
            {
                result = OperationResult<IEnumerable<clientEntities.Petition>>.ExceptionResult(exc);
            }

            return result;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Search petition by specific tag.
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public OperationResult<IEnumerable<clientEntities.Petition>> KeyWordSearch(string tag)
        {
            OperationResult<IEnumerable<clientEntities.Petition>> result;

            var script = string.Empty;

            try
            {
                using (var db = new EDEntities())
                {
                    db.Database.Log = s => script += s;

                    // TODO: get rid of db.Petitions.ToList():
                    var petitions =
                        //from petition in db.Petitions
                        //where petition.KeyWords.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Contains(tag)
                        //select petition;
                        db.Petitions.ToList().Where(p => p.KeyWords.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Contains(tag));

                    var clientPetitions = this.GetPetitionsUnderLimit(db, petitions);
                    result = OperationResult<IEnumerable<clientEntities.Petition>>.Success(clientPetitions);
                }
            }
            catch (Exception exc)
            {
                result = OperationResult<IEnumerable<clientEntities.Petition>>.ExceptionResult(exc);
            }

            return result;
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Get all petitions.
        /// </summary>
        /// <returns></returns>
        public OperationResult<IEnumerable<clientEntities.Petition>> Get()
        {
            OperationResult<IEnumerable<clientEntities.Petition>> result;

            try
            {
                using (var db = new EDEntities())
                {
                    var petitions = from p in db.Petitions
                                    select p;

                    var clientPetitions = this.GetPetitionsUnderLimit(db, petitions);
                    result = OperationResult<IEnumerable<clientEntities.Petition>>.Success(clientPetitions);
                }
            }
            catch (Exception exc)
            {
                result = OperationResult<IEnumerable<clientEntities.Petition>>.ExceptionResult(exc);
            }

            return result;
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Get petition by ID.
        /// </summary>
        /// <param name="petitionID"></param>
        /// <returns></returns>
        public OperationResult<clientEntities.Petition> Get(int petitionID)
        {
            OperationResult<clientEntities.Petition> result;

            try
            {
                using (var db = new EDEntities())
                {
                    var petition = db.Petitions.SingleOrDefault(p => p.ID == petitionID);
                    if (petition == default(Petition))
                    {
                        result = OperationResult<clientEntities.Petition>.Fail(-2, "Petition not found");
                        return result;
                    }

                    var clientPetition = new clientEntities.Petition(petition);
                    clientPetition.VotesCount = petition.PetitionVotes.Count + petition.PetitionEmailVotes.Count(v => v.IsConfirmed);

                    result = OperationResult<clientEntities.Petition>.Success(1, "Success", clientPetition);
                }
            }
            catch (Exception ex)
            {
                result = OperationResult<clientEntities.Petition>.ExceptionResult(ex);
            }

            return result;
        }
Ejemplo n.º 16
0
        private IEnumerable<clientEntities.Petition> GetPetitionsUnderLimit(EDEntities db, IEnumerable<Petition> petitions)
        {
            var petitionsUnderLimit = new List<clientEntities.Petition>();
            foreach (var petition in petitions)
            {
                var votesCount = db.PetitionVotes.Count(p => p.PetitionID == petition.ID) +
                                 db.PetitionEmailVotes.Count(p => p.PetitionID == petition.ID && p.IsConfirmed);
                if (votesCount > petition.Limit)
                {
                    var clientPetition = new clientEntities.Petition(petition)
                                         {
                                             VotesCount = votesCount
                                         };
                    petitionsUnderLimit.Add(clientPetition);
                }
            }

            return petitionsUnderLimit;
        }
Ejemplo n.º 17
0
        public OperationResult<Model.BusinessEntities.Petition> GetPetition(string hash)
        {
            OperationResult<Model.BusinessEntities.Petition> result;

            try
            {
                using (var db = new EDEntities())
                {
                    var petitionEmailVote = db.PetitionEmailVotes.SingleOrDefault(v => v.Hash == hash);
                    if (petitionEmailVote == null)
                    {
                        result = OperationResult<Model.BusinessEntities.Petition>.Fail(-2, "Petition email vote for such hash not found.");
                        return result;
                    }

                    var petition = db.Petitions.SingleOrDefault(p => p.ID == petitionEmailVote.PetitionID);
                    result = OperationResult<Model.BusinessEntities.Petition>.Success(1, "Success.", new Model.BusinessEntities.Petition(petition));
                }
            }
            catch (Exception exception)
            {
                result = OperationResult<Model.BusinessEntities.Petition>.ExceptionResult(exception);
            }

            return result;
        }