コード例 #1
0
        public Models.PetitionModel InitializePetitionModel()
        {
            var result = new Models.PetitionModel();

            using (var context = ApplicationDbContext.Create())
            {
                InitializePetitionModelInternal(result, context);
            }
            return(result);
        }
コード例 #2
0
        public Models.PetitionModel UpdatePetition(Models.PetitionModel model, string currentUserID)
        {
            if (model == null)
            {
                return(null);
            }

            if (string.IsNullOrWhiteSpace(currentUserID))
            {
                return(null);
            }

            using (var context = ApplicationDbContext.Create())
            {
                //var user = context.Users.Where( x => x.Id == currentUserID ).FirstOrDefault();
                //if ( user == null )
                //    return null;

                var petition = context.Petitions.Where(x => model.PetitionID == x.PetitionID).FirstOrDefault();
                if (petition == null)
                {
                    return(null);
                }

                if (model.Image != null)
                {
                    var fileService = new FileService();
                    petition.ImageRelativePath = fileService.UploadFile(new List <string> {
                        "PetitionImage"
                    }, model.Image.FileName, model.Image.InputStream);
                }
                else
                {
                    petition.ImageRelativePath = model.ImageRelativePath;
                }

                AutoMapper.Mapper.Map <Models.PetitionModel, Petition>(model, petition);

                var youtubeCode = EEP.Utility.Youtube.GetYoutubeIdFromUrl(model.YoutubeUrl);
                if (youtubeCode != null)
                {
                    petition.YoutubeCode = youtubeCode;
                }

                context.SaveChanges();
            }

            return(GetPetition(model.PetitionID, currentUserID));
        }
コード例 #3
0
        public void UpdatePetition_New_Updated()
        {
            var user = Helpers.CreateNewUser();

            Models.PetitionModel model      = null;
            Models.PetitionModel savedModel = null;
            int petitionID = 0;

            try
            {
                var service = new PetitionService();
                model = new Models.PetitionModel
                {
                    Title             = "Some Title",
                    Description       = "Some Description",
                    SubmitterName     = "Some user",
                    SubmitterUserID   = user.Id,
                    TargetInstitution = "Test institution",
                    Verified          = false
                };

                petitionID = service.CreateNewPetition(model);
                Assert.AreNotEqual(0, petitionID, "ID not created");

                savedModel = service.GetPetition(petitionID, user.Id);
                Assert.IsNotNull(savedModel);

                string newTitle = "Some new title";
                savedModel.Title = newTitle;
                var changedModel = service.UpdatePetition(savedModel, user.Id);

                Assert.IsNotNull(savedModel);
                Assert.AreEqual(newTitle, changedModel.Title, "Title Changed");
                Assert.AreEqual(changedModel.Description, model.Description, "Description did not change");
            }
            finally
            {
                Helpers.DeleteUser(user);

                if (model != null)
                {
                    new PetitionService().DeletePetition(petitionID);
                }
            }
        }
コード例 #4
0
        public void DeletePetition_New_Deleted()
        {
            var user = Helpers.CreateNewUser();

            Models.PetitionModel model      = null;
            Models.PetitionModel savedModel = null;

            try
            {
                var service = new PetitionService();
                model = new Models.PetitionModel
                {
                    Title             = "Some Title",
                    Description       = "Some Description",
                    SubmitterName     = "Some user",
                    SubmitterUserID   = user.Id,
                    TargetInstitution = "Test institution",
                    Verified          = false
                };

                var id = service.CreateNewPetition(model);
                Assert.AreNotEqual(0, id, "ID not created");

                savedModel = service.GetPetition(id, user.Id);
                Assert.IsNotNull(savedModel);

                service.DeletePetition(id);
                savedModel = service.GetPetition(id, user.Id);
                Assert.IsNull(savedModel);
            }
            finally
            {
                Helpers.DeleteUser(user);

                if (model != null)
                {
                    new PetitionService().DeletePetition(model.PetitionID);
                }
            }
        }
コード例 #5
0
        public void GetPetition_NewVerified_NonVerified()
        {
            var user = Helpers.CreateNewUser();

            Models.PetitionModel model      = null;
            Models.PetitionModel savedModel = null;
            int petitionID = 0;

            try
            {
                var service = new PetitionService();
                model = new Models.PetitionModel
                {
                    Title             = "Some Title",
                    Description       = "Some Description",
                    SubmitterName     = "Some user",
                    SubmitterUserID   = user.Id,
                    TargetInstitution = "Test institution",
                    Verified          = false
                };

                petitionID = service.CreateNewPetition(model);

                Assert.AreNotEqual(0, petitionID, "ID not created");

                savedModel = service.GetPetition(petitionID, user.Id);

                Assert.IsFalse(savedModel.Verified, "Petition should be non verified");
            }
            finally
            {
                Helpers.DeleteUser(user);

                if (model != null)
                {
                    new PetitionService().DeletePetition(petitionID);
                }
            }
        }
コード例 #6
0
        public static int CreateNewPetition(ApplicationUser user,
                                            string title             = "Some title",
                                            string description       = "Some Description",
                                            int targetCount          = 5000,
                                            string submitterName     = "Some submitter",
                                            string targetInstitution = "Some institution",
                                            DateTime?startTime       = null,
                                            DateTime?endTime         = null)
        {
            var service = new PetitionService();
            var model   = new Models.PetitionModel
            {
                Title             = title,
                Description       = description,
                SubmitterName     = submitterName,
                SubmitterUserID   = user.Id,
                TargetInstitution = targetInstitution,
                Verified          = false
            };

            return(service.CreateNewPetition(model));
        }
コード例 #7
0
        public int CreateNewPetition(Models.PetitionModel model)
        {
            if (model == null)
            {
                return(0);
            }

            if (string.IsNullOrWhiteSpace(model.SubmitterUserID))
            {
                return(0);
            }

            using (var context = ApplicationDbContext.Create())
            {
                var user = context.Users.Where(x => x.Id == model.SubmitterUserID).FirstOrDefault();
                if (user == null)
                {
                    return(0);
                }

                Petition petition = new Petition
                {
                    SubmitterUser       = user,
                    Verified            = false,
                    TargetInstitutionId = model.SelectedTargetInstitution,
                    AdminIgnore         = false,
                    Description         = model.Description,
                    SubmitterName       = model.SubmitterName,
                    Title = model.Title
                };


                if (string.IsNullOrWhiteSpace(petition.Title) ||
                    string.IsNullOrWhiteSpace(petition.Description) ||
                    string.IsNullOrWhiteSpace(petition.SubmitterName)
                    )
                {
                    return(0);
                }

                // Fix some values


                var youtubeCode = EEP.Utility.Youtube.GetYoutubeIdFromUrl(model.YoutubeUrl);
                if (youtubeCode != null)
                {
                    petition.YoutubeCode = youtubeCode;
                }

                if (model.Image != null)
                {
                    var fileService = new FileService();
                    petition.ImageRelativePath = fileService.UploadFile(new List <string> {
                        "PetitionImage"
                    }, model.Image.FileName, model.Image.InputStream);
                }

                context.Petitions.Add(petition);

                context.SaveChanges();

                return(petition.PetitionID);
            }
        }
コード例 #8
0
 private static void InitializePetitionModelInternal(Models.PetitionModel result, ApplicationDbContext context)
 {
     result.TargetInstitutionList = context.Parliaments.Select(x => new Models.TargetInstitutionModel {
         Id = x.ParliamentID, Name = x.Name
     }).ToList();
 }