public void UpdateAvailability(Break theBreak)
        {
            _breakRepository.Add(theBreak);
            BreaksHaveChanges = true;

            if (_scheduleUtilities is null)
            {
                return;
            }

            var breakExternalReference = theBreak.ExternalBreakRef;

            var(schedule, message) = _scheduleUtilities.FindScheduleForBreak(theBreak);
            if (schedule is null)
            {
                _logger.LogWarning(LogPrologue(breakExternalReference) + message);
                return;
            }

            var breakForSchedule = schedule.GetBreak(breakExternalReference);

            if (breakForSchedule is null)
            {
                _logger.LogWarning(LogPrologue(breakExternalReference) + "No schedule break");
                return;
            }

            if (UpdateScheduleBreakAvailIfChanged(breakForSchedule, theBreak.Avail))
            {
                ScheduleBreaksHaveChanges = true;
            }
        }
Beispiel #2
0
        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();
        }
Beispiel #3
0
        public IHttpActionResult Post([FromBody] List <CreateBreak> breaks)
        {
            if (breaks is null || !breaks.Any() || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Determine if they've just started uploading schedule data
            bool isScheduleDataUploadStarted = false;

            using (MachineLock.Create("xggameplan.checkisscheduledatauploadstarted", TimeSpan.FromSeconds(30)))
            {
                isScheduleDataUploadStarted = _breakRepository.CountAll == 0 && _spotRepository.CountAll == 0;
            }

            ValidateBreaks(breaks);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // zero out BroadcastDate's times
            foreach (var b in breaks)
            {
                if (b.BroadcastDate.HasValue)
                {
                    b.BroadcastDate = b.BroadcastDate.Value.Date;
                }
            }

            // group by date and channels
            breaks.GroupBy(s => new { s.ScheduledDate.Date, s.SalesArea }).ForEach(grp =>
            {
                using (MachineLock.Create(string.Format("xggameplan.scheduleday.{0}.{1}", grp.Key.SalesArea, grp.Key.Date), new TimeSpan(0, 10, 0)))
                {
                    var schedule  = _scheduleRepository.GetOrCreateSchedule(grp.Key.SalesArea, grp.Key.Date);
                    var breaklist = _mapper.Map <List <Break> >(grp.ToList());

                    LoadBreakProperties(ref breaklist);

                    _breakRepository.Add(breaklist);

                    schedule.Breaks = breaklist.ToList();
                    _scheduleRepository.Add(schedule);
                    _scheduleRepository.SaveChanges();
                }
            });

            // Generate notification for schedule data upload started
            if (isScheduleDataUploadStarted)
            {
                _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForScheduleDataUploadStarted(0, 0, null));
            }

            return(Ok());
        }