2014-11-09 13:24:57 -05:00
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.Net ;
using MediaBrowser.Model.Querying ;
using System ;
2016-10-08 01:57:38 -04:00
using System.Collections.Generic ;
2014-11-09 13:24:57 -05:00
using System.Linq ;
using System.Threading.Tasks ;
2016-10-25 15:02:04 -04:00
using MediaBrowser.Model.Services ;
2014-11-09 13:24:57 -05:00
namespace MediaBrowser.Api
{
[Route("/Items/Filters", "GET", Summary = "Gets branding configuration")]
public class GetQueryFilters : IReturn < QueryFilters >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string UserId { get ; set ; }
[ApiMember(Name = "ParentId", Description = "Specify this to localize the search to a specific item or folder. Omit to use the root", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ParentId { get ; set ; }
[ApiMember(Name = "IncludeItemTypes", Description = "Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimeted.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string IncludeItemTypes { get ; set ; }
[ApiMember(Name = "MediaTypes", Description = "Optional filter by MediaType. Allows multiple, comma delimited.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string MediaTypes { get ; set ; }
public string [ ] GetMediaTypes ( )
{
return ( MediaTypes ? ? string . Empty ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
}
public string [ ] GetIncludeItemTypes ( )
{
return ( IncludeItemTypes ? ? string . Empty ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
}
}
[Authenticated]
public class FilterService : BaseApiService
{
private readonly ILibraryManager _libraryManager ;
private readonly IUserManager _userManager ;
public FilterService ( ILibraryManager libraryManager , IUserManager userManager )
{
_libraryManager = libraryManager ;
_userManager = userManager ;
}
2017-05-26 02:48:54 -04:00
public object Get ( GetQueryFilters request )
2014-11-09 13:24:57 -05:00
{
var parentItem = string . IsNullOrEmpty ( request . ParentId ) ? null : _libraryManager . GetItemById ( request . ParentId ) ;
var user = ! string . IsNullOrWhiteSpace ( request . UserId ) ? _userManager . GetUserById ( request . UserId ) : null ;
var item = string . IsNullOrEmpty ( request . ParentId ) ?
user = = null ? _libraryManager . RootFolder : user . RootFolder :
parentItem ;
2017-08-01 12:45:57 -04:00
var result = ( ( Folder ) item ) . GetItemList ( GetItemsQuery ( request , user ) ) ;
2014-11-09 13:24:57 -05:00
2017-08-01 12:45:57 -04:00
return ToOptimizedResult ( GetFilters ( result . ToArray ( ) ) ) ;
2014-11-09 13:24:57 -05:00
}
private QueryFilters GetFilters ( BaseItem [ ] items )
{
var result = new QueryFilters ( ) ;
result . Years = items . Select ( i = > i . ProductionYear ? ? - 1 )
. Where ( i = > i > 0 )
. Distinct ( )
. OrderBy ( i = > i )
. ToArray ( ) ;
result . Genres = items . SelectMany ( i = > i . Genres )
2015-04-09 17:11:57 -04:00
. DistinctNames ( )
2014-11-09 13:24:57 -05:00
. OrderBy ( i = > i )
. ToArray ( ) ;
2016-06-02 13:43:29 -04:00
result . Tags = items
2014-11-09 13:24:57 -05:00
. SelectMany ( i = > i . Tags )
. Distinct ( StringComparer . OrdinalIgnoreCase )
. OrderBy ( i = > i )
. ToArray ( ) ;
result . OfficialRatings = items
. Select ( i = > i . OfficialRating )
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
. Distinct ( StringComparer . OrdinalIgnoreCase )
. OrderBy ( i = > i )
. ToArray ( ) ;
return result ;
}
private InternalItemsQuery GetItemsQuery ( GetQueryFilters request , User user )
{
var query = new InternalItemsQuery
{
User = user ,
MediaTypes = request . GetMediaTypes ( ) ,
IncludeItemTypes = request . GetIncludeItemTypes ( ) ,
2016-05-18 01:34:10 -04:00
Recursive = true ,
2016-10-08 01:57:38 -04:00
EnableTotalRecordCount = false ,
2016-10-10 14:18:28 -04:00
DtoOptions = new Controller . Dto . DtoOptions
{
Fields = new List < ItemFields > { ItemFields . Genres , ItemFields . Tags } ,
EnableImages = false ,
EnableUserData = false
}
2014-11-09 13:24:57 -05:00
} ;
return query ;
}
}
}