2017-02-11 06:46:39 +00:00
|
|
|
using System.Collections.Generic;
|
2021-11-06 20:44:26 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-12-04 16:19:55 +00:00
|
|
|
using NzbDrone.Core.CustomFormats;
|
2017-02-11 06:46:39 +00:00
|
|
|
using NzbDrone.Core.Datastore.Events;
|
2018-10-20 20:27:51 +00:00
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
2017-02-11 06:46:39 +00:00
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
using NzbDrone.SignalR;
|
|
|
|
using Sonarr.Api.V3.EpisodeFiles;
|
|
|
|
using Sonarr.Api.V3.Series;
|
2021-11-06 20:44:26 +00:00
|
|
|
using Sonarr.Http.REST;
|
2017-02-11 06:46:39 +00:00
|
|
|
|
|
|
|
namespace Sonarr.Api.V3.Episodes
|
|
|
|
{
|
2021-11-06 20:44:26 +00:00
|
|
|
public abstract class EpisodeControllerWithSignalR : RestControllerWithSignalR<EpisodeResource, Episode>,
|
|
|
|
IHandle<EpisodeGrabbedEvent>,
|
|
|
|
IHandle<EpisodeImportedEvent>,
|
|
|
|
IHandle<EpisodeFileDeletedEvent>
|
2017-02-11 06:46:39 +00:00
|
|
|
{
|
|
|
|
protected readonly IEpisodeService _episodeService;
|
|
|
|
protected readonly ISeriesService _seriesService;
|
2015-07-12 16:44:33 +00:00
|
|
|
protected readonly IUpgradableSpecification _upgradableSpecification;
|
2022-12-04 16:19:55 +00:00
|
|
|
protected readonly ICustomFormatCalculationService _formatCalculator;
|
2017-02-11 06:46:39 +00:00
|
|
|
|
2021-11-06 20:44:26 +00:00
|
|
|
protected EpisodeControllerWithSignalR(IEpisodeService episodeService,
|
2017-02-11 06:46:39 +00:00
|
|
|
ISeriesService seriesService,
|
2015-07-12 16:44:33 +00:00
|
|
|
IUpgradableSpecification upgradableSpecification,
|
2022-12-04 16:19:55 +00:00
|
|
|
ICustomFormatCalculationService formatCalculator,
|
2017-02-11 06:46:39 +00:00
|
|
|
IBroadcastSignalRMessage signalRBroadcaster)
|
|
|
|
: base(signalRBroadcaster)
|
|
|
|
{
|
|
|
|
_episodeService = episodeService;
|
|
|
|
_seriesService = seriesService;
|
2015-07-12 16:44:33 +00:00
|
|
|
_upgradableSpecification = upgradableSpecification;
|
2022-12-04 16:19:55 +00:00
|
|
|
_formatCalculator = formatCalculator;
|
2017-02-11 06:46:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 20:44:26 +00:00
|
|
|
protected EpisodeControllerWithSignalR(IEpisodeService episodeService,
|
2017-02-11 06:46:39 +00:00
|
|
|
ISeriesService seriesService,
|
2015-07-12 16:44:33 +00:00
|
|
|
IUpgradableSpecification upgradableSpecification,
|
2022-12-04 16:19:55 +00:00
|
|
|
ICustomFormatCalculationService formatCalculator,
|
2017-02-11 06:46:39 +00:00
|
|
|
IBroadcastSignalRMessage signalRBroadcaster,
|
|
|
|
string resource)
|
2021-11-06 20:44:26 +00:00
|
|
|
: base(signalRBroadcaster)
|
2017-02-11 06:46:39 +00:00
|
|
|
{
|
|
|
|
_episodeService = episodeService;
|
|
|
|
_seriesService = seriesService;
|
2015-07-12 16:44:33 +00:00
|
|
|
_upgradableSpecification = upgradableSpecification;
|
2022-12-04 16:19:55 +00:00
|
|
|
_formatCalculator = formatCalculator;
|
2017-02-11 06:46:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 20:44:26 +00:00
|
|
|
protected override EpisodeResource GetResourceById(int id)
|
2017-02-11 06:46:39 +00:00
|
|
|
{
|
|
|
|
var episode = _episodeService.GetEpisode(id);
|
|
|
|
var resource = MapToResource(episode, true, true, true);
|
|
|
|
return resource;
|
|
|
|
}
|
2021-08-03 04:43:28 +00:00
|
|
|
|
2017-02-11 06:46:39 +00:00
|
|
|
protected EpisodeResource MapToResource(Episode episode, bool includeSeries, bool includeEpisodeFile, bool includeImages)
|
|
|
|
{
|
|
|
|
var resource = episode.ToResource();
|
|
|
|
|
|
|
|
if (includeSeries || includeEpisodeFile || includeImages)
|
|
|
|
{
|
|
|
|
var series = episode.Series ?? _seriesService.GetSeries(episode.SeriesId);
|
|
|
|
|
|
|
|
if (includeSeries)
|
|
|
|
{
|
|
|
|
resource.Series = series.ToResource();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeEpisodeFile && episode.EpisodeFileId != 0)
|
|
|
|
{
|
2022-12-04 16:19:55 +00:00
|
|
|
resource.EpisodeFile = episode.EpisodeFile.Value.ToResource(series, _upgradableSpecification, _formatCalculator);
|
2017-02-11 06:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (includeImages)
|
|
|
|
{
|
|
|
|
resource.Images = episode.Images;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<EpisodeResource> MapToResource(List<Episode> episodes, bool includeSeries, bool includeEpisodeFile, bool includeImages)
|
|
|
|
{
|
|
|
|
var result = episodes.ToResource();
|
|
|
|
|
|
|
|
if (includeSeries || includeEpisodeFile || includeImages)
|
|
|
|
{
|
|
|
|
var seriesDict = new Dictionary<int, NzbDrone.Core.Tv.Series>();
|
|
|
|
for (var i = 0; i < episodes.Count; i++)
|
|
|
|
{
|
|
|
|
var episode = episodes[i];
|
|
|
|
var resource = result[i];
|
|
|
|
|
|
|
|
var series = episode.Series ?? seriesDict.GetValueOrDefault(episodes[i].SeriesId) ?? _seriesService.GetSeries(episodes[i].SeriesId);
|
|
|
|
seriesDict[series.Id] = series;
|
|
|
|
|
|
|
|
if (includeSeries)
|
|
|
|
{
|
|
|
|
resource.Series = series.ToResource();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeEpisodeFile && episode.EpisodeFileId != 0)
|
|
|
|
{
|
2022-12-04 16:19:55 +00:00
|
|
|
resource.EpisodeFile = episode.EpisodeFile.Value.ToResource(series, _upgradableSpecification, _formatCalculator);
|
2017-02-11 06:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (includeImages)
|
|
|
|
{
|
|
|
|
resource.Images = episode.Images;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-11-06 20:44:26 +00:00
|
|
|
[NonAction]
|
2017-02-11 06:46:39 +00:00
|
|
|
public void Handle(EpisodeGrabbedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var episode in message.Episode.Episodes)
|
|
|
|
{
|
|
|
|
var resource = episode.ToResource();
|
|
|
|
resource.Grabbed = true;
|
|
|
|
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 20:44:26 +00:00
|
|
|
[NonAction]
|
2017-02-11 06:46:39 +00:00
|
|
|
public void Handle(EpisodeImportedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var episode in message.EpisodeInfo.Episodes)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, episode.Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 20:44:26 +00:00
|
|
|
[NonAction]
|
2017-02-11 06:46:39 +00:00
|
|
|
public void Handle(EpisodeFileDeletedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var episode in message.EpisodeFile.Episodes.Value)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, episode.Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|