Lidarr/NzbDrone.Web/Controllers/SeriesController.cs

305 lines
12 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;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Jobs;
using NzbDrone.Core.Repository;
using NzbDrone.Web.Models;
using Telerik.Web.Mvc;
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;
private readonly MediaFileProvider _mediaFileProvider;
2011-04-10 02:44:01 +00:00
private readonly QualityProvider _qualityProvider;
2011-04-09 00:08:03 +00:00
private readonly RenameProvider _renameProvider;
2011-04-08 15:10:46 +00:00
private readonly RootDirProvider _rootDirProvider;
2011-04-10 02:44:01 +00:00
private readonly SeriesProvider _seriesProvider;
private readonly TvDbProvider _tvDbProvider;
private readonly JobProvider _jobProvider;
2010-09-23 03:19:47 +00:00
//
// GET: /Series/
public SeriesController(SeriesProvider seriesProvider,
2011-04-20 01:20:20 +00:00
EpisodeProvider episodeProvider,
2011-04-10 02:44:01 +00:00
QualityProvider qualityProvider, MediaFileProvider mediaFileProvider,
RenameProvider renameProvider, RootDirProvider rootDirProvider,
TvDbProvider tvDbProvider, JobProvider jobProvider)
2010-09-23 03:19:47 +00:00
{
_seriesProvider = seriesProvider;
2010-10-05 06:21:18 +00:00
_episodeProvider = episodeProvider;
_qualityProvider = qualityProvider;
_mediaFileProvider = mediaFileProvider;
_renameProvider = renameProvider;
_rootDirProvider = rootDirProvider;
_tvDbProvider = tvDbProvider;
_jobProvider = jobProvider;
2010-09-23 03:19:47 +00:00
}
public ActionResult Index()
{
var profiles = _qualityProvider.GetAllProfiles();
ViewData["SelectList"] = new SelectList(profiles, "QualityProfileId", "Name");
2010-09-23 03:19:47 +00:00
return View();
}
public ActionResult RssSync()
{
_jobProvider.BeginExecute(typeof(RssSyncJob));
return RedirectToAction("Index");
}
2010-10-05 06:21:18 +00:00
public ActionResult LoadEpisodes(int seriesId)
2010-09-23 03:19:47 +00:00
{
_episodeProvider.RefreshEpisodeInfo(seriesId);
2010-10-05 06:21:18 +00:00
return RedirectToAction("Details", new
2011-04-10 02:44:01 +00:00
{
seriesId
});
2010-09-23 03:19:47 +00:00
}
[GridAction]
public ActionResult _AjaxSeriesGrid()
{
var series = GetSeriesModels(_seriesProvider.GetAllSeries().ToList());
return View(new GridModel(series));
}
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _SaveAjaxSeriesEditing(int id, string path, bool monitored, bool seasonFolder, int qualityProfileId)
{
var oldSeries = _seriesProvider.GetSeries(id);
oldSeries.Path = path;
oldSeries.Monitored = monitored;
oldSeries.SeasonFolder = seasonFolder;
oldSeries.QualityProfileId = qualityProfileId;
_seriesProvider.UpdateSeries(oldSeries);
var series = GetSeriesModels(_seriesProvider.GetAllSeries().ToList());
return View(new GridModel(series));
}
[GridAction]
public ActionResult _DeleteAjaxSeriesEditing(int id)
{
//Grab the series from the DB so we can remove it from the list we return to the client
var seriesInDb = _seriesProvider.GetAllSeries().ToList();
//Remove this so we don't send it back to the client (since it hasn't really been deleted yet)
seriesInDb.RemoveAll(s => s.SeriesId == id);
//Start removing this series
_jobProvider.BeginExecute(typeof(DeleteSeriesJob), id);
var series = GetSeriesModels(seriesInDb);
return View(new GridModel(series));
}
[GridAction]
public ActionResult _AjaxSeasonGrid(int seasonId)
{
2011-04-10 02:44:01 +00:00
var episodes = _episodeProvider.GetEpisodeBySeason(seasonId).Select(c => new EpisodeModel
{
EpisodeId = c.EpisodeId,
EpisodeNumber = c.EpisodeNumber,
SeasonNumber = c.SeasonNumber,
2011-04-10 02:44:01 +00:00
Title = c.Title,
Overview = c.Overview,
AirDate = c.AirDate,
Path = GetEpisodePath(c.EpisodeFile),
Quality = c.EpisodeFile == null
2011-04-10 02:44:01 +00:00
? String.Empty
: c.EpisodeFile.Quality.ToString()
2011-04-10 02:44:01 +00:00
});
return View(new GridModel(episodes));
}
[GridAction]
public ActionResult _CustomBinding(GridCommand command, int seasonId)
2010-10-08 05:50:01 +00:00
{
IEnumerable<Episode> data = GetData(command);
return View(new GridModel
2011-04-10 02:44:01 +00:00
{
Data = data,
Total = data.Count()
});
2010-10-08 05:50:01 +00:00
}
public ActionResult SearchForSeries(string seriesName)
{
var model = new List<SeriesSearchResultModel>();
//Get Results from TvDb and convert them to something we can use.
foreach (var tvdbSearchResult in _tvDbProvider.SearchSeries(seriesName))
{
model.Add(new SeriesSearchResultModel
{
TvDbId = tvdbSearchResult.Id,
TvDbName = tvdbSearchResult.SeriesName,
FirstAired = tvdbSearchResult.FirstAired
});
}
//model.Add(new SeriesSearchResultModel{ TvDbId = 12345, TvDbName = "30 Rock", FirstAired = DateTime.Today });
//model.Add(new SeriesSearchResultModel { TvDbId = 65432, TvDbName = "The Office (US)", FirstAired = DateTime.Today.AddDays(-100) });
return PartialView("SeriesSearchResults", model);
}
private IEnumerable<Episode> GetData(GridCommand command)
2010-10-08 05:50:01 +00:00
{
return null;
/*
IQueryable<Episode> data = .Orders;
//Apply filtering
if (command.FilterDescriptors.Any())
{
data = data.Where(ExpressionBuilder.Expression<Order>(command.FilterDescriptors));
}
// Apply sorting
foreach (SortDescriptor sortDescriptor in command.SortDescriptors)
{
if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
{
switch (sortDescriptor.Member)
{
case "OrderID":
data = data.OrderBy(ExpressionBuilder.Expression<Order, int>(sortDescriptor.Member));
break;
case "Customer.ContactName":
data = data.OrderBy(order => order.Customer.ContactName);
break;
case "ShipAddress":
data = data.OrderBy(order => order.ShipAddress);
break;
case "OrderDate":
data = data.OrderBy(order => order.OrderDate);
break;
}
}
else
{
switch (sortDescriptor.Member)
{
case "OrderID":
data = data.OrderByDescending(order => order.OrderID);
break;
case "Customer.ContactName":
data = data.OrderByDescending(order => order.Customer.ContactName);
break;
case "ShipAddress":
data = data.OrderByDescending(order => order.ShipAddress);
break;
case "OrderDate":
data = data.OrderByDescending(order => order.OrderDate);
break;
}
}
}
count = data.Count();
// ... and paging
if (command.PageSize > 0)
2010-10-08 05:50:01 +00:00
{
data = data.Skip((command.Page - 1) * command.PageSize);
2010-10-08 05:50:01 +00:00
}
data = data.Take(command.PageSize);
return data;*/
2010-10-08 05:50:01 +00:00
}
2011-02-04 00:48:09 +00:00
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _SaveAjaxEditing(string id)
{
return RedirectToAction("UnMapped");
}
2010-10-05 06:21:18 +00:00
public ActionResult Details(int seriesId)
2010-09-23 03:19:47 +00:00
{
var series = _seriesProvider.GetSeries(seriesId);
return View(series);
2010-09-23 03:19:47 +00:00
}
public ActionResult SyncEpisodesOnDisk(int seriesId)
{
//Syncs the episodes on disk for the specified series
var series = _seriesProvider.GetSeries(seriesId);
_mediaFileProvider.Scan(series);
2011-04-20 01:20:20 +00:00
return RedirectToAction("Details", new { seriesId });
}
public ActionResult UpdateInfo(int seriesId)
{
//Syncs the episodes on disk for the specified series
_jobProvider.BeginExecute(typeof(UpdateInfoJob), seriesId);
return RedirectToAction("Details", new { seriesId });
}
public ActionResult RenameAll()
{
_renameProvider.RenameAll();
return RedirectToAction("Index");
}
public ActionResult RenameSeries(int seriesId)
{
_renameProvider.RenameSeries(seriesId);
2011-04-20 01:20:20 +00:00
return RedirectToAction("Details", new { seriesId });
}
public ActionResult RenameSeason(int seasonId)
{
//Todo: Stay of Series Detail... AJAX?
_renameProvider.RenameSeason(seasonId);
return RedirectToAction("Index");
}
public ActionResult RenameEpisode(int episodeId)
{
//Todo: Stay of Series Detail... AJAX?
_renameProvider.RenameEpisode(episodeId);
return RedirectToAction("Index");
}
//Local Helpers
private string GetEpisodePath(EpisodeFile file)
{
if (file == null)
return String.Empty;
//Return the path relative to the Series' Folder
return file.Path.Replace(file.Series.Path, "").Trim(Path.DirectorySeparatorChar);
}
private List<SeriesModel> GetSeriesModels(List<Series> seriesInDb)
{
var series = new List<SeriesModel>();
seriesInDb.ForEach(s => series.Add(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,
SeasonsCount = s.Seasons.Where(x => x.SeasonNumber > 0).Count(),
SeasonFolder = s.SeasonFolder,
Status = s.Status
}));
return series;
}
2010-09-23 03:19:47 +00:00
}
2011-04-10 02:44:01 +00:00
}