2013-09-30 15:09:47 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-08-27 03:20:03 +00:00
|
|
|
|
using NzbDrone.Api.Mapping;
|
2013-05-31 00:12:20 +00:00
|
|
|
|
using NzbDrone.Api.REST;
|
2013-09-30 15:09:47 +00:00
|
|
|
|
using NzbDrone.Core.Datastore.Events;
|
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-03-02 19:13:23 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Episodes
|
|
|
|
|
{
|
2013-09-30 15:09:47 +00:00
|
|
|
|
public class EpisodeModule : NzbDroneRestModuleWithSignalR<EpisodeResource, Episode>,
|
|
|
|
|
IHandle<EpisodeGrabbedEvent>,
|
|
|
|
|
IHandle<EpisodeDownloadedEvent>
|
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
|
{
|
2013-04-01 06:22:16 +00:00
|
|
|
|
private readonly IEpisodeService _episodeService;
|
2013-03-02 19:13:23 +00:00
|
|
|
|
|
2013-09-30 15:09:47 +00:00
|
|
|
|
public EpisodeModule(ICommandExecutor commandExecutor, IEpisodeService episodeService)
|
|
|
|
|
: base(commandExecutor, "episodes")
|
2013-03-02 19:13:23 +00:00
|
|
|
|
{
|
|
|
|
|
_episodeService = episodeService;
|
2013-05-31 00:12:20 +00:00
|
|
|
|
|
|
|
|
|
GetResourceAll = GetEpisodes;
|
2013-07-09 15:41:42 +00:00
|
|
|
|
UpdateResource = SetMonitored;
|
2013-08-27 03:20:03 +00:00
|
|
|
|
GetResourceById = GetEpisode;
|
2013-03-02 19:13:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
private List<EpisodeResource> GetEpisodes()
|
2013-03-02 19:13:23 +00:00
|
|
|
|
{
|
2013-05-31 00:12:20 +00:00
|
|
|
|
var seriesId = (int?)Request.Query.SeriesId;
|
|
|
|
|
|
|
|
|
|
if (seriesId == null)
|
|
|
|
|
{
|
|
|
|
|
throw new BadRequestException("seriesId is missing");
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-22 00:56:25 +00:00
|
|
|
|
return ToListResource(() => _episodeService.GetEpisodeBySeries(seriesId.Value));
|
2013-03-02 19:13:23 +00:00
|
|
|
|
}
|
2013-07-09 15:41:42 +00:00
|
|
|
|
|
2013-08-26 05:14:55 +00:00
|
|
|
|
private void SetMonitored(EpisodeResource episodeResource)
|
2013-07-09 15:41:42 +00:00
|
|
|
|
{
|
|
|
|
|
_episodeService.SetEpisodeMonitored(episodeResource.Id, episodeResource.Monitored);
|
|
|
|
|
}
|
2013-08-27 03:20:03 +00:00
|
|
|
|
|
|
|
|
|
private EpisodeResource GetEpisode(int id)
|
|
|
|
|
{
|
|
|
|
|
return _episodeService.GetEpisode(id).InjectTo<EpisodeResource>();
|
|
|
|
|
}
|
2013-09-30 15:09:47 +00:00
|
|
|
|
|
|
|
|
|
public void Handle(EpisodeGrabbedEvent message)
|
|
|
|
|
{
|
|
|
|
|
foreach (var episode in message.Episode.Episodes)
|
|
|
|
|
{
|
2013-10-02 05:20:30 +00:00
|
|
|
|
var resource = episode.InjectTo<EpisodeResource>();
|
|
|
|
|
resource.Downloading = true;
|
|
|
|
|
|
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, resource);
|
2013-09-30 15:09:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(EpisodeDownloadedEvent message)
|
|
|
|
|
{
|
|
|
|
|
foreach (var episode in message.Episode.Episodes)
|
|
|
|
|
{
|
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, episode.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-02 19:13:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|