public virtual CreateResult CreateFreshCharity(string charityId)
        {
            Charity charity = new Charity
            {
                Address1 = "Enter an Address",
                Address2 = string.Empty,
                CharityId = charityId,
                City = "Dallas",
                Email = "Enter an Email",
                Essay = "My Description",
                FirstName = "First Name",
                IsSearchable = false,
                LastName = "Last Name",
                OrganizationName = "Charity " + charityId,
                Phone = "123-456-7890",
                PostalCode = "75254",
                State = "TX",
                Website = "http://www.google.com/",
                YearsService = 0
            };

            _repository.Add(charity);

            return CreateResult.SuccessfullyCreated;
        }
        public virtual UpdateResult Update(Charity updatedCharityData, IEnumerable<Skill> neededSkills, IEnumerable<ServiceArea> neededAreas)
        {
            if (_charityProvider.DoesCharityExist(updatedCharityData.OrganizationName, updatedCharityData.CharityId))
                return UpdateResult.ItemAlreadyExists;

            _repository.Update(updatedCharityData, neededSkills, neededAreas);

            return UpdateResult.Successful;
        }
        public virtual CreateResult Save(Charity newCharityData)
        {
            if (_charityProvider.DoesCharityExist(newCharityData.OrganizationName, newCharityData.CharityId))
                return CreateResult.ItemAlreadyExists;

            _repository.Add(newCharityData);

            return CreateResult.SuccessfullyCreated;
        }
        public virtual void Add(Charity charity)
        {
            using (var context = new SqlDataContext())
            {
                context.Charities.Add(charity);
                context.SaveChanges();
            }

            _searchRepository.UpdateSearchIndexForCharity(charity.CharityId);
        }
        public void NewPost_Success_ReturnsRedirect()
        {
            var charity = new Charity
            {
                OrganizationName = "Stuff"
            };

            _mockCharityCreator.Setup(x => x.Save(charity)).Returns(CreateResult.SuccessfullyCreated);

            var redirectResult = _controller.New(new CharityContainerViewModel { Charity = charity }).EnsureIs<StronglyNamedRouteResult>();
            redirectResult.RouteValues["controller"].ToString().ToLower().IsEqualTo("charity");
            redirectResult.RouteValues["action"].ToString().ToLower().IsEqualTo("search");
        }
        public void NewPost_OrganizationNameExists_ReturnsView()
        {
            var charity = new Charity
            {
                OrganizationName = "Stuff"
            };

            _mockCharityCreator.Setup(x => x.Save(charity)).Returns(CreateResult.ItemAlreadyExists);

            var viewResult = _controller.New(new CharityContainerViewModel { Charity = charity }).EnsureIs<ViewResult>();
            var model = viewResult.Model.EnsureIs<CharityContainerViewModel>();
            model.Charity.IsEqualTo(charity);
        }
        public void NewPost_ValidationErrors_ReturnsView()
        {
            _controller.ModelState.AddModelError("Test", "Test");

            var charity = new Charity
            {
                OrganizationName = "Stuff"
            };

            var viewResult = _controller.New(new CharityContainerViewModel { Charity = charity }).EnsureIs<ViewResult>();
            var model = viewResult.Model.EnsureIs<CharityContainerViewModel>();
            model.Charity.IsEqualTo(charity);

            _mockCharityCreator.Verify(x => x.Save(charity), Times.Never());
        }
        public void Update_CharityAlreadyExists_ReturnsError()
        {
            var charity = new Charity
            {
                OrganizationName = "SomeOrg",
                CharityId = "2"
            };

            _mockCharityProvider.Setup(x => x.DoesCharityExist("SomeOrg", "2")).Returns(true);

            var result = _updater.Update(charity, null, null);

            result.IsEqualTo(UpdateResult.ItemAlreadyExists);
            _mockRepository.Verify(x => x.Update(charity, null, null), Times.Never());
        }
        public void Save_CharityAlreadyExists_ReturnsError()
        {
            Charity charity = new Charity
            {
                CharityId = "2",
                OrganizationName = "SomeOrg"
            };

            _mockCharityProvider.Setup(x => x.DoesCharityExist("SomeOrg", "2")).Returns(true);

            var result = _creator.Save(charity);

            result.IsEqualTo(CreateResult.ItemAlreadyExists);

            _mockRepository.Verify(x => x.Add(charity), Times.Never());
        }
        public void Update_UpdatesValues()
        {
            _mockRepository.Setup(x => x.GetAllCharities()).Returns(new List<Charity>
            {
                new Charity { OrganizationName = "SomeOrg2" }
            });

            var charity = new Charity
            {
                OrganizationName = "SomeOrg"
            };

            var result = _updater.Update(charity, null, null);

            result.IsEqualTo(UpdateResult.Successful);
            _mockRepository.Verify(x => x.Update(charity, null, null), Times.Once());
        }
        public void Save_PersistsTheCharity_SavesIt()
        {
            Charity charity = new Charity
            {
                CharityId = "2",
                OrganizationName = "SomeOrg"
            };

            _mockRepository.Setup(x => x.GetAllCharities()).Returns(new List<Charity>
            {
               
            });

            var result = _creator.Save(charity);

            result.IsEqualTo(CreateResult.SuccessfullyCreated);

            _mockRepository.Verify(x => x.Add(charity), Times.Once());
        }
        public virtual void Update(Charity updatedData, IEnumerable<Skill> neededSkills, IEnumerable<ServiceArea> neededAreas)
        {
            using (var context = new SqlDataContext())
            {
                var charity = context.Charities.First(x => x.CharityId == updatedData.CharityId);

                charity.Address1 = updatedData.Address1;
                charity.Address2 = updatedData.Address2;
                charity.City = updatedData.City;
                charity.Email = updatedData.Email;
                charity.Essay = updatedData.Essay;
                charity.FirstName = updatedData.FirstName;
                charity.LastName = updatedData.LastName;
                charity.OrganizationName = updatedData.OrganizationName;
                charity.Phone = updatedData.Phone;
                charity.PostalCode = updatedData.PostalCode;
                charity.State = updatedData.State;
                charity.Website = updatedData.Website;
                charity.YearsService = updatedData.YearsService;
                charity.IsSearchable = updatedData.IsSearchable;

                if (!string.IsNullOrWhiteSpace(updatedData.Password))
                    charity.Password = updatedData.Password;

                charity.NeededSkills.Clear();
                charity.NeededServiceAreas.Clear();

                var skillTransform = neededSkills.Select(x => (int)x);
                var areaTransform = neededAreas.Select(x => (int)x);

                var newSkills = context.Skills.Where(x => skillTransform.Contains(x.SkillId)).ToList();
                var newAreas = context.ServiceAreas.Where(x => areaTransform.Contains(x.ServiceAreaId)).ToList();

                newSkills.ForEach(x => charity.NeededSkills.Add(x));
                newAreas.ForEach(x => charity.NeededServiceAreas.Add(x));

                context.SaveChanges();
            }

            _searchRepository.UpdateSearchIndexForCharity(updatedData.CharityId);
        }
        public ActionResult CreateCharity(string id, string password)
        {
            var encryptor = new Encryptor();
            password = encryptor.Encrypt(password);

            var charity = new Charity
                {
                    CharityId = id,
                    Password = password,
                    Address1 = "__",
                    Address2 = "__",
                    City = "__",
                    State = "__",
                    PostalCode = "__",
                    Essay = "__",
                    Email = "__",
                    FirstName = "__",
                    IsSearchable = false,
                    LastName = "__",
                    OrganizationName = "charity" + id,
                    Phone = "__",
                    Website = "__",
                    YearsService = 0
                };

            _charityRepository.Add(charity);

            return Json(new { awesome = "clearly true" });
        }
 private void RemoveDefaultValues(Charity charity)
 {
     if (charity.Phone == "__" && charity.FirstName == "__")
     {
         charity.Address1 = String.Empty;
         charity.Address2 = String.Empty;
         charity.City = String.Empty;
         charity.State = String.Empty;
         charity.PostalCode = String.Empty;
         charity.Essay = String.Empty;
         charity.Email = String.Empty;
         charity.FirstName = String.Empty;
         charity.LastName = String.Empty;
         charity.Phone = String.Empty;
         charity.Website = String.Empty;
     }
 }