Esempio n. 1
0
        public void Add(Schedule item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var isNew = true;

            if (item.Id > 0)
            {
                var entity = GetEntityById(item.Id);
                if (entity != null)
                {
                    _ = _mapper.Map(item, entity, opts => opts.UseEntityCache(_salesAreaByNameCache));
                    UpdateScheduleBreaks(item, entity);
                    UpdateScheduleProgrammes(item, entity);
                    _ = _dbContext.Update(entity,
                                          post => post.MapTo(item, opts => opts.UseEntityCache(_salesAreaByIdCache)), _mapper);
                    isNew = false;
                }
            }

            if (isNew)
            {
                var entity = _mapper.Map <ScheduleEntity>(item, opts => opts.UseEntityCache(_salesAreaByNameCache));
                UpdateScheduleBreaks(item, entity, true);
                UpdateScheduleProgrammes(item, entity, true);
                _ = _dbContext.Add(entity, post => post.MapTo(item, opts => opts.UseEntityCache(_salesAreaByIdCache)),
                                   _mapper);
            }
        }
Esempio n. 2
0
        private void UpdateScheduleProgrammes(Schedule scheduleModel, ScheduleEntity updatedEntity, bool isNew = false)
        {
            if (scheduleModel.Programmes?.Any() ?? false)
            {
                if (!isNew)
                {
                    //delete programmes
                    updatedEntity.Programmes.Where(p => scheduleModel.Programmes.All(mp => mp.Id != p.ProgrammeId)).ToList()
                    .ForEach(p => updatedEntity.Programmes.Remove(p));

                    //update programmes
                    foreach (var p in updatedEntity.Programmes)
                    {
                        var model = scheduleModel.Programmes.FirstOrDefault(mp => mp.Id == p.ProgrammeId);
                        if (model != null)
                        {
                            _ = _mapper.Map(model, p, opts => opts.UseEntityCache(_salesAreaByNameCache));
                        }
                    }
                }

                //add programmes
                _mapper.Map <List <ScheduleProgramme> >(
                    scheduleModel.Programmes.Where(mp => updatedEntity.Programmes.All(p => p.ProgrammeId != mp.Id)), opts => opts.UseEntityCache(_salesAreaByNameCache))
                .ForEach(p => updatedEntity.Programmes.Add(p));
            }
            else
            {
                updatedEntity.Programmes.Clear();
            }
        }
Esempio n. 3
0
        private void UpdateScheduleBreaks(Schedule scheduleModel, ScheduleEntity updatedEntity, bool isNew = false)
        {
            if (scheduleModel.Breaks?.Any() ?? false)
            {
                if (!isNew)
                {
                    //delete breaks
                    updatedEntity.Breaks.Where(b => scheduleModel.Breaks.All(mb => mb.Id != b.Id)).ToList()
                    .ForEach(b => updatedEntity.Breaks.Remove(b));

                    //update breaks
                    foreach (var b in updatedEntity.Breaks)
                    {
                        var model = scheduleModel.Breaks.FirstOrDefault(x => x.Id == b.Id);
                        if (model != null)
                        {
                            _ = _mapper.Map(model, b, opts => opts.UseEntityCache(_salesAreaByNameCache));

                            #region Update Break Efficiency

                            if (model.BreakEfficiencyList?.Any() ?? false)
                            {
                                //delete break efficiency
                                b.BreakEfficiencies.Where(be => model.BreakEfficiencyList.All(mbe =>
                                                                                              !string.Equals(mbe.Demographic, be.Demographic,
                                                                                                             StringComparison.InvariantCultureIgnoreCase)))
                                .ToList()
                                .ForEach(be => b.BreakEfficiencies.Remove(be));

                                //update break efficiency
                                foreach (var be in b.BreakEfficiencies)
                                {
                                    var mbe = model.BreakEfficiencyList.FirstOrDefault(x =>
                                                                                       string.Equals(x.Demographic, be.Demographic,
                                                                                                     StringComparison.InvariantCultureIgnoreCase));
                                    if (mbe != null)
                                    {
                                        _mapper.Map(mbe, be);
                                    }
                                }

                                //add break efficiency
                                _mapper.Map <List <ScheduleBreakEfficiency> >(
                                    model.BreakEfficiencyList.Where(mbe => b.BreakEfficiencies.All(be =>
                                                                                                   !string.Equals(be.Demographic, mbe.Demographic,
                                                                                                                  StringComparison.InvariantCultureIgnoreCase))))
                                .ForEach(be => b.BreakEfficiencies.Add(be));
                            }
                            else
                            {
                                b.BreakEfficiencies.Clear();
                            }

                            #endregion Update Break Efficiency
                        }
                    }
                }

                //add breaks
                var newBreaks = scheduleModel.Breaks.Where(mb => updatedEntity.Breaks.All(b => b.Id != mb.Id)).ToList();
                if (newBreaks.Any())
                {
                    var idx = 0;
                    foreach (var b in _mapper.Map <List <ScheduleBreak> >(newBreaks, opts => opts.UseEntityCache(_salesAreaByNameCache)))
                    {
                        var efficiencyList = newBreaks[idx].BreakEfficiencyList;
                        if (efficiencyList?.Any() ?? false)
                        {
                            b.BreakEfficiencies = _mapper.Map <List <ScheduleBreakEfficiency> >(efficiencyList);
                        }
                        updatedEntity.Breaks.Add(b);
                    }
                }
            }
            else
            {
                updatedEntity.Breaks.Clear();
            }
        }