Beispiel #1
0
        public void UpdateOccupationBestWorkTime(string[] selectedBestWorkTime, OccupationVM occupationToUpdate)
        {
            if (occupationToUpdate.BestWorkTimes == null)
            {
                occupationToUpdate.BestWorkTimes = new List <OccupationBestWorkTimeVM>();
            }

            var all = bestWorkTimeRepository.GetAll();
            var selectedBestWorkTimeHS = new HashSet <string>(selectedBestWorkTime);
            var occupationBestWorkTime = new HashSet <int>(occupationToUpdate.BestWorkTimes.Select(c => c.BestWorkTime.Id));

            occupationBestWorkTimeRepository.RemoveAll(occupationToUpdate.Id);
            foreach (var bestWorkTime in all)
            {
                if (selectedBestWorkTimeHS.Contains(bestWorkTime.Id.ToString()))
                {
                    if (!occupationBestWorkTime.Contains(bestWorkTime.Id))
                    {
                        occupationToUpdate.BestWorkTimes.Add(new OccupationBestWorkTimeVM {
                            OccupationId = occupationToUpdate.Id, BestWorkTimeId = bestWorkTime.Id
                        });
                    }
                }
                else
                {
                    if (occupationBestWorkTime.Contains(bestWorkTime.Id))
                    {
                        var bestWorkTimeToRemove = occupationToUpdate.BestWorkTimes.SingleOrDefault(i => i.BestWorkTimeId == bestWorkTime.Id);
                        occupationBestWorkTimeRepository.Remove(mapper.Map <OccupationBestWorkTime>(bestWorkTimeToRemove));
                    }
                }
            }
        }
 public IEnumerable <BestWorkTimeVM> GetAll()
 {
     return(bestWorkTimeRepository.GetAll().ProjectTo <BestWorkTimeVM>(mapper.ConfigurationProvider));
 }