/// <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); }
public PetitionCreatedNotification(Petition createdPetition, string confirmCreationUrl) : base(Action.PetitionCreation, createdPetition.Email, new Dictionary<string, string>() { {"PetitionName", createdPetition.Subject}, {"PetitionText", createdPetition.Text}, {"PetitionRequirements", createdPetition.Requirements}, {"ConfirmUrl", confirmCreationUrl}, {"PetitionUrl", createdPetition.Url}, {"PetitionLimit", createdPetition.Limit.ToString()} }, string.Format("��� ������������ ��������� ������� �������� �� ����������, ���������� ��� �� email {0}.", createdPetition.Email)) { }
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); }
public void Map_PetitionWebToData_Success() { MapperConfig.Map(); var webPetition1 = new WebModels.Petition() { ID = 12, AddressedTo = "no one", Category = new WebModels.Entity() { ID = 2, Name = "Data category name", Description = "Category from data model", Group = new WebModels.EntityGroup() { ID = 3, Name = "Data entity group", Parent = new WebModels.EntityGroup() { ID = -3, Name = "Parent group", Parent = null } } }, CreatedBy = new WebModels.People() { ID = 1, Login = "******" }, CreatedDate = DateTime.Now, EffectiveFrom = DateTime.Now, EffectiveTo = DateTime.Now.AddMinutes(1), Email = "test@data", KeyWords = new List<string>() {"abc", "c de", "xy z"}, Level = new WebModels.PetitionLevel() { ID = 4, Limit = 1000, Name = "Thousand level" }, Limit = 1100, Requirements = "Long text", Subject = "Petition caption", Text = "Short text", VotesCount = 10 }; var dataPetition1 = Mapper.Map<DataModels.Petition>(webPetition1); Assert.IsNotNull(dataPetition1.Category); Assert.AreEqual(dataPetition1.Category.ID, 2); Assert.AreEqual(dataPetition1.CategoryID, 2); Assert.IsNotNull(dataPetition1.Category.EntityGroup); Assert.AreEqual(dataPetition1.Category.EntityGroup.ID, 3); Assert.AreEqual(dataPetition1.Category.EntityGroupID, 3); Assert.IsNotNull(dataPetition1.KeyWords); Assert.AreEqual(dataPetition1.KeyWords, "abc, c de, xy z"); Assert.IsNotNull(dataPetition1.PetitionLevel); Assert.AreEqual(dataPetition1.LevelID, 4); Assert.IsNotNull(dataPetition1.Person); Assert.AreEqual(dataPetition1.Person.ID, 1); Assert.AreEqual(dataPetition1.CreatedBy, 1); }
/// <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); }
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; }
/// <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; }
public void Map_PetitionWebToData_Success() { MapperConfig.Map(); var webPetition1 = new WebModels.Petition() { ID = 12, AddressedTo = "no one", Category = new WebModels.Entity() { ID = 2, Name = "Data category name", Description = "Category from data model", Group = new WebModels.EntityGroup() { ID = 3, Name = "Data entity group", Parent = new WebModels.EntityGroup() { ID = -3, Name = "Parent group", Parent = null } } }, CreatedBy = new WebModels.People() { ID = 1, Login = "******" }, CreatedDate = DateTime.Now, EffectiveFrom = DateTime.Now, EffectiveTo = DateTime.Now.AddMinutes(1), Email = "test@data", KeyWords = new List <string>() { "abc", "c de", "xy z" }, Level = new WebModels.PetitionLevel() { ID = 4, Limit = 1000, Name = "Thousand level" }, Limit = 1100, Requirements = "Long text", Subject = "Petition caption", Text = "Short text", VotesCount = 10 }; var dataPetition1 = Mapper.Map <DataModels.Petition>(webPetition1); Assert.IsNotNull(dataPetition1.Category); Assert.AreEqual(dataPetition1.Category.ID, 2); Assert.AreEqual(dataPetition1.CategoryID, 2); Assert.IsNotNull(dataPetition1.Category.EntityGroup); Assert.AreEqual(dataPetition1.Category.EntityGroup.ID, 3); Assert.AreEqual(dataPetition1.Category.EntityGroupID, 3); Assert.IsNotNull(dataPetition1.KeyWords); Assert.AreEqual(dataPetition1.KeyWords, "abc, c de, xy z"); Assert.IsNotNull(dataPetition1.PetitionLevel); Assert.AreEqual(dataPetition1.LevelID, 4); Assert.IsNotNull(dataPetition1.Person); Assert.AreEqual(dataPetition1.Person.ID, 1); Assert.AreEqual(dataPetition1.CreatedBy, 1); }