diff --git a/MediaBrowser.Api/HttpHandlers/GenreHandler.cs b/MediaBrowser.Api/HttpHandlers/GenreHandler.cs new file mode 100644 index 000000000..2b0d1c57d --- /dev/null +++ b/MediaBrowser.Api/HttpHandlers/GenreHandler.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using MediaBrowser.Common.Net.Handlers; +using MediaBrowser.Controller; +using MediaBrowser.Model.DTO; +using MediaBrowser.Model.Entities; + +namespace MediaBrowser.Api.HttpHandlers +{ + /// + /// Gets a single genre + /// + public class GenreHandler : BaseJsonHandler> + { + protected override IBNItem GetObjectToSerialize() + { + Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder; + Guid userId = Guid.Parse(QueryString["userid"]); + User user = Kernel.Instance.Users.First(u => u.Id == userId); + + string name = QueryString["name"]; + + return GetGenre(parent, user, name); + } + + /// + /// Gets a Genre + /// + private IBNItem GetGenre(Folder parent, User user, string name) + { + int count = 0; + + // Get all the allowed recursive children + IEnumerable allItems = parent.GetParentalAllowedRecursiveChildren(user); + + foreach (var item in allItems) + { + if (item.Genres != null && item.Genres.Any(s => s.Equals(name, StringComparison.OrdinalIgnoreCase))) + { + count++; + } + } + + // Get the original entity so that we can also supply the PrimaryImagePath + return new IBNItem() + { + Item = Kernel.Instance.ItemController.GetGenre(name), + BaseItemCount = count + }; + } + } +} diff --git a/MediaBrowser.Api/HttpHandlers/GenresHandler.cs b/MediaBrowser.Api/HttpHandlers/GenresHandler.cs index c0f2e9eb3..1134eb568 100644 --- a/MediaBrowser.Api/HttpHandlers/GenresHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/GenresHandler.cs @@ -23,7 +23,7 @@ namespace MediaBrowser.Api.HttpHandlers /// Gets all genres from all recursive children of a folder /// The CategoryInfo class is used to keep track of the number of times each genres appears /// - public IEnumerable> GetAllGenres(Folder parent, User user) + private IEnumerable> GetAllGenres(Folder parent, User user) { Dictionary data = new Dictionary(); diff --git a/MediaBrowser.Api/HttpHandlers/PersonHandler.cs b/MediaBrowser.Api/HttpHandlers/PersonHandler.cs index 513840b81..75cccfac6 100644 --- a/MediaBrowser.Api/HttpHandlers/PersonHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/PersonHandler.cs @@ -1,14 +1,53 @@ -using MediaBrowser.Common.Net.Handlers; +using System; +using System.Collections.Generic; +using System.Linq; +using MediaBrowser.Common.Net.Handlers; using MediaBrowser.Controller; +using MediaBrowser.Model.DTO; using MediaBrowser.Model.Entities; namespace MediaBrowser.Api.HttpHandlers { - public class PersonHandler : BaseJsonHandler + /// + /// Gets a single Person + /// + public class PersonHandler : BaseJsonHandler> { - protected override Person GetObjectToSerialize() + protected override IBNItem GetObjectToSerialize() { - return Kernel.Instance.ItemController.GetPerson(QueryString["name"]); + Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder; + Guid userId = Guid.Parse(QueryString["userid"]); + User user = Kernel.Instance.Users.First(u => u.Id == userId); + + string name = QueryString["name"]; + + return GetPerson(parent, user, name); + } + + /// + /// Gets a Person + /// + private IBNItem GetPerson(Folder parent, User user, string name) + { + int count = 0; + + // Get all the allowed recursive children + IEnumerable allItems = parent.GetParentalAllowedRecursiveChildren(user); + + foreach (var item in allItems) + { + if (item.People != null && item.People.Any(s => s.Name.Equals(name, StringComparison.OrdinalIgnoreCase))) + { + count++; + } + } + + // Get the original entity so that we can also supply the PrimaryImagePath + return new IBNItem() + { + Item = Kernel.Instance.ItemController.GetPerson(name), + BaseItemCount = count + }; } } } diff --git a/MediaBrowser.Api/HttpHandlers/StudioHandler.cs b/MediaBrowser.Api/HttpHandlers/StudioHandler.cs new file mode 100644 index 000000000..40daadc70 --- /dev/null +++ b/MediaBrowser.Api/HttpHandlers/StudioHandler.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using MediaBrowser.Common.Net.Handlers; +using MediaBrowser.Controller; +using MediaBrowser.Model.DTO; +using MediaBrowser.Model.Entities; + +namespace MediaBrowser.Api.HttpHandlers +{ + /// + /// Gets a single studio + /// + public class StudioHandler : BaseJsonHandler> + { + protected override IBNItem GetObjectToSerialize() + { + Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder; + Guid userId = Guid.Parse(QueryString["userid"]); + User user = Kernel.Instance.Users.First(u => u.Id == userId); + + string name = QueryString["name"]; + + return GetStudio(parent, user, name); + } + + /// + /// Gets a Studio + /// + private IBNItem GetStudio(Folder parent, User user, string name) + { + int count = 0; + + // Get all the allowed recursive children + IEnumerable allItems = parent.GetParentalAllowedRecursiveChildren(user); + + foreach (var item in allItems) + { + if (item.Studios != null && item.Studios.Any(s => s.Equals(name, StringComparison.OrdinalIgnoreCase))) + { + count++; + } + } + + // Get the original entity so that we can also supply the PrimaryImagePath + return new IBNItem() + { + Item = Kernel.Instance.ItemController.GetStudio(name), + BaseItemCount = count + }; + } + } +} diff --git a/MediaBrowser.Api/HttpHandlers/YearHandler.cs b/MediaBrowser.Api/HttpHandlers/YearHandler.cs new file mode 100644 index 000000000..89cde9943 --- /dev/null +++ b/MediaBrowser.Api/HttpHandlers/YearHandler.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using MediaBrowser.Common.Net.Handlers; +using MediaBrowser.Controller; +using MediaBrowser.Model.DTO; +using MediaBrowser.Model.Entities; + +namespace MediaBrowser.Api.HttpHandlers +{ + /// + /// Gets a single year + /// + public class YearHandler : BaseJsonHandler> + { + protected override IBNItem GetObjectToSerialize() + { + Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder; + Guid userId = Guid.Parse(QueryString["userid"]); + User user = Kernel.Instance.Users.First(u => u.Id == userId); + + string year = QueryString["year"]; + + return GetYear(parent, user, int.Parse(year)); + } + + /// + /// Gets a Year + /// + private IBNItem GetYear(Folder parent, User user, int year) + { + int count = 0; + + // Get all the allowed recursive children + IEnumerable allItems = parent.GetParentalAllowedRecursiveChildren(user); + + foreach (var item in allItems) + { + if (item.ProductionYear.HasValue && item.ProductionYear.Value == year) + { + count++; + } + } + + // Get the original entity so that we can also supply the PrimaryImagePath + return new IBNItem() + { + Item = Kernel.Instance.ItemController.GetYear(year), + BaseItemCount = count + }; + } + } +} diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj index 4b6b6aa1a..d4333de95 100644 --- a/MediaBrowser.Api/MediaBrowser.Api.csproj +++ b/MediaBrowser.Api/MediaBrowser.Api.csproj @@ -49,6 +49,7 @@ + @@ -56,9 +57,11 @@ + + diff --git a/MediaBrowser.Api/Plugin.cs b/MediaBrowser.Api/Plugin.cs index 659763c5f..7f764d6fb 100644 --- a/MediaBrowser.Api/Plugin.cs +++ b/MediaBrowser.Api/Plugin.cs @@ -85,6 +85,22 @@ namespace MediaBrowser.Api { return new VideoHandler(); } + else if (localPath.EndsWith("/api/person", StringComparison.OrdinalIgnoreCase)) + { + return new PersonHandler(); + } + else if (localPath.EndsWith("/api/genre", StringComparison.OrdinalIgnoreCase)) + { + return new GenreHandler(); + } + else if (localPath.EndsWith("/api/year", StringComparison.OrdinalIgnoreCase)) + { + return new YearHandler(); + } + else if (localPath.EndsWith("/api/studio", StringComparison.OrdinalIgnoreCase)) + { + return new StudioHandler(); + } return null; } diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs index c30e67747..58ccb627c 100644 --- a/MediaBrowser.ApiInteraction/ApiClient.cs +++ b/MediaBrowser.ApiInteraction/ApiClient.cs @@ -294,6 +294,58 @@ namespace MediaBrowser.ApiInteraction } } + /// + /// Gets a studio + /// + public async Task> GetStudioAsync(Guid userId, string name) + { + string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name; + + using (Stream stream = await HttpClient.GetStreamAsync(url)) + { + return JsonSerializer.DeserializeFromStream>(stream); + } + } + + /// + /// Gets a genre + /// + public async Task> GetGenreAsync(Guid userId, string name) + { + string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name; + + using (Stream stream = await HttpClient.GetStreamAsync(url)) + { + return JsonSerializer.DeserializeFromStream>(stream); + } + } + + /// + /// Gets a person + /// + public async Task> GetPersonAsync(Guid userId, string name) + { + string url = ApiUrl + "/person?userId=" + userId.ToString() + "&name=" + name; + + using (Stream stream = await HttpClient.GetStreamAsync(url)) + { + return JsonSerializer.DeserializeFromStream>(stream); + } + } + + /// + /// Gets a year + /// + public async Task> GetYearAsync(Guid userId, int year) + { + string url = ApiUrl + "/year?userId=" + userId.ToString() + "&year=" + year; + + using (Stream stream = await HttpClient.GetStreamAsync(url)) + { + return JsonSerializer.DeserializeFromStream>(stream); + } + } + public void Dispose() { HttpClient.Dispose(); diff --git a/MediaBrowser.Model/DTO/DTOBaseItem.cs b/MediaBrowser.Model/DTO/DTOBaseItem.cs index 6f41ac578..76a9ea400 100644 --- a/MediaBrowser.Model/DTO/DTOBaseItem.cs +++ b/MediaBrowser.Model/DTO/DTOBaseItem.cs @@ -4,6 +4,10 @@ using MediaBrowser.Model.Entities; namespace MediaBrowser.Model.DTO { + /// + /// This is strictly used as a data transfer object from the api layer. + /// This holds information about a BaseItem in a format that is convenient for the client. + /// public class DTOBaseItem : IHasProviderIds { public string Name { get; set; }