Skip to content

Commit

Permalink
Merge pull request #435 from angyanmark/feature/extend-media-type
Browse files Browse the repository at this point in the history
Extend MediaType
  • Loading branch information
cvium authored Sep 22, 2023
2 parents f1ee9df + 9fb6275 commit 17674ca
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
11 changes: 10 additions & 1 deletion TMDbLib/Objects/General/MediaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public enum MediaType
Person = 3,

[EnumValue("episode")]
Episode = 4
Episode = 4,

[EnumValue("tv_episode")]
TvEpisode = 5,

[EnumValue("season")]
Season = 6,

[EnumValue("tv_season")]
TvSeason = 7
}
}
11 changes: 7 additions & 4 deletions TMDbLib/Objects/Search/SearchTvSeason.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using System;
using Newtonsoft.Json;
using TMDbLib.Objects.General;

namespace TMDbLib.Objects.Search
{
public class SearchTvSeason
public class SearchTvSeason : SearchBase
{
public SearchTvSeason()
{
MediaType = MediaType.Season;
}

[JsonProperty("air_date")]
public DateTime? AirDate { get; set; }

[JsonProperty("episode_count")]
public int EpisodeCount { get; set; }

[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

Expand Down
26 changes: 10 additions & 16 deletions TMDbLib/Utilities/Converters/SearchBaseConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,17 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
// Determine the type based on the media_type
MediaType mediaType = jObject["media_type"].ToObject<MediaType>();

switch (mediaType)
result = mediaType switch
{
case MediaType.Movie:
result = new SearchMovie();
break;
case MediaType.Tv:
result = new SearchTv();
break;
case MediaType.Person:
result = new SearchPerson();
break;
case MediaType.Episode:
result = new SearchTvEpisode();
break;
default:
throw new ArgumentOutOfRangeException();
}
MediaType.Movie => new SearchMovie(),
MediaType.Tv => new SearchTv(),
MediaType.Person => new SearchPerson(),
MediaType.Episode => new SearchTvEpisode(),
MediaType.TvEpisode => new SearchTvEpisode(),
MediaType.Season => new SearchTvSeason(),
MediaType.TvSeason => new SearchTvSeason(),
_ => throw new ArgumentOutOfRangeException(),
};
}

// Populate the result
Expand Down
21 changes: 7 additions & 14 deletions TMDbLib/Utilities/Converters/TaggedImageConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,14 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
serializer.Populate(jsonReader, result);

JToken mediaJson = jObject["media"];
switch (result.MediaType)
result.Media = result.MediaType switch
{
case MediaType.Movie:
result.Media = mediaJson.ToObject<SearchMovie>();
break;
case MediaType.Tv:
result.Media = mediaJson.ToObject<SearchTv>();
break;
case MediaType.Episode:
result.Media = mediaJson.ToObject<SearchTvEpisode>();
break;
default:
throw new ArgumentOutOfRangeException();
}

MediaType.Movie => mediaJson.ToObject<SearchMovie>(),
MediaType.Tv => mediaJson.ToObject<SearchTv>(),
MediaType.Episode => mediaJson.ToObject<SearchTvEpisode>(),
MediaType.Season => mediaJson.ToObject<SearchTvSeason>(),
_ => throw new ArgumentOutOfRangeException(),
};
return result;
}

Expand Down
1 change: 1 addition & 0 deletions TMDbLibTests/Helpers/IdHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static class IdHelper
public const int TomHanks = 31;
public const string ImdbBruceWillis = "nm0000246";
public const int JoshACagan = 129305;
public const int AnnaTorv = 30084;

// Collections
public const int JamesBondCollection = 645;
Expand Down
5 changes: 4 additions & 1 deletion TMDbLibTests/UtilityTests/TaggedImageConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ await Verify(new
/// Tests the TaggedImageConverter
/// </summary>
[Theory]
[InlineData(IdHelper.HughLaurie)] // Has Movie media
[InlineData(IdHelper.HughLaurie)] // Has Movie media
[InlineData(IdHelper.TomHanks)] // Has Episode media
[InlineData(IdHelper.AnnaTorv)] // Has Tv, Season media
public async Task TestJsonTaggedImageConverter(int personId)
{
// Get images
Expand All @@ -77,6 +78,8 @@ public async Task TestJsonTaggedImageConverter(int personId)
Assert.IsType<SearchTv>(item.Media);
else if (item.MediaType == MediaType.Episode)
Assert.IsType<SearchTvEpisode>(item.Media);
else if (item.MediaType == MediaType.Season)
Assert.IsType<SearchTvSeason>(item.Media);
else
Assert.False(true, $"Unexpected type {item.GetType().Name}");
});
Expand Down

0 comments on commit 17674ca

Please sign in to comment.