/// <summary> /// Creates set of spots /// </summary> /// <param name="spots">entities to be persisted</param> /// <returns>List of created entities</returns> private IEnumerable <Spot> CreateSpots(List <CreateSpot> spots) { var createdSpots = new List <Spot>(); bool isScheduleDataUploadStarted = false; using (MachineLock.Create("xggameplan.checkisscheduledatauploadstarted", TimeSpan.FromSeconds(30))) { isScheduleDataUploadStarted = _repository.CountAll == 0 && _breakRepository.CountAll == 0; } foreach (var spot in spots) { var dto = _mapper.Map <Spot>(spot); dto.Uid = Guid.NewGuid(); _repository.Add(dto); createdSpots.Add(dto); } // Generate notification for schedule data upload started if (isScheduleDataUploadStarted) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForScheduleDataUploadStarted(0, 0, null)); } return(createdSpots); }
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()); }