public IHttpActionResult Delete([FromUri] List <string> externalSpotRef) { externalSpotRef = externalSpotRef?.Where(name => !string.IsNullOrWhiteSpace(name)).ToList(); if (externalSpotRef == null || !externalSpotRef.Any()) { ModelState.AddModelError(nameof(externalSpotRef), "External spot references are missing"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var spots = _repository.FindByExternal(externalSpotRef).ToList(); if (spots.Count == 0) { return(NotFound()); } var multipartSpots = new Lazy <List <Spot> >(() => _repository.GetAllMultipart().ToList()); var spotsToDelete = new List <Guid>(); foreach (var spot in spots) { if (!spot.IsMultipartSpot) { spotsToDelete.Add(spot.Uid); } else if (spot.IsTopMultipart()) { spotsToDelete.AddRange( BreakUtilities.GetLinkedMultipartSpots(spot, multipartSpots.Value, true) .Select(s => s.Uid) ); } } _repository.Delete(spotsToDelete.Distinct()); return(Ok()); }
public IHttpActionResult Delete([FromUri] DateTime dateRangeStart, [FromUri] DateTime dateRangeEnd, [FromUri] List <string> salesAreaNames) { var messages = ValidateForDelete(dateRangeStart, dateRangeEnd, salesAreaNames); foreach (var message in messages) { ModelState.AddModelError(message.Key, message.Value); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // broadcast day var defaultBroadcastStartTime = new TimeSpan(6, 0, 0); var defaultBroadcastEndTime = defaultBroadcastStartTime.Subtract(new TimeSpan(0, 0, 1)); var broadcastStartDateTime = dateRangeStart.Date.Add(defaultBroadcastStartTime); var broadcastEndDateTime = dateRangeEnd.Date.AddDays(1).Add(defaultBroadcastEndTime); var scheduledDatesRange = new DateTimeRange(broadcastStartDateTime, broadcastEndDateTime); var breaks = _breakRepository.Search(scheduledDatesRange, salesAreaNames).ToList(); if (!breaks.Any()) { return(NotFound()); } var breakIdsToDelete = new HashSet <Guid>(); var breakExtRefsToDelete = new List <string>(); foreach (var oneBreak in breaks) { breakIdsToDelete.Add(oneBreak.Id); breakExtRefsToDelete.Add(oneBreak.ExternalBreakRef); } // load spots and schedules to be updated var spotsToDelete = _spotRepository.FindByExternalBreakNumbers(breakExtRefsToDelete); var schedulesToUpdate = _scheduleRepository.FindByBreakIds(breakIdsToDelete); // delete spots first and persist changes if (spotsToDelete.Any()) { _spotRepository.Delete(spotsToDelete.Select(s => s.Uid)); _spotRepository.SaveChanges(); } _breakRepository.Delete(breakIdsToDelete.ToList()); foreach (var schedule in schedulesToUpdate) { schedule.Breaks = schedule.Breaks.Where(b => !breakIdsToDelete.Contains(b.Id)).ToList(); _scheduleRepository.Update(schedule); _scheduleRepository.SaveChanges(); } return(Ok()); }