using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web.Mvc; using MvcMiniProfiler; using NzbDrone.Common.Model; using NzbDrone.Core; using NzbDrone.Core.Helpers; using NzbDrone.Core.Jobs; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Repository; using NzbDrone.Core.Repository.Quality; using NzbDrone.Web.Models; using Telerik.Web.Mvc; namespace NzbDrone.Web.Controllers { [HandleError] public class SeriesController : Controller { private readonly EpisodeProvider _episodeProvider; private readonly QualityProvider _qualityProvider; private readonly SeriesProvider _seriesProvider; private readonly JobProvider _jobProvider; // // GET: /Series/ public SeriesController(SeriesProvider seriesProvider, EpisodeProvider episodeProvider, QualityProvider qualityProvider, JobProvider jobProvider) { _seriesProvider = seriesProvider; _episodeProvider = episodeProvider; _qualityProvider = qualityProvider; _jobProvider = jobProvider; } public ActionResult Index() { var series = GetSeriesModels(_seriesProvider.GetAllSeriesWithEpisodeCount()).OrderBy(o => SortHelper.SkipArticles(o.Title)); return View(series); } public ActionResult SeriesEditor(int seriesId) { var profiles = _qualityProvider.All(); ViewData["SelectList"] = new SelectList(profiles, "QualityProfileId", "Name"); var backlogStatusTypes = new List>(); foreach (BacklogSettingType backlogStatusType in Enum.GetValues(typeof(BacklogSettingType))) { backlogStatusTypes.Add(new KeyValuePair((int)backlogStatusType, backlogStatusType.ToString())); } ViewData["BacklogSettingSelectList"] = new SelectList(backlogStatusTypes, "Key", "Value"); var series = GetSeriesModels(new List{_seriesProvider.GetSeries(seriesId)}).Single(); return View(series); } [HttpPost] public EmptyResult SaveSeriesEditor(SeriesModel seriesModel) { var series = _seriesProvider.GetSeries(seriesModel.SeriesId); series.Monitored = seriesModel.Monitored; series.SeasonFolder = seriesModel.SeasonFolder; series.QualityProfileId = seriesModel.QualityProfileId; series.Path = seriesModel.Path; series.BacklogSetting = (BacklogSettingType)seriesModel.BacklogSetting; _seriesProvider.UpdateSeries(series); return new EmptyResult(); } [HttpPost] public EmptyResult DeleteSeries(int seriesId) { _jobProvider.QueueJob(typeof(DeleteSeriesJob), seriesId); return new EmptyResult(); } public JsonResult LocalSearch(string term) { //Get Results from the local DB and return var results = _seriesProvider.SearchForSeries(term).Select(s => new SeriesSearchResultModel { Id = s.SeriesId, Title = s.Title }).ToList(); return Json(results, JsonRequestBehavior.AllowGet); } public ActionResult Details(int seriesId) { var series = _seriesProvider.GetSeries(seriesId); var model = new SeriesDetailsModel(); if (series.AirsDayOfWeek != null) { model.AirsDayOfWeek = series.AirsDayOfWeek.Value.ToString(); } else { model.AirsDayOfWeek = "N/A"; } model.Overview = series.Overview; model.Title = series.Title; model.SeriesId = series.SeriesId; model.HasBanner = !String.IsNullOrEmpty(series.BannerUrl); var seasons = new List(); var episodes = _episodeProvider.GetEpisodeBySeries(seriesId); foreach (var season in episodes.Select(s => s.SeasonNumber).Distinct()) { var episodesInSeason = episodes.Where(e => e.SeasonNumber == season).ToList(); var commonStatusList = episodes.Select(s => s.Status).Distinct().ToList(); var commonStatus = commonStatusList.Count > 1 ? "Missing" : commonStatusList.First().ToString(); seasons.Add(new SeasonModel { SeriesId = seriesId, SeasonNumber = season, Episodes = GetEpisodeModels(episodesInSeason).OrderByDescending(e=> e.EpisodeNumber).ToList(), AnyWanted = episodesInSeason.Any(e => !e.Ignored), CommonStatus = commonStatus }); } model.Seasons = seasons; return View(model); } public ActionResult MassEdit() { var profiles = _qualityProvider.All(); ViewData["QualityProfiles"] = profiles; //Create the select lists var masterProfiles = profiles.ToList(); masterProfiles.Insert(0, new QualityProfile {QualityProfileId = -10, Name = "Unchanged"}); ViewData["MasterProfileSelectList"] = new SelectList(masterProfiles, "QualityProfileId", "Name"); ViewData["BoolSelectList"] = new SelectList(new List> { new KeyValuePair(-10, "Unchanged"), new KeyValuePair(1, "True"), new KeyValuePair(0, "False") }, "Key", "Value" ); var backlogSettingTypes = new List>(); foreach (BacklogSettingType backlogSettingType in Enum.GetValues(typeof(BacklogSettingType))) { backlogSettingTypes.Add(new KeyValuePair((int)backlogSettingType, backlogSettingType.ToString())); } ViewData["BacklogSettingTypes"] = backlogSettingTypes; var masterBacklogList = backlogSettingTypes.ToList(); masterBacklogList.Insert(0, new KeyValuePair(-10, "Unchanged")); ViewData["MasterBacklogSettingSelectList"] = new SelectList(masterBacklogList, "Key", "Value"); var series = _seriesProvider.GetAllSeries().OrderBy(o => SortHelper.SkipArticles(o.Title)); return View(series); } [HttpPost] public JsonResult SaveMassEdit(List series) { //Save edits if (series == null || series.Count == 0) return JsonNotificationResult.Opps("Invalid post data"); _seriesProvider.UpdateFromMassEdit(series); return JsonNotificationResult.Info("Series Mass Edit Saved"); } private List GetSeriesModels(IList seriesInDb) { var series = seriesInDb.Select(s => new SeriesModel { SeriesId = s.SeriesId, Title = s.Title, AirsDayOfWeek = s.AirsDayOfWeek.ToString(), Monitored = s.Monitored, Overview = s.Overview, Path = s.Path, QualityProfileId = s.QualityProfileId, QualityProfileName = s.QualityProfile.Name, SeasonFolder = s.SeasonFolder, BacklogSetting = (int)s.BacklogSetting, Status = s.Status, SeasonsCount = s.SeasonCount, EpisodeCount = s.EpisodeCount, EpisodeFileCount = s.EpisodeFileCount, NextAiring = s.NextAiring == null ? String.Empty : s.NextAiring.Value.ToBestDateString() }).ToList(); return series; } private List GetEpisodeModels(IList episodesInDb) { var episodes = new List(); foreach (var e in episodesInDb) { var episodeFileId = 0; var episodePath = String.Empty; var episodeQuality = "N/A"; if (e.EpisodeFile != null) { episodePath = e.EpisodeFile.Path; episodeFileId = e.EpisodeFile.EpisodeFileId; episodeQuality = e.EpisodeFile.Quality.ToString(); } var airDate = String.Empty; if (e.AirDate != null) airDate = e.AirDate.Value.ToBestDateString(); episodes.Add(new EpisodeModel { EpisodeId = e.EpisodeId, EpisodeNumber = e.EpisodeNumber, SeasonNumber = e.SeasonNumber, Title = e.Title, Overview = e.Overview, AirDate = airDate, Path = episodePath, EpisodeFileId = episodeFileId, Status = e.Status.ToString(), Quality = episodeQuality, Ignored = e.Ignored }); } return episodes; } } }