public virtual void Add(BoardMember member, IEnumerable<Skill> skills, IEnumerable<ServiceArea> serviceAreas)
        {
            using (var context = new SqlDataContext())
            {
                member.AvailableSkills.Clear();
                member.Interests.Clear();

                context.BoardMembers.Add(member);
                context.SaveChanges();

                if (skills != null && serviceAreas != null && member.AvailableSkills != null && member.Interests != null)
                {
                    member.AvailableSkills.Clear();
                    member.Interests.Clear();

                    var rawSkillIds = skills.Select(x => (int)x);
                    var rawServiceIds = serviceAreas.Select(x => (int)x);

                    var transformSkils = context.Skills.Where(x => rawSkillIds.Contains(x.SkillId)).ToList();
                    var transformAreas = context.ServiceAreas.Where(x => rawServiceIds.Contains(x.ServiceAreaId)).ToList();

                    transformSkils.ForEach(x => member.AvailableSkills.Add(x));
                    transformAreas.ForEach(x => member.Interests.Add(x));
                }

                context.SaveChanges();
            }
        }
 public IEnumerable<string> GetStates()
 {
     using (var context = new SqlDataContext())
     {
         return context.Zips.Select(z => z.State).Distinct().ToList();
     }
 }
 public virtual IEnumerable<Charity> GetAllCharities()
 {
     using (var context = new SqlDataContext())
     {
         return context.Charities.ToList();
     }
 }
 public virtual IEnumerable<BoardMember> GetAllBoardMembers()
 {
     using (var context = new SqlDataContext())
     {
         return context.BoardMembers.ToList();
     }
 }
 public virtual IEnumerable<BoardMember> GetSetOfBoardMembers(Expression<Func<BoardMember, bool>> selector)
 {
     using(var context = new SqlDataContext())
     {
         return context.BoardMembers.Where(selector).ToList();
     }
 }
 public virtual BoardMember GetBoardMember(int id)
 {
     using (var context = new SqlDataContext())
     {
         return context.BoardMembers.Find(id);
     }
 }
 public virtual ZipCode GetZipCode(string zipCode)
 {
     using (var context = new SqlDataContext())
     {
         return context.Zips.FirstOrDefault(x => x.Zip == zipCode);
     }
 }
 public IEnumerable<string> GetCitiesFor(string state)
 {
     using (var context = new SqlDataContext())
     {
         return context.Zips.Where(z => z.State == state).Select(z => z.City).ToList();
     }
 }
 public virtual IEnumerable<Charity> GetSubsetOfCharities(Expression<Func<Charity, bool>> selector)
 {
     using (var context = new SqlDataContext())
     {
         return context.Charities.Where(selector).ToList();
     }
 }
        public virtual BoardMember GetBoardMember(string email)
        {
            using (var context = new SqlDataContext())
            {
                return context.BoardMembers.Where(x => x.Email == email).FirstOrDefault();

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

            _searchRepository.UpdateSearchIndexForCharity(charity.CharityId);
        }
        public void CanCrudSkill()
        {
            Database.SetInitializer<SqlDataContext>(null);

            var dataContext = new SqlDataContext();

            //Get
            var skill = dataContext.Skills.Find(1);
            Assert.IsNotNull(skill);
        }
        public virtual IEnumerable<SkillEntity> GetSkillsFor(string charityId)
        {
            using (var context = new SqlDataContext())
            {
                var charity =  context.Charities.FirstOrDefault(x => x.CharityId == charityId);

                if (charity == null)
                    return new List<SkillEntity>();

                return charity.NeededSkills.ToList();
            }
        }
        public virtual IEnumerable<SkillEntity> GetSkillsFor(int memberId)
        {
            using (var context = new SqlDataContext())
            {
                var boardMember = context.BoardMembers.FirstOrDefault(x => x.BoardMemberId == memberId);

                if (boardMember == null)
                    return new List<SkillEntity>();

                return boardMember.AvailableSkills.ToList();
            }
        }
        public virtual IEnumerable<ServiceAreaEntity> GetServiceAreasFor(int memberId)
        {
            using (var context = new SqlDataContext())
            {
                var boardMember = context.BoardMembers.FirstOrDefault(x => x.BoardMemberId == memberId);

                if (boardMember == null)
                    return new List<ServiceAreaEntity>();

                return boardMember.Interests.ToList();
            }
        }
        public virtual Dictionary<Charity, IEnumerable<ServiceAreaEntity>> GetSkillsForCharitySet(IEnumerable<string> charityIds)
        {
            Dictionary<Charity, IEnumerable<ServiceAreaEntity>> data = new Dictionary<Charity, IEnumerable<ServiceAreaEntity>>();

            using (var context = new SqlDataContext())
            {
                var charities = context.Charities.Where(x => charityIds.Contains(x.CharityId)).ToList();

                charities.ForEach(x =>
                {
                    data.Add(x, x.NeededServiceAreas.ToList());
                });

                return data;
            }
        }
 public virtual IEnumerable<BoardMemberSearchResult> GetBoardMemberSearchResultsFor(SearchCriteria searchCriteria)
 {
     using (var context = new SqlDataContext())
     {
         if (!string.IsNullOrEmpty(searchCriteria.State) && !string.IsNullOrEmpty(searchCriteria.City))
         {
             return context.BoardMemberSearchIndex.Where(x => x.State == searchCriteria.State && x.City == searchCriteria.City).ToList();
         }
         else if (!string.IsNullOrEmpty(searchCriteria.PostalCode))
         {
             return context.BoardMemberSearchIndex.Where(x => x.Zip == searchCriteria.PostalCode).ToList();
         }
         else
         {
             return context.BoardMemberSearchIndex.Take(10000).ToList();
         }
     }
 }
        public virtual void Update(BoardMember updatedMember, IEnumerable<Skill> skills, IEnumerable<ServiceArea> serviceAreas)
        {
            using (var context = new SqlDataContext())
            {
                var member = context.BoardMembers.First(x => x.BoardMemberId == updatedMember.BoardMemberId);

                member.Address1 = updatedMember.Address1;
                member.Address2 = updatedMember.Address2;
                member.City = updatedMember.City;
                member.Email = updatedMember.Email;
                member.Employer = updatedMember.Employer;
                member.FirstName = updatedMember.FirstName;
                member.LastName = updatedMember.LastName;
                member.Title = updatedMember.Title;
                member.Phone = updatedMember.Phone;
                member.PostalCode = updatedMember.PostalCode;
                member.State = updatedMember.State;
                member.Ethnicity = updatedMember.Ethnicity;
                member.Gender = updatedMember.Ethnicity;
                member.YearsService = updatedMember.YearsService;
                member.MissionStatement = updatedMember.MissionStatement;
                member.BirthDay = updatedMember.BirthDay;
                member.IsSearchable = updatedMember.IsSearchable;
                if (!String.IsNullOrWhiteSpace(updatedMember.Password))
                    member.Password = updatedMember.Password;

                if (skills != null && serviceAreas != null && member.AvailableSkills != null && member.Interests != null)
                {
                    member.AvailableSkills.Clear();
                    member.Interests.Clear();

                    var rawSkillIds = skills.Select(x => (int)x);
                    var rawServiceIds = serviceAreas.Select(x => (int)x);

                    var transformSkils = context.Skills.Where(x => rawSkillIds.Contains(x.SkillId)).ToList();
                    var transformAreas = context.ServiceAreas.Where(x => rawServiceIds.Contains(x.ServiceAreaId)).ToList();

                    transformSkils.ForEach(x => member.AvailableSkills.Add(x));
                    transformAreas.ForEach(x => member.Interests.Add(x));
                }

                context.SaveChanges();
            }
        }
        public void CanCrudBoardMembers()
        {
            Database.SetInitializer<SqlDataContext>(null);

            var dataContext = new SqlDataContext();

            //Insert!
            var member = new BoardMember
            {
                FirstName = "bob",
                LastName = "smith",
                BirthDay = DateTime.Now,
                Employer = "wut.com",
                Title = "le title",
                Phone = "8765435467",
                Email = "*****@*****.**",
                Address1 = "123 Foo",
                Address2 = "Suite 5",
                City = "Dallas",
                State = "TX",
                PostalCode = "76543",
                MissionStatement = "To hate Ken"
                
            };
            dataContext.BoardMembers.Add(member);
            dataContext.SaveChanges();

            //Get
            var dude = dataContext.BoardMembers.Find(member.BoardMemberId);
            Assert.IsNotNull(dude);

            //Update
            dude.LastName = "JustinIsShort";
            dataContext.SaveChanges();
            dude = dataContext.BoardMembers.Find(member.BoardMemberId);
            Assert.AreEqual("JustinIsShort", dude.LastName, "Bad last name.");

            //Delete
            dataContext.BoardMembers.Remove(dude);
            dataContext.SaveChanges();
            dude = dataContext.BoardMembers.Find(member.BoardMemberId);
            Assert.IsNull(dude, "Did not delete.");
        }
        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 virtual int? ValidateLogin(string email, string password)
        {
            using (var context = new SqlDataContext())
            {
                var thingy = context.BoardMembers
                    .Where(x => x.Email == email && x.Password == password)
                    .FirstOrDefault();

                if (thingy == null)
                    return null;
                else
                    return thingy.BoardMemberId;
            }

        }
        public virtual string ValidateLogin(string username, string password)
        {
            LookupCharityId procedure = new LookupCharityId();
            var charityId = procedure.Execute(username).CharityId;

            using (var context = new SqlDataContext())
            {
                var matchingPassword = context.Charities.FirstOrDefault(x => x.CharityId == charityId && x.Password.ToLower() == password.ToLower());

                if (matchingPassword != null)
                    return charityId;

                return null;
            }

        }