Optionally include season images when fetching series from API

Closes #464
pull/2381/head
Mark McDowall 7 years ago
parent ee673cd152
commit 245bf6c7d6

@ -1,4 +1,4 @@
using System; using System;
using Nancy; using Nancy;
namespace NzbDrone.Api.Extensions namespace NzbDrone.Api.Extensions
@ -42,5 +42,17 @@ namespace NzbDrone.Api.Extensions
return request.Path.StartsWith("/MediaCover/", StringComparison.InvariantCultureIgnoreCase) || return request.Path.StartsWith("/MediaCover/", StringComparison.InvariantCultureIgnoreCase) ||
request.Path.StartsWith("/Content/Images/", StringComparison.InvariantCultureIgnoreCase); request.Path.StartsWith("/Content/Images/", StringComparison.InvariantCultureIgnoreCase);
} }
public static bool GetBooleanQueryParameter(this Request request, string parameter, bool defaultValue = false)
{
var parameterValue = request.Query[parameter];
if (parameterValue.HasValue)
{
return bool.Parse(parameterValue.Value);
}
return defaultValue;
}
} }
} }

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
namespace NzbDrone.Api.Series namespace NzbDrone.Api.Series
{ {
@ -8,18 +9,20 @@ namespace NzbDrone.Api.Series
public int SeasonNumber { get; set; } public int SeasonNumber { get; set; }
public bool Monitored { get; set; } public bool Monitored { get; set; }
public SeasonStatisticsResource Statistics { get; set; } public SeasonStatisticsResource Statistics { get; set; }
public List<MediaCover> Images { get; set; }
} }
public static class SeasonResourceMapper public static class SeasonResourceMapper
{ {
public static SeasonResource ToResource(this Season model) public static SeasonResource ToResource(this Season model, bool includeImages = false)
{ {
if (model == null) return null; if (model == null) return null;
return new SeasonResource return new SeasonResource
{ {
SeasonNumber = model.SeasonNumber, SeasonNumber = model.SeasonNumber,
Monitored = model.Monitored Monitored = model.Monitored,
Images = includeImages ? model.Images : null
}; };
} }
@ -30,13 +33,14 @@ namespace NzbDrone.Api.Series
return new Season return new Season
{ {
SeasonNumber = resource.SeasonNumber, SeasonNumber = resource.SeasonNumber,
Monitored = resource.Monitored Monitored = resource.Monitored,
Images = resource.Images
}; };
} }
public static List<SeasonResource> ToResource(this IEnumerable<Season> models) public static List<SeasonResource> ToResource(this IEnumerable<Season> models, bool includeImages = false)
{ {
return models.Select(ToResource).ToList(); return models.Select(s => ToResource(s, includeImages)).ToList();
} }
public static List<Season> ToModel(this IEnumerable<SeasonResource> resources) public static List<Season> ToModel(this IEnumerable<SeasonResource> resources)

@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Nancy; using Nancy;
using NzbDrone.Api.Extensions; using NzbDrone.Api.Extensions;
@ -24,7 +24,7 @@ namespace NzbDrone.Api.Series
var series = resources.Select(seriesResource => seriesResource.ToModel(_seriesService.GetSeries(seriesResource.Id))).ToList(); var series = resources.Select(seriesResource => seriesResource.ToModel(_seriesService.GetSeries(seriesResource.Id))).ToList();
return _seriesService.UpdateSeries(series) return _seriesService.UpdateSeries(series)
.ToResource() .ToResource(false)
.AsResponse(HttpStatusCode.Accepted); .AsResponse(HttpStatusCode.Accepted);
} }
} }

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using FluentValidation; using FluentValidation;
using NzbDrone.Api.Extensions;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Datastore.Events; using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.MediaCover; using NzbDrone.Core.MediaCover;
@ -86,26 +87,17 @@ namespace NzbDrone.Api.Series
private SeriesResource GetSeries(int id) private SeriesResource GetSeries(int id)
{ {
var series = _seriesService.GetSeries(id); var includeSeasonImages = Request.GetBooleanQueryParameter("includeSeasonImages");
return MapToResource(series);
}
private SeriesResource MapToResource(Core.Tv.Series series)
{
if (series == null) return null;
var resource = series.ToResource(); var series = _seriesService.GetSeries(id);
MapCoversToLocal(resource); return MapToResource(series, includeSeasonImages);
FetchAndLinkSeriesStatistics(resource);
PopulateAlternateTitles(resource);
return resource;
} }
private List<SeriesResource> AllSeries() private List<SeriesResource> AllSeries()
{ {
var includeSeasonImages = Request.GetBooleanQueryParameter("includeSeasonImages");
var seriesStats = _seriesStatisticsService.SeriesStatistics(); var seriesStats = _seriesStatisticsService.SeriesStatistics();
var seriesResources = _seriesService.GetAllSeries().ToResource(); var seriesResources = _seriesService.GetAllSeries().Select(s => s.ToResource(includeSeasonImages)).ToList();
MapCoversToLocal(seriesResources.ToArray()); MapCoversToLocal(seriesResources.ToArray());
LinkSeriesStatistics(seriesResources, seriesStats); LinkSeriesStatistics(seriesResources, seriesStats);
@ -143,6 +135,18 @@ namespace NzbDrone.Api.Series
_seriesService.DeleteSeries(id, deleteFiles); _seriesService.DeleteSeries(id, deleteFiles);
} }
private SeriesResource MapToResource(Core.Tv.Series series, bool includeSeasonImages)
{
if (series == null) return null;
var resource = series.ToResource(includeSeasonImages);
MapCoversToLocal(resource);
FetchAndLinkSeriesStatistics(resource);
PopulateAlternateTitles(resource);
return resource;
}
private void MapCoversToLocal(params SeriesResource[] series) private void MapCoversToLocal(params SeriesResource[] series)
{ {
foreach (var seriesResource in series) foreach (var seriesResource in series)

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NzbDrone.Api.REST; using NzbDrone.Api.REST;
@ -97,7 +97,7 @@ namespace NzbDrone.Api.Series
public static class SeriesResourceMapper public static class SeriesResourceMapper
{ {
public static SeriesResource ToResource(this Core.Tv.Series model) public static SeriesResource ToResource(this Core.Tv.Series model, bool includeSeasonImages = false)
{ {
if (model == null) return null; if (model == null) return null;
@ -121,7 +121,7 @@ namespace NzbDrone.Api.Series
AirTime = model.AirTime, AirTime = model.AirTime,
Images = model.Images, Images = model.Images,
Seasons = model.Seasons.ToResource(), Seasons = model.Seasons.ToResource(includeSeasonImages),
Year = model.Year, Year = model.Year,
Path = model.Path, Path = model.Path,
@ -214,9 +214,9 @@ namespace NzbDrone.Api.Series
return series; return series;
} }
public static List<SeriesResource> ToResource(this IEnumerable<Core.Tv.Series> series) public static List<SeriesResource> ToResource(this IEnumerable<Core.Tv.Series> series, bool includeSeasonImages)
{ {
return series.Select(ToResource).ToList(); return series.Select(s => ToResource(s, includeSeasonImages)).ToList();
} }
} }
} }

Loading…
Cancel
Save