Radarr/NzbDrone.Web/Controllers/SeriesController.cs

258 lines
11 KiB
C#
Raw Normal View History

2010-09-23 03:19:47 +00:00
using System;
using System.Collections.Generic;
using System.IO;
2010-09-23 03:19:47 +00:00
using System.Linq;
using System.Web.Mvc;
2012-02-16 05:53:00 +00:00
using System.Web.Script.Serialization;
2011-06-23 06:56:17 +00:00
using MvcMiniProfiler;
using NzbDrone.Common.Model;
using NzbDrone.Core;
using NzbDrone.Core.Helpers;
2011-12-02 01:33:17 +00:00
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Repository;
2012-01-20 07:50:45 +00:00
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Web.Models;
2010-09-23 03:19:47 +00:00
namespace NzbDrone.Web.Controllers
{
[HandleError]
2010-09-23 03:19:47 +00:00
public class SeriesController : Controller
{
private readonly EpisodeProvider _episodeProvider;
2011-04-10 02:44:01 +00:00
private readonly QualityProvider _qualityProvider;
private readonly SeriesProvider _seriesProvider;
private readonly JobProvider _jobProvider;
private readonly SeasonProvider _seasonProvider;
2010-09-23 03:19:47 +00:00
//
// GET: /Series/
public SeriesController(SeriesProvider seriesProvider, EpisodeProvider episodeProvider,
QualityProvider qualityProvider, JobProvider jobProvider,
SeasonProvider seasonProvider)
2010-09-23 03:19:47 +00:00
{
_seriesProvider = seriesProvider;
2010-10-05 06:21:18 +00:00
_episodeProvider = episodeProvider;
_qualityProvider = qualityProvider;
_jobProvider = jobProvider;
_seasonProvider = seasonProvider;
2010-09-23 03:19:47 +00:00
}
public ActionResult Index()
{
2012-02-16 05:53:00 +00:00
var series = GetSeriesModels(_seriesProvider.GetAllSeriesWithEpisodeCount());
var serialized = new JavaScriptSerializer().Serialize(series);
return View((object)serialized);
}
public ActionResult SingleSeriesEditor(int seriesId)
2010-09-23 03:19:47 +00:00
{
2011-07-08 05:41:08 +00:00
var profiles = _qualityProvider.All();
ViewData["SelectList"] = new SelectList(profiles, "QualityProfileId", "Name");
var backlogStatusTypes = new List<KeyValuePair<int, string>>();
2012-01-26 01:02:21 +00:00
foreach (BacklogSettingType backlogStatusType in Enum.GetValues(typeof(BacklogSettingType)))
{
backlogStatusTypes.Add(new KeyValuePair<int, string>((int)backlogStatusType, backlogStatusType.ToString()));
}
2012-01-26 01:02:21 +00:00
ViewData["BacklogSettingSelectList"] = new SelectList(backlogStatusTypes, "Key", "Value");
var series = GetSeriesModels(new List<Series>{_seriesProvider.GetSeries(seriesId)}).Single();
return View(series);
2010-09-23 03:19:47 +00:00
}
[HttpPost]
public EmptyResult SaveSingleSeriesEditor(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);
2011-05-18 05:32:23 +00:00
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 = _seasonProvider.All(seriesId).Select(s => new SeasonModel
{
SeriesId = seriesId,
SeasonNumber = s.SeasonNumber,
Ignored = s.Ignored,
Episodes = GetEpisodeModels(s.Episodes).OrderByDescending(e => e.EpisodeNumber).ToList(),
CommonStatus = GetCommonStatus(s.Episodes)
}).ToList();
model.Seasons = seasons;
return View(model);
}
public ActionResult Editor()
2012-01-20 07:50:45 +00:00
{
var profiles = _qualityProvider.All();
ViewData["QualityProfiles"] = profiles;
//Create the select lists
var masterProfiles = profiles.ToList();
masterProfiles.Insert(0, new QualityProfile {QualityProfileId = -10, Name = "Select..."});
2012-01-20 07:50:45 +00:00
ViewData["MasterProfileSelectList"] = new SelectList(masterProfiles, "QualityProfileId", "Name");
ViewData["BoolSelectList"] = new SelectList(new List<KeyValuePair<int, string>>
{
new KeyValuePair<int, string>(-10, "Select..."),
2012-01-20 07:50:45 +00:00
new KeyValuePair<int, string>(1, "True"),
new KeyValuePair<int, string>(0, "False")
}, "Key", "Value"
);
2012-01-26 01:02:21 +00:00
var backlogSettingTypes = new List<KeyValuePair<int, string>>();
2012-01-26 01:02:21 +00:00
foreach (BacklogSettingType backlogSettingType in Enum.GetValues(typeof(BacklogSettingType)))
{
2012-01-26 01:02:21 +00:00
backlogSettingTypes.Add(new KeyValuePair<int, string>((int)backlogSettingType, backlogSettingType.ToString()));
}
2012-01-26 01:02:21 +00:00
ViewData["BacklogSettingTypes"] = backlogSettingTypes;
2012-01-26 01:02:21 +00:00
var masterBacklogList = backlogSettingTypes.ToList();
masterBacklogList.Insert(0, new KeyValuePair<int, string>(-10, "Select..."));
2012-01-26 01:02:21 +00:00
ViewData["MasterBacklogSettingSelectList"] = new SelectList(masterBacklogList, "Key", "Value");
2012-01-20 07:50:45 +00:00
var series = _seriesProvider.GetAllSeries().OrderBy(o => SortHelper.SkipArticles(o.Title));
return View(series);
}
[HttpPost]
public JsonResult SaveEditor(List<Series> series)
2012-01-20 07:50:45 +00:00
{
//Save edits
if (series == null || series.Count == 0)
return JsonNotificationResult.Oops("Invalid post data");
2012-01-20 07:50:45 +00:00
_seriesProvider.UpdateFromSeriesEditor(series);
2012-01-20 07:50:45 +00:00
return JsonNotificationResult.Info("Series Mass Edit Saved");
}
private List<SeriesModel> GetSeriesModels(IList<Series> seriesInDb)
{
var series = seriesInDb.Select(s => new SeriesModel
{
SeriesId = s.SeriesId,
Title = s.Title,
2012-02-16 05:53:00 +00:00
TitleSorter = SortHelper.SkipArticles(s.Title),
AirsDayOfWeek = s.AirsDayOfWeek.ToString(),
Monitored = s.Monitored,
Overview = s.Overview,
Path = s.Path,
QualityProfileId = s.QualityProfileId,
QualityProfileName = s.QualityProfile.Name,
Network = s.Network,
SeasonFolder = s.SeasonFolder,
2012-01-26 01:02:21 +00:00
BacklogSetting = (int)s.BacklogSetting,
Status = s.Status,
SeasonsCount = s.SeasonCount,
EpisodeCount = s.EpisodeCount,
EpisodeFileCount = s.EpisodeFileCount,
2012-02-16 05:53:00 +00:00
NextAiring = s.NextAiring == null ? String.Empty : s.NextAiring.Value.ToBestDateString(),
NextAiringSorter = s.NextAiring == null ? "12/31/9999" : s.NextAiring.Value.ToString("MM/dd/yyyy"),
AirTime = s.AirTimes
}).ToList();
return series;
}
private List<EpisodeModel> GetEpisodeModels(IList<Episode> episodesInDb)
{
var episodes = new List<EpisodeModel>();
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,
Status = e.Status.ToString(),
Quality = episodeQuality,
Ignored = e.Ignored
});
}
return episodes;
}
private string GetCommonStatus(IList<Episode> episodes)
{
var commonStatusList = episodes.Select(s => s.Status).Distinct().ToList();
var commonStatus = commonStatusList.Count > 1 ? "Missing" : commonStatusList.First().ToString();
return commonStatus;
}
2010-09-23 03:19:47 +00:00
}
2011-04-10 02:44:01 +00:00
}