예제 #1
0
        public IHttpActionResult Put(Guid id, [FromBody] CreateSalesAreaModel command)
        {
            if (command == null || !ModelState.IsValid)
            {
                return(this.Error().InvalidParameters());
            }

            var salesArea = _salesAreaRepository.Find(id);

            if (salesArea == null)
            {
                return(NotFound());
            }

            Validate(command.BaseDemographic1, command.BaseDemographic2);

            //Input values are validated by calling Validate method as part of Update
            salesArea.Update(id,
                             command.Name,
                             command.ShortName,
                             command.CurrencyCode,
                             command.BaseDemographic1,
                             command.BaseDemographic2,
                             command.ChannelGroup,
                             command.StartOffset,
                             command.DayDuration);
            _salesAreaRepository.Update(salesArea);

            return(Ok(_mapper.Map <SalesAreaModel>(salesArea)));
        }
        public override void Handle(IBulkSalesAreaCreatedOrUpdated command)
        {
            ValidateSalesAreaDemographics(command);

            var customIds          = command.Data.Select(x => x.CustomId).ToList();
            var existingSalesAreas = _salesAreaRepository.FindByIds(customIds)
                                     .ToDictionary(x => x.CustomId);

            var salesAreasWithDemographics = new Dictionary <SalesArea, List <SalesAreaDemographic> >();
            var resultSalesAreas           = new List <SalesArea>();

            foreach (var item in command.Data)
            {
                var demographics = _mapper.Map <List <SalesAreaDemographic> >(item.Demographics);
                var newSalesArea = _mapper.Map <SalesArea>(item);

                if (existingSalesAreas.TryGetValue(item.CustomId, out SalesArea salesArea))
                {
                    UpdateSalesAreaModel(salesArea, newSalesArea);
                }
                else
                {
                    salesArea    = newSalesArea;
                    salesArea.Id = System.Guid.NewGuid();
                }

                resultSalesAreas.Add(salesArea);
                salesAreasWithDemographics.Add(salesArea, demographics);
            }

            _salesAreaRepository.Update(resultSalesAreas);
            UpdateSalesAreaDemographics(salesAreasWithDemographics);
            _salesAreaDemographicRepository.SaveChanges();
        }
예제 #3
0
        public override void Handle(ISalesAreaUpdated command)
        {
            var salesArea = _salesAreaRepository.FindByCustomId(command.CustomId);

            if (salesArea is null)
            {
                throw new DataSyncException(DataSyncErrorCode.SalesAreaNotFound, "no sales area found");
            }

            _demographicRepository.ValidateDemographics(new List <string> {
                command.BaseDemographic1, command.BaseDemographic2
            });
            ValidateSalesAreaDemographics(command);

            var newSalesArea = _mapper.Map <SalesArea>(command);

            UpdateSalesAreaModel(salesArea, newSalesArea);
            _salesAreaRepository.Update(salesArea);

            var demographics = _mapper.Map <IEnumerable <SalesAreaDemographic> >(command.Demographics).ToList();

            demographics.ForEach(x => x.SalesArea = salesArea.Name);
            UpdateSalesAreaDemographics(salesArea, demographics);

            _salesAreaRepository.SaveChanges();
        }
예제 #4
0
        public IHttpActionResult Post([FromBody] CreateHoliday command)
        {
            if (!ModelState.IsValid)
            {
                return(this.Error().InvalidParameters());
            }

            List <SalesArea> salesAreas;

            if (command.SalesAreaNames == null || !command.SalesAreaNames.Any())
            {
                salesAreas = _salesAreaRepository.GetAll().ToList(); // Apply to all
            }
            else
            {
                salesAreas = _salesAreaRepository.FindByNames(command.SalesAreaNames);
            }

            if (salesAreas == null || !salesAreas.Any())
            {
                return(NotFound());
            }

            ValidateHolidays(command.HolidayDateRanges);
            salesAreas.ForEach(salesArea =>
            {
                switch (command.HolidayType)
                {
                case HolidayType.PublicHoliday:
                    salesArea.PublicHolidays = Add(salesArea.PublicHolidays, command.HolidayDateRanges);
                    break;

                case HolidayType.SchoolHoliday:
                    salesArea.SchoolHolidays = Add(salesArea.SchoolHolidays, command.HolidayDateRanges);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(command.HolidayType), command.HolidayType,
                                                          "Invalid holiday type");
                }
            });
            _salesAreaRepository.Update(salesAreas);
            return(Ok());
        }
예제 #5
0
        public override void Handle(IBulkHolidayCreated command)
        {
            foreach (var holiday in command.Data)
            {
                List <SalesArea> salesAreas = GetSalesAreasFromDatabase(holiday.SalesAreaNames);

                salesAreas.ForEach(salesArea =>
                {
                    Add(salesArea, holiday.HolidayDateRanges, holiday.HolidayType);
                });

                _salesAreaRepository.Update(salesAreas);
            }

            _salesAreaRepository.SaveChanges();
        }
        public override void Handle(IBulkHolidayDeleted command)
        {
            var salesAreas = _salesAreaRepository.GetAll().ToList();

            foreach (var commandItem in command.Data)
            {
                foreach (var salesArea in salesAreas)
                {
                    salesArea.PublicHolidays?.RemoveAll(c => c.Start >= commandItem.StartDate && c.End <= commandItem.EndDate);
                    salesArea.SchoolHolidays?.RemoveAll(c => c.Start >= commandItem.StartDate && c.End <= commandItem.EndDate);
                }
            }

            _salesAreaRepository.Update(salesAreas);
            _salesAreaRepository.SaveChanges();
        }