Esempio n. 1
0
        public IHttpActionResult PostScenario([FromBody] CreateScenarioModel command)
        {
            // Check empty name
            if (string.IsNullOrWhiteSpace(command.Name))
            {
                return(this.Error().InvalidParameters("No name was entered"));
            }

            if (!ModelState.IsValid)
            {
                return(this.Error().InvalidParameters("Invalid scenario parameters"));
            }

            // Check that scenario doesn't exist
            Scenario scenario = _scenarioRepository.Get(command.Id);

            if (scenario != null)
            {
                return(this.Error().InvalidParameters("Scenario already exists"));
            }

            // Check that scenario with such name doesn't exist
            scenario = _scenarioRepository.FindByName(command.Name, command.IsLibraried);
            if (scenario != null)
            {
                return(this.Error().InvalidParameters("Scenario with such name already exists"));
            }

            foreach (var pass in command.Passes)
            {
                if (_passInspectorService.InspectPassSalesAreaPriorities(
                        pass.PassSalesAreaPriorities,
                        out string errorMessage))
                {
                    return(this.Error().InvalidParameters(errorMessage));
                }

                List <string> errorMessages = new List <string>();

                if (!BreakExclusionsValidations.DateTimeRangeIsValid(pass.BreakExclusions, _salesAreaRepository.GetAll(), out errorMessages))
                {
                    return(this.Error().InvalidParameters(errorMessages));
                }
            }

            ValidateCampaignPriorityRounds(command.CampaignPriorityRounds);

            // Add scenario
            scenario = _mapper.Map <Scenario>(command);

            IdUpdater.SetIds(scenario, _identityGeneratorResolver);

            // Update Pass repository
            UpdatePassRepository(scenario, command, null);
            _passRepository.SaveChanges();

            // Add Campaign Pass Priorities to Scenario
            var campaignsResult   = _campaignRepository.GetWithProduct(null);
            var usingAllCampaigns = campaignsResult.Items?.Any() == true?campaignsResult.Items.ToList() : null;

            var forScenarioPasses = _passRepository.FindByIds(scenario.Passes.Select(p => p.Id)).ToList();

            var allCampaigns = usingAllCampaigns?.ToDictionary(x => x.ExternalId, x => x);

            CampaignPassPrioritiesServiceMapper.AmendCampaignPassPrioritiesForNewCampaigns(
                scenario,
                forScenarioPasses,
                allCampaigns,
                _passRepository,
                _mapper);

            // Validate
            ValidateForSave(scenario);

            scenario.DateUserModified = DateTime.UtcNow;

            // Add scenario
            _scenarioRepository.Add(scenario);
            _scenarioRepository.SaveChanges();   // Do not remove this, need to persist changes now so that we can return ScenarioModel

            return(Ok(Mappings.MapToScenarioModel(scenario, _scenarioRepository, _passRepository,
                                                  _tenantSettingsRepository, _mapper)));
        }