Radarr/src/NzbDrone.Api/Episodes/EpisodeModule.cs

72 lines
2.3 KiB
C#
Raw Normal View History

2013-09-30 15:09:47 +00:00
using System;
using System.Collections.Generic;
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;
UpdateResource = SetMonitored;
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");
}
return ToListResource(() => _episodeService.GetEpisodeBySeries(seriesId.Value));
2013-03-02 19:13:23 +00:00
}
private void SetMonitored(EpisodeResource episodeResource)
{
_episodeService.SetEpisodeMonitored(episodeResource.Id, episodeResource.Monitored);
}
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)
{
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
}
}