public ActionResult Save(string seriesId) { var series = _searchService.GetTvSeries(seriesId); _service.AddOrUpdate(series); _service.SaveChanges(); return(RedirectToAction("Index")); }
public void Update() { _logger.Info("Updating TV Database..."); _analyticsService.ReportEvent(AnalyticEvent.DbUpdate); var seriesList = _context.TvSeries .Where(s => SqlFunctions.DateDiff("day", s.LastUpdated, DateTime.Now) >= 7) .Select(s => new { s.Id, s.TvDbSeriesId, s.Name, s.LastUpdated }).ToList(); foreach (var series in seriesList) { if (!_searchService.HasUpdates(series.TvDbSeriesId, series.LastUpdated)) { continue; } _logger.Info("Updates found for Series: {0}", series.Name); var currentSeries = _context.TvSeries.Include(s => s.Episodes).FirstOrDefault(s => s.TvDbSeriesId == series.TvDbSeriesId); var updatedSeries = _searchService.GetTvSeries(series.TvDbSeriesId); if (currentSeries == null) { _context.TvSeries.AddOrUpdate(updatedSeries); _context.SaveChanges(); return; } currentSeries.Name = updatedSeries.Name.Trim(new [] { '.' }); currentSeries.Network = updatedSeries.Network; currentSeries.AirsDayOfWeek = updatedSeries.AirsDayOfWeek; currentSeries.AirsTimeOfDay = updatedSeries.AirsTimeOfDay; currentSeries.FirstAired = updatedSeries.FirstAired; currentSeries.Rating = updatedSeries.Rating; currentSeries.Status = updatedSeries.Status; currentSeries.Summary = updatedSeries.Summary; currentSeries.LastUpdated = DateTime.Now; foreach (var episode in updatedSeries.Episodes) { var currentEpisode = currentSeries.Episodes.FirstOrDefault( e => e.Season == episode.Season && e.EpisodeNumber == episode.EpisodeNumber); if (currentEpisode == null) { episode.DownloadStatus = (episode.Season > 0 && episode.EpisodeNumber > 0) ? "WANT" : "SKIP"; currentSeries.Episodes.Add(episode); continue; } currentEpisode.FirstAired = episode.FirstAired; currentEpisode.Summary = episode.Summary; currentEpisode.Title = episode.Title; currentEpisode.DownloadStatus = episode.DownloadStatus; } _context.TvSeries.AddOrUpdate(t => t.TvDbSeriesId, currentSeries); } _context.SaveChanges(); _logger.Info("TV Database Update complete."); }