public override void Handle(IBulkBreakCreated command) { _metadataRepository.ValidateBreakType(command.Data.Select(b => b.BreakType).ToList()); _salesAreaRepository.ValidateSalesArea(command.Data.Select(s => s.SalesArea).ToList()); var salesAreaDictionary = _salesAreaRepository.GetAll() .ToDictionary(x => x.Name, x => x.CustomId); var breaks = _mapper.Map <List <Break> >(command.Data); // Populate break data counting custom ID from zero for each day var breakGroups = breaks.GroupBy(s => new { s.ScheduledDate.Date, s.SalesArea }); foreach (var breakGroup in breakGroups) { PopulateBreakProperties(breakGroup.ToList(), salesAreaDictionary); } _breakRepository.Add(breaks); // Note: this implies that any batch of breaks recieved should be treated as the only // schedule breaks for a schedule day ie. overwrite list not append to list foreach (var breakGroup in breakGroups) { var schedule = _scheduleRepository.GetSchedule(breakGroup.Key.SalesArea, breakGroup.Key.Date) ?? new Schedule { SalesArea = breakGroup.Key.SalesArea, Date = breakGroup.Key.Date }; schedule.Breaks = breakGroup.ToList(); _scheduleRepository.Add(schedule); } _scheduleRepository.SaveChanges(); }
public IHttpActionResult Post([FromBody] CreatePassModel command) { // Check empty name if (string.IsNullOrWhiteSpace(command.Name)) { return(this.Error().InvalidParameters("Pass name must be set")); } if (!ModelState.IsValid) { return(this.Error().InvalidParameters("Invalid pass parameters")); } // Check that pass doesn't exist Pass pass = _passRepository.Get(command.Id); if (pass != null) { return(this.Error().InvalidParameters("Pass already exists")); } // Check that pass with such name doesn't exist pass = _passRepository.FindByName(command.Name, command.IsLibraried); if (pass != null) { return(this.Error().InvalidParameters("Pass with such name already exists")); } if (_passInspectorService.InspectPassSalesAreaPriorities( command.PassSalesAreaPriorities, out string errorMessage)) { return(this.Error().InvalidParameters(errorMessage)); } List <string> breakExclusionsErrorMessages = new List <string>(); if (!BreakExclusionsValidations.DateTimeRangeIsValid(command.BreakExclusions, _salesAreaRepository.GetAll(), out breakExclusionsErrorMessages)) { return(this.Error().InvalidParameters(breakExclusionsErrorMessages)); } pass = _mapper.Map <Pass>(command); IdUpdater.SetIds(pass, _identityGeneratorResolver); // Validate ValidateForSave(pass); // Save _passRepository.Add(pass); _passRepository.SaveChanges(); // Do not remove this, need to persist changes now so that we can return PassModel return(Ok(Mappings.MapToPassModel(pass, _passRepository, _mapper))); }
private List <FailureModel> GetFailureModels(IEnumerable <Failure> failures) { // Index sales areas by name var salesAreasByName = SalesArea.IndexListByName(_salesAreaRepository.GetAll()); // Map Failure to FailureModel var failuresModels = _mapper.Map <List <FailureModel> >(failures); failuresModels.ForEach(fm => fm.SalesAreaShortName = salesAreasByName.ContainsKey(fm.SalesAreaName) ? salesAreasByName[fm.SalesAreaName].ShortName : fm.SalesAreaShortName); return(failuresModels); }
private List <SalesArea> GetSalesAreasFromDatabase(List <string> salesAreaNames) { var salesAreas = salesAreaNames == null || !salesAreaNames.Any() ? _salesAreaRepository.GetAll().ToList() : _salesAreaRepository.FindByNames(salesAreaNames); if (salesAreas == null || !salesAreas.Any()) { throw new DataSyncException(DataSyncErrorCode.SalesAreaNotFound, "SalesArea not found"); } return(salesAreas); }
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(); }
public override void Handle(IBulkSpotCreatedOrUpdated command) { ValidateBookingPositions(command); var salesAreaDictionary = _salesAreaRepository.GetAll().ToDictionary(x => x.Name, x => x.CustomId); var spotEntities = _mapper.Map <List <SpotDbObject> >(command.Data); spotEntities.ForEach(s => { s.Uid = Guid.NewGuid(); var salesAreaCustomId = salesAreaDictionary[s.SalesArea]; s.ExternalBreakNo = s.ExternalBreakNo.GenerateBreakExternalRef(salesAreaCustomId, s.StartDateTime); }); _spotRepository.InsertOrReplace(spotEntities); _spotRepository.SaveChanges(); }
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()); }
/// <summary> /// Creates recommendations data /// </summary> /// <param name="numberOfSpots">Number of spots to generate data for</param> /// <returns></returns> public List <Recommendation> Create(int numberOfSpots) { var recommendations = new List <Recommendation>(); _random = new Random(); DateTime processorDateTime = DateTime.UtcNow; // Load data var spots = _spotRepository.GetAll(); // Doesn't actually return all var salesAreas = _salesAreaRepository.GetAll(); var programmes = _programmeRepository.GetAll(); // Doesn't actually return all numberOfSpots = (numberOfSpots > spots.ToList().Count ? spots.ToList().Count : numberOfSpots); // If insufficient spots then return max number if (!spots.Any()) { throw new ArgumentException("Cannot generate fake recommendations data because there are no spots in the database"); } if (!programmes.Any()) { throw new ArgumentException("Cannot generate fake recommendations data because there are no programmes in the database"); } // Add data for each spot int programmeIndex = -1; for (int spotIndex = 0; spotIndex < numberOfSpots; spotIndex++) { programmeIndex++; programmeIndex = (programmeIndex >= programmes.ToList().Count - 1 ? 0 : programmeIndex); var spot = spots.ToList()[spotIndex]; var salesArea = salesAreas.Where(x => x.Name.Equals(spot.SalesArea)).First(); var programme = programmes.ToList()[++programmeIndex]; recommendations.Add(CreateRecommendation(spots.ToList()[spotIndex], salesArea, programme, processorDateTime)); } return(recommendations); }
public IEnumerable <SalesAreaModel> Get() { var salesAreas = _salesAreaRepository.GetAll().ToList(); return(_mapper.Map <List <SalesAreaModel> >(salesAreas)); }
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))); }
public IEnumerable <SalesArea> GetAll() => _salesAreaRepository.GetAll();