Example #1
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();
        }