Skip to content

Commit

Permalink
feat: set default/preferred for TMDB image DTOs in APIv3
Browse files Browse the repository at this point in the history
Set the default/preferred flag for the TMDB image DTOs in APIv3.
  • Loading branch information
revam committed Dec 5, 2024
1 parent f924b4e commit dec67a1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
17 changes: 12 additions & 5 deletions Shoko.Server/API/v3/Controllers/TmdbController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public ActionResult<IReadOnlyList<Overview>> GetOverviewsForTmdbMovieByMovieID(
[HttpGet("Movie/{movieID}/Images")]
public ActionResult<Images> GetImagesForTmdbMovieByMovieID(
[FromRoute] int movieID,
[FromQuery] bool includeDisabled = false,
[FromQuery, ModelBinder(typeof(CommaDelimitedModelBinder))] HashSet<TitleLanguage>? language = null
)
{
Expand All @@ -233,7 +234,7 @@ public ActionResult<Images> GetImagesForTmdbMovieByMovieID(
return NotFound(MovieNotFound);

return movie.GetImages()
.ToDto(language);
.ToDto(language, includeDisabled: includeDisabled, preferredPoster: movie.DefaultPoster, preferredBackdrop: movie.DefaultBackdrop);
}

[HttpGet("Movie/{movieID}/Cast")]
Expand Down Expand Up @@ -757,6 +758,7 @@ public ActionResult<IReadOnlyList<Overview>> GetOverviewsForMovieCollectionByCol
[HttpGet("Movie/Collection/{collectionID}/Images")]
public ActionResult<Images> GetImagesForMovieCollectionByCollectionID(
[FromRoute] int collectionID,
[FromQuery] bool includeDisabled = false,
[FromQuery, ModelBinder(typeof(CommaDelimitedModelBinder))] HashSet<TitleLanguage>? language = null
)
{
Expand All @@ -765,7 +767,7 @@ public ActionResult<Images> GetImagesForMovieCollectionByCollectionID(
return NotFound(MovieCollectionNotFound);

return collection.GetImages()
.ToDto(language);
.ToDto(language, includeDisabled: includeDisabled);
}

#endregion
Expand Down Expand Up @@ -969,6 +971,7 @@ public ActionResult<IReadOnlyList<Overview>> GetOverviewsForTmdbShowByShowID(
[HttpGet("Show/{showID}/Images")]
public ActionResult<Images> GetImagesForTmdbShowByShowID(
[FromRoute] int showID,
[FromQuery] bool includeDisabled = false,
[FromQuery, ModelBinder(typeof(CommaDelimitedModelBinder))] HashSet<TitleLanguage>? language = null
)
{
Expand All @@ -977,7 +980,7 @@ public ActionResult<Images> GetImagesForTmdbShowByShowID(
return NotFound(ShowNotFound);

return show.GetImages()
.ToDto(language);
.ToDto(language, includeDisabled: includeDisabled, preferredPoster: show.DefaultPoster, preferredBackdrop: show.DefaultBackdrop);
}

[HttpGet("Show/{showID}/Ordering")]
Expand Down Expand Up @@ -1724,6 +1727,7 @@ public ActionResult<IReadOnlyList<Overview>> GetOverviewsForTmdbSeasonBySeasonID
[HttpGet("Season/{seasonID}/Images")]
public ActionResult<Images> GetImagesForTmdbSeasonBySeasonID(
[FromRoute, RegularExpression(SeasonIdRegex)] string seasonID,
[FromQuery] bool includeDisabled = false,
[FromQuery, ModelBinder(typeof(CommaDelimitedModelBinder))] HashSet<TitleLanguage>? language = null
)
{
Expand All @@ -1741,7 +1745,8 @@ public ActionResult<Images> GetImagesForTmdbSeasonBySeasonID(
if (season is null)
return NotFound(SeasonNotFound);

return season.GetImages().ToDto(language);
return season.GetImages()
.ToDto(language, includeDisabled: includeDisabled, preferredPoster: season.DefaultPoster);
}

[HttpGet("Season/{seasonID}/Cast")]
Expand Down Expand Up @@ -2122,14 +2127,16 @@ public ActionResult<IReadOnlyList<Overview>> GetOverviewsForTmdbEpisodeByEpisode
[HttpGet("Episode/{episodeID}/Images")]
public ActionResult<Images> GetImagesForTmdbEpisodeByEpisodeID(
[FromRoute] int episodeID,
[FromQuery] bool includeDisabled = false,
[FromQuery, ModelBinder(typeof(CommaDelimitedModelBinder))] HashSet<TitleLanguage>? language = null
)
{
var episode = RepoFactory.TMDB_Episode.GetByTmdbEpisodeID(episodeID);
if (episode is null)
return NotFound(EpisodeNotFound);

return episode.GetImages().ToDto(language, includeThumbnails: true);
return episode.GetImages()
.ToDto(language, includeDisabled: includeDisabled, includeThumbnails: true);
}

[HttpGet("Episode/{episodeID}/Cast")]
Expand Down
26 changes: 19 additions & 7 deletions Shoko.Server/API/v3/Helpers/APIv3_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ public static IEnumerable<IImageMetadata> InLanguage(this IEnumerable<IImageMeta
? imageList.Where(title => language.Contains(title.Language))
: imageList;

public static Images ToDto(this IEnumerable<IImageMetadata> imageList, IReadOnlySet<TitleLanguage>? language = null, bool includeDisabled = false, bool includeThumbnails = false, bool preferredImages = false, bool randomizeImages = false)
public static Images ToDto(
this IEnumerable<IImageMetadata> imageList,
IReadOnlySet<TitleLanguage>? language = null,
IImageMetadata? preferredPoster = null,
IImageMetadata? preferredBackdrop = null,
bool includeDisabled = false,
bool includeThumbnails = false,
bool preferredImages = false,
bool randomizeImages = false)
{
var images = new Images();
if (includeThumbnails)
Expand All @@ -51,23 +59,27 @@ public static Images ToDto(this IEnumerable<IImageMetadata> imageList, IReadOnly
if (language != null && !language.Contains(image.Language))
continue;

var dto = new Image(image);
bool? preferredOverride = null;
switch (image.ImageType)
{
case ImageEntityType.Poster:
images.Posters.Add(dto);
if (image.IsEnabled && preferredPoster is not null && preferredPoster.Equals(image))
preferredOverride = true;
images.Posters.Add(new(image, preferredOverride));
break;
case ImageEntityType.Banner:
images.Banners.Add(dto);
images.Banners.Add(new(image));
break;
case ImageEntityType.Backdrop:
images.Backdrops.Add(dto);
if (image.IsEnabled && preferredBackdrop is not null && preferredBackdrop.Equals(image))
preferredOverride = true;
images.Backdrops.Add(new(image, preferredOverride));
break;
case ImageEntityType.Logo:
images.Logos.Add(dto);
images.Logos.Add(new(image));
break;
case ImageEntityType.Thumbnail when includeThumbnails:
images.Thumbnails!.Add(dto);
images.Thumbnails!.Add(new(image));
break;
default:
break;
Expand Down
4 changes: 2 additions & 2 deletions Shoko.Server/API/v3/Models/Common/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public class Image
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public ImageSeriesInfo? Series { get; set; } = null;

public Image(IImageMetadata imageMetadata)
public Image(IImageMetadata imageMetadata, bool? preferredOverride = null)
{
ID = imageMetadata.ID;
Type = imageMetadata.ImageType.ToV3Dto();
Source = imageMetadata.Source.ToV3Dto();

Preferred = imageMetadata.IsPreferred;
Preferred = preferredOverride ?? imageMetadata.IsPreferred;
Disabled = !imageMetadata.IsEnabled;
LanguageCode = imageMetadata.LanguageCode;

Expand Down
2 changes: 1 addition & 1 deletion Shoko.Server/API/v3/Models/TMDB/Movie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public Movie(TMDB_Movie movie, IncludeDetails? includeDetails = null, IReadOnlyS
.ToList();
if (include.HasFlag(IncludeDetails.Images))
Images = movie.GetImages()
.ToDto(language);
.ToDto(language, preferredPoster: movie.DefaultPoster, preferredBackdrop: movie.DefaultBackdrop);
if (include.HasFlag(IncludeDetails.Cast))
Cast = movie.Cast
.Select(cast => new Role(cast))
Expand Down
2 changes: 1 addition & 1 deletion Shoko.Server/API/v3/Models/TMDB/Season.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public Season(TMDB_Season season, IncludeDetails? includeDetails = null, IReadOn
.ToDto(season.EnglishOverview, preferredOverview, language);
if (include.HasFlag(IncludeDetails.Images))
Images = season.GetImages()
.ToDto(language);
.ToDto(language, preferredPoster: season.DefaultPoster);
if (include.HasFlag(IncludeDetails.Cast))
Cast = season.Cast
.Select(cast => new Role(cast))
Expand Down
2 changes: 1 addition & 1 deletion Shoko.Server/API/v3/Models/TMDB/Show.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public Show(TMDB_Show show, TMDB_AlternateOrdering? alternateOrdering, IncludeDe
.ToList();
if (include.HasFlag(IncludeDetails.Images))
Images = show.GetImages()
.ToDto(language);
.ToDto(language, preferredPoster: show.DefaultPoster, preferredBackdrop: show.DefaultBackdrop);
if (include.HasFlag(IncludeDetails.Cast))
Cast = (alternateOrdering is null ? show.Cast : alternateOrdering.Cast)
.Select(cast => new Role(cast))
Expand Down
2 changes: 2 additions & 0 deletions Shoko.Server/Models/TMDB/TMDB_Season.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ public IReadOnlyList<TMDB_Overview> GetAllOverviews(bool force = false) => force
? _allOverviews = RepoFactory.TMDB_Overview.GetByParentTypeAndID(ForeignEntityType.Season, TmdbSeasonID)
: _allOverviews ??= RepoFactory.TMDB_Overview.GetByParentTypeAndID(ForeignEntityType.Season, TmdbSeasonID);

public TMDB_Image? DefaultPoster => RepoFactory.TMDB_Image.GetByRemoteFileNameAndType(PosterPath, ImageEntityType.Poster);

/// <summary>
/// Get all images for the season, or all images for the given
/// <paramref name="entityType"/> provided for the season.
Expand Down

0 comments on commit dec67a1

Please sign in to comment.