2014-05-08 20:09:53 +00:00
using MediaBrowser.Controller.Dto ;
2013-04-13 18:02:30 +00:00
using MediaBrowser.Controller.Entities ;
2013-08-31 01:08:32 +00:00
using MediaBrowser.Controller.Entities.TV ;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Library ;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net ;
2013-02-21 17:50:46 +00:00
using MediaBrowser.Model.Dto ;
2013-10-02 17:23:10 +00:00
using MediaBrowser.Model.Entities ;
2014-06-07 19:46:24 +00:00
using MediaBrowser.Model.Library ;
2013-03-10 05:36:39 +00:00
using MediaBrowser.Model.Querying ;
2014-10-31 04:57:24 +00:00
using MoreLinq ;
2013-12-07 15:52:38 +00:00
using ServiceStack ;
2013-02-21 01:33:05 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2013-04-05 17:10:55 +00:00
using System.Threading ;
2013-02-21 01:33:05 +00:00
using System.Threading.Tasks ;
namespace MediaBrowser.Api.UserLibrary
{
/// <summary>
/// Class GetItem
/// </summary>
2014-11-15 02:31:03 +00:00
[Route("/Users/{UserId}/Items/{Id}", "GET", Summary = "Gets an item from a user's library")]
2013-02-21 17:50:46 +00:00
public class GetItem : IReturn < BaseItemDto >
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 22:41:38 +00:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-21 01:33:05 +00:00
public Guid UserId { get ; set ; }
2013-02-24 21:53:54 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 23:10:37 +00:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-21 01:33:05 +00:00
public string Id { get ; set ; }
}
2014-06-05 02:32:40 +00:00
[Route("/Users/{UserId}/Views", "GET")]
public class GetUserViews : IReturn < QueryResult < BaseItemDto > >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2014-06-07 19:46:24 +00:00
public string UserId { get ; set ; }
[ApiMember(Name = "IncludeExternalContent", Description = "Whether or not to include external views such as channels or live tv", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "POST")]
public bool? IncludeExternalContent { get ; set ; }
2014-06-05 02:32:40 +00:00
}
2013-03-09 02:34:54 +00:00
/// <summary>
/// Class GetItem
/// </summary>
2014-11-15 02:31:03 +00:00
[Route("/Users/{UserId}/Items/Root", "GET", Summary = "Gets the root folder from a user's library")]
2013-03-09 02:34:54 +00:00
public class GetRootFolder : IReturn < BaseItemDto >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public Guid UserId { get ; set ; }
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Class GetIntros
/// </summary>
2014-11-15 02:31:03 +00:00
[Route("/Users/{UserId}/Items/{Id}/Intros", "GET", Summary = "Gets intros to play before the main media item plays")]
2013-10-04 17:04:18 +00:00
public class GetIntros : IReturn < ItemsResult >
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 22:41:38 +00:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-21 01:33:05 +00:00
public Guid UserId { get ; set ; }
2013-02-24 21:53:54 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets or sets the item id.
/// </summary>
/// <value>The item id.</value>
2013-03-08 23:10:37 +00:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-21 01:33:05 +00:00
public string Id { get ; set ; }
}
/// <summary>
/// Class MarkFavoriteItem
/// </summary>
2014-11-15 02:31:03 +00:00
[Route("/Users/{UserId}/FavoriteItems/{Id}", "POST", Summary = "Marks an item as a favorite")]
2013-08-22 21:33:39 +00:00
public class MarkFavoriteItem : IReturn < UserItemDataDto >
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 22:41:38 +00:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2013-02-21 01:33:05 +00:00
public Guid UserId { get ; set ; }
2013-02-24 21:53:54 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 23:10:37 +00:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2013-02-21 01:33:05 +00:00
public string Id { get ; set ; }
}
/// <summary>
/// Class UnmarkFavoriteItem
/// </summary>
2014-11-15 02:31:03 +00:00
[Route("/Users/{UserId}/FavoriteItems/{Id}", "DELETE", Summary = "Unmarks an item as a favorite")]
2013-08-22 21:33:39 +00:00
public class UnmarkFavoriteItem : IReturn < UserItemDataDto >
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 22:41:38 +00:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2013-02-21 01:33:05 +00:00
public Guid UserId { get ; set ; }
2013-02-24 21:53:54 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 23:10:37 +00:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2013-02-21 01:33:05 +00:00
public string Id { get ; set ; }
}
/// <summary>
/// Class ClearUserItemRating
/// </summary>
2014-11-15 02:31:03 +00:00
[Route("/Users/{UserId}/Items/{Id}/Rating", "DELETE", Summary = "Deletes a user's saved personal rating for an item")]
2013-08-22 21:33:39 +00:00
public class DeleteUserItemRating : IReturn < UserItemDataDto >
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 22:41:38 +00:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2013-02-21 01:33:05 +00:00
public Guid UserId { get ; set ; }
2013-02-24 21:53:54 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 23:10:37 +00:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2013-02-21 01:33:05 +00:00
public string Id { get ; set ; }
}
/// <summary>
/// Class UpdateUserItemRating
/// </summary>
2014-11-15 02:31:03 +00:00
[Route("/Users/{UserId}/Items/{Id}/Rating", "POST", Summary = "Updates a user's rating for an item")]
2013-08-22 21:33:39 +00:00
public class UpdateUserItemRating : IReturn < UserItemDataDto >
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 22:41:38 +00:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2013-02-21 01:33:05 +00:00
public Guid UserId { get ; set ; }
2013-02-24 21:53:54 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 23:10:37 +00:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2013-02-21 01:33:05 +00:00
public string Id { get ; set ; }
/// <summary>
2013-04-24 16:37:12 +00:00
/// Gets or sets a value indicating whether this <see cref="UpdateUserItemRating" /> is likes.
2013-02-21 01:33:05 +00:00
/// </summary>
/// <value><c>true</c> if likes; otherwise, <c>false</c>.</value>
2013-03-08 23:10:37 +00:00
[ApiMember(Name = "Likes", Description = "Whether the user likes the item or not. true/false", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "POST")]
2013-02-21 01:33:05 +00:00
public bool Likes { get ; set ; }
}
/// <summary>
2014-07-05 05:21:13 +00:00
/// Class GetLocalTrailers
2013-04-24 16:37:12 +00:00
/// </summary>
2014-11-15 02:31:03 +00:00
[Route("/Users/{UserId}/Items/{Id}/LocalTrailers", "GET", Summary = "Gets local trailers for an item")]
2014-07-05 05:21:13 +00:00
public class GetLocalTrailers : IReturn < List < BaseItemDto > >
2013-02-25 02:41:51 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-25 02:41:51 +00:00
public Guid UserId { get ; set ; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-25 02:41:51 +00:00
public string Id { get ; set ; }
}
2013-04-24 16:37:12 +00:00
/// <summary>
2014-07-05 05:21:13 +00:00
/// Class GetSpecialFeatures
2013-04-24 16:37:12 +00:00
/// </summary>
2014-11-15 02:31:03 +00:00
[Route("/Users/{UserId}/Items/{Id}/SpecialFeatures", "GET", Summary = "Gets special features for an item")]
2014-07-05 05:21:13 +00:00
public class GetSpecialFeatures : IReturn < List < BaseItemDto > >
2013-02-25 02:41:51 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-25 02:41:51 +00:00
public Guid UserId { get ; set ; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "Id", Description = "Movie Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-25 02:41:51 +00:00
public string Id { get ; set ; }
}
2014-07-05 05:21:13 +00:00
[Route("/Users/{UserId}/Items/Latest", "GET", Summary = "Gets latest media")]
2014-12-01 12:43:34 +00:00
public class GetLatestMedia : IReturn < List < BaseItemDto > > , IHasDtoOptions
2013-02-25 02:41:51 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-25 02:41:51 +00:00
public Guid UserId { get ; set ; }
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "Limit", Description = "Limit", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int Limit { get ; set ; }
2013-02-25 02:41:51 +00:00
2014-07-05 05:21:13 +00:00
[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 ; }
2014-04-06 17:53:23 +00:00
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, CriticRatingSummary, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string Fields { get ; set ; }
2013-04-24 16:37:12 +00:00
2014-07-05 05:21:13 +00:00
[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 ; }
2013-02-21 01:33:05 +00:00
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "IsFolder", Description = "Filter by items that are folders, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool? IsFolder { get ; set ; }
2013-02-21 01:33:05 +00:00
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "IsPlayed", Description = "Filter by items that are played, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool? IsPlayed { get ; set ; }
2013-02-21 01:33:05 +00:00
2014-07-05 05:21:13 +00:00
[ApiMember(Name = "GroupItems", Description = "Whether or not to group items into a parent container.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool GroupItems { get ; set ; }
2014-10-31 04:57:24 +00:00
2014-12-01 12:43:34 +00:00
[ApiMember(Name = "EnableImages", Description = "Optional, include image information in output", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? EnableImages { get ; set ; }
[ApiMember(Name = "ImageTypeLimit", Description = "Optional, the max number of images to return, per image type", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? ImageTypeLimit { get ; set ; }
[ApiMember(Name = "EnableImageTypes", Description = "Optional. The image types to include in the output.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string EnableImageTypes { get ; set ; }
2015-01-25 06:34:50 +00:00
2014-07-05 05:21:13 +00:00
public GetLatestMedia ( )
{
Limit = 20 ;
GroupItems = true ;
}
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Class UserLibraryService
/// </summary>
2014-07-02 18:34:08 +00:00
[Authenticated]
2013-03-16 05:52:33 +00:00
public class UserLibraryService : BaseApiService
2013-02-21 01:33:05 +00:00
{
2013-02-27 20:25:45 +00:00
private readonly IUserManager _userManager ;
2013-10-02 16:08:58 +00:00
private readonly IUserDataManager _userDataRepository ;
2013-02-28 19:32:41 +00:00
private readonly ILibraryManager _libraryManager ;
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService ;
2014-06-07 19:46:24 +00:00
private readonly IUserViewManager _userViewManager ;
2014-07-05 05:21:13 +00:00
public UserLibraryService ( IUserManager userManager , ILibraryManager libraryManager , IUserDataManager userDataRepository , IDtoService dtoService , IUserViewManager userViewManager )
2013-02-24 21:53:54 +00:00
{
2013-02-27 20:25:45 +00:00
_userManager = userManager ;
2013-02-28 19:32:41 +00:00
_libraryManager = libraryManager ;
2013-04-13 18:02:30 +00:00
_userDataRepository = userDataRepository ;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService ;
2014-06-07 19:46:24 +00:00
_userViewManager = userViewManager ;
2013-02-24 21:53:54 +00:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2013-02-21 01:33:05 +00:00
public object Get ( GetSpecialFeatures request )
2013-08-22 21:33:39 +00:00
{
2013-09-17 02:44:06 +00:00
var result = GetAsync ( request ) ;
2013-08-22 21:33:39 +00:00
2014-02-04 04:04:19 +00:00
return ToOptimizedSerializedResultUsingCache ( result ) ;
2013-08-22 21:33:39 +00:00
}
2014-07-05 05:21:13 +00:00
public object Get ( GetLatestMedia request )
{
var user = _userManager . GetUserById ( request . UserId ) ;
2015-01-25 06:34:50 +00:00
var includeTypes = string . IsNullOrWhiteSpace ( request . IncludeItemTypes )
? new string [ ] { }
: request . IncludeItemTypes . Split ( ',' ) ;
2014-10-31 04:57:24 +00:00
2015-01-25 06:34:50 +00:00
var currentUser = user ;
Func < BaseItem , bool > filter = i = >
{
if ( includeTypes . Length > 0 )
{
if ( ! includeTypes . Contains ( i . GetType ( ) . Name , StringComparer . OrdinalIgnoreCase ) )
{
return false ;
}
}
2014-07-05 05:21:13 +00:00
2015-01-25 06:34:50 +00:00
if ( request . IsPlayed . HasValue )
{
var val = request . IsPlayed . Value ;
if ( i . IsPlayed ( currentUser ) ! = val )
{
return false ;
}
}
2014-08-14 13:24:30 +00:00
2015-01-25 06:34:50 +00:00
return i . LocationType ! = LocationType . Virtual & & ! i . IsFolder ;
} ;
2014-10-31 04:57:24 +00:00
2015-01-25 06:34:50 +00:00
// Avoid implicitly captured closure
var libraryItems = string . IsNullOrEmpty ( request . ParentId ) & & user ! = null ?
GetItemsConfiguredForLatest ( user , filter ) :
GetAllLibraryItems ( request . UserId , _userManager , _libraryManager , request . ParentId , filter ) ;
2014-07-05 05:21:13 +00:00
2015-01-25 06:34:50 +00:00
libraryItems = libraryItems . OrderByDescending ( i = > i . DateCreated ) ;
2014-07-05 05:21:13 +00:00
if ( request . IsPlayed . HasValue )
{
var takeLimit = request . Limit * 20 ;
2015-01-25 06:34:50 +00:00
libraryItems = libraryItems . Take ( takeLimit ) ;
2014-07-05 05:21:13 +00:00
}
2014-10-31 04:57:24 +00:00
2014-07-05 05:21:13 +00:00
// Avoid implicitly captured closure
var items = libraryItems
. ToList ( ) ;
var list = new List < Tuple < BaseItem , List < BaseItem > > > ( ) ;
foreach ( var item in items )
{
// Only grab the index container for media
var container = item . IsFolder | | ! request . GroupItems ? null : item . LatestItemsIndexContainer ;
if ( container = = null )
{
list . Add ( new Tuple < BaseItem , List < BaseItem > > ( null , new List < BaseItem > { item } ) ) ;
}
else
{
var current = list . FirstOrDefault ( i = > i . Item1 ! = null & & i . Item1 . Id = = container . Id ) ;
if ( current ! = null )
{
current . Item2 . Add ( item ) ;
}
else
{
list . Add ( new Tuple < BaseItem , List < BaseItem > > ( container , new List < BaseItem > { item } ) ) ;
}
}
if ( list . Count > = request . Limit )
{
break ;
}
}
2015-01-24 19:03:55 +00:00
var options = GetDtoOptions ( request ) ;
2014-07-05 05:21:13 +00:00
var dtos = list . Select ( i = >
{
var item = i . Item2 [ 0 ] ;
var childCount = 0 ;
if ( i . Item1 ! = null & & i . Item2 . Count > 0 )
{
item = i . Item1 ;
childCount = i . Item2 . Count ;
}
2014-12-01 12:43:34 +00:00
var dto = _dtoService . GetBaseItemDto ( item , options , user ) ;
2014-07-05 05:21:13 +00:00
dto . ChildCount = childCount ;
return dto ;
} ) ;
return ToOptimizedResult ( dtos . ToList ( ) ) ;
}
2015-01-25 06:34:50 +00:00
private IEnumerable < BaseItem > GetItemsConfiguredForLatest ( User user , Func < BaseItem , bool > filter )
2014-10-31 04:57:24 +00:00
{
2015-01-25 06:34:50 +00:00
// Avoid implicitly captured closure
var currentUser = user ;
2014-10-31 04:57:24 +00:00
return user . RootFolder . GetChildren ( user , true )
. OfType < Folder > ( )
. Where ( i = > ! user . Configuration . LatestItemsExcludes . Contains ( i . Id . ToString ( "N" ) ) )
2015-01-25 06:34:50 +00:00
. SelectMany ( i = > i . GetRecursiveChildren ( currentUser , filter ) )
2014-10-31 04:57:24 +00:00
. DistinctBy ( i = > i . Id ) ;
}
2014-08-17 05:38:13 +00:00
public async Task < object > Get ( GetUserViews request )
2014-06-05 02:32:40 +00:00
{
2014-09-14 15:10:51 +00:00
var user = _userManager . GetUserById ( request . UserId ) ;
2014-06-05 02:32:40 +00:00
2014-06-07 19:46:24 +00:00
var query = new UserViewQuery
{
UserId = request . UserId
} ;
if ( request . IncludeExternalContent . HasValue )
{
query . IncludeExternalContent = request . IncludeExternalContent . Value ;
}
2014-08-17 05:38:13 +00:00
var folders = await _userViewManager . GetUserViews ( query , CancellationToken . None ) . ConfigureAwait ( false ) ;
2014-06-05 02:32:40 +00:00
2015-01-24 19:03:55 +00:00
var dtoOptions = GetDtoOptions ( request ) ;
2014-12-27 05:08:39 +00:00
var dtos = folders . Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user ) )
2014-06-05 02:32:40 +00:00
. ToArray ( ) ;
var result = new QueryResult < BaseItemDto >
{
Items = dtos ,
TotalRecordCount = dtos . Length
} ;
return ToOptimizedResult ( result ) ;
}
2013-09-20 00:53:18 +00:00
private List < BaseItemDto > GetAsync ( GetSpecialFeatures request )
2013-02-21 01:33:05 +00:00
{
2013-02-27 20:25:45 +00:00
var user = _userManager . GetUserById ( request . UserId ) ;
2013-02-21 01:33:05 +00:00
2014-06-05 02:32:40 +00:00
var item = string . IsNullOrEmpty ( request . Id ) ?
user . RootFolder :
2014-04-25 20:15:50 +00:00
_libraryManager . GetItemById ( request . Id ) ;
2013-02-24 21:53:54 +00:00
2013-08-31 01:08:32 +00:00
var series = item as Series ;
// Get them from the child tree
if ( series ! = null )
{
2015-01-24 19:03:55 +00:00
var dtoOptions = GetDtoOptions ( request ) ;
2014-12-27 05:08:39 +00:00
// Avoid implicitly captured closure
var currentUser = user ;
2013-09-17 02:44:06 +00:00
var dtos = series
2015-01-25 06:34:50 +00:00
. GetRecursiveChildren ( i = > i is Episode & & i . ParentIndexNumber . HasValue & & i . ParentIndexNumber . Value = = 0 )
2013-08-31 14:55:10 +00:00
. OrderBy ( i = >
{
if ( i . PremiereDate . HasValue )
{
return i . PremiereDate . Value ;
}
if ( i . ProductionYear . HasValue )
{
return new DateTime ( i . ProductionYear . Value , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
}
return DateTime . MinValue ;
} )
. ThenBy ( i = > i . SortName )
2014-12-27 05:08:39 +00:00
. Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , currentUser ) ) ;
2013-08-31 01:08:32 +00:00
2013-09-20 00:53:18 +00:00
return dtos . ToList ( ) ;
2013-08-31 01:08:32 +00:00
}
2014-09-20 15:48:23 +00:00
var movie = item as IHasSpecialFeatures ;
// Get them from the db
if ( movie ! = null )
{
2015-01-24 19:03:55 +00:00
var dtoOptions = GetDtoOptions ( request ) ;
2014-12-27 05:08:39 +00:00
2014-09-20 15:48:23 +00:00
var dtos = movie . SpecialFeatureIds
. Select ( _libraryManager . GetItemById )
. OrderBy ( i = > i . SortName )
2014-12-27 05:08:39 +00:00
. Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user , item ) ) ;
2014-09-20 15:48:23 +00:00
return dtos . ToList ( ) ;
}
2014-06-09 19:16:14 +00:00
return new List < BaseItemDto > ( ) ;
2013-02-21 01:33:05 +00:00
}
2013-02-24 21:53:54 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2013-02-21 01:33:05 +00:00
public object Get ( GetLocalTrailers request )
2013-08-22 21:33:39 +00:00
{
2013-09-17 02:44:06 +00:00
var result = GetAsync ( request ) ;
2013-08-22 21:33:39 +00:00
2014-02-04 04:04:19 +00:00
return ToOptimizedSerializedResultUsingCache ( result ) ;
2013-08-22 21:33:39 +00:00
}
2013-09-20 00:53:18 +00:00
private List < BaseItemDto > GetAsync ( GetLocalTrailers request )
2013-02-21 01:33:05 +00:00
{
2013-02-27 20:25:45 +00:00
var user = _userManager . GetUserById ( request . UserId ) ;
2013-02-21 01:33:05 +00:00
2014-04-25 20:15:50 +00:00
var item = string . IsNullOrEmpty ( request . Id ) ? user . RootFolder : _libraryManager . GetItemById ( request . Id ) ;
2013-02-24 21:53:54 +00:00
2013-12-02 16:46:25 +00:00
var trailerIds = new List < Guid > ( ) ;
var hasTrailers = item as IHasTrailers ;
if ( hasTrailers ! = null )
{
2014-12-11 06:20:28 +00:00
trailerIds = hasTrailers . GetTrailerIds ( ) ;
2013-12-02 16:46:25 +00:00
}
2015-01-24 19:03:55 +00:00
var dtoOptions = GetDtoOptions ( request ) ;
2014-12-27 05:08:39 +00:00
2013-12-02 16:46:25 +00:00
var dtos = trailerIds
2013-09-25 22:35:23 +00:00
. Select ( _libraryManager . GetItemById )
2013-07-30 14:51:28 +00:00
. OrderBy ( i = > i . SortName )
2014-12-27 05:08:39 +00:00
. Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user , item ) ) ;
2013-02-21 01:33:05 +00:00
2013-09-20 00:53:18 +00:00
return dtos . ToList ( ) ;
2013-02-21 01:33:05 +00:00
}
2013-02-24 21:53:54 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetItem request )
{
2013-02-27 20:25:45 +00:00
var user = _userManager . GetUserById ( request . UserId ) ;
2013-02-21 01:33:05 +00:00
2014-04-25 20:15:50 +00:00
var item = string . IsNullOrEmpty ( request . Id ) ? user . RootFolder : _libraryManager . GetItemById ( request . Id ) ;
2013-02-24 21:53:54 +00:00
2015-01-24 19:03:55 +00:00
var dtoOptions = GetDtoOptions ( request ) ;
2013-02-21 01:33:05 +00:00
2014-12-27 05:08:39 +00:00
var result = _dtoService . GetBaseItemDto ( item , dtoOptions , user ) ;
2013-02-21 01:33:05 +00:00
2014-02-04 04:04:19 +00:00
return ToOptimizedSerializedResultUsingCache ( result ) ;
2013-02-21 01:33:05 +00:00
}
2013-04-24 16:37:12 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2013-03-09 02:34:54 +00:00
public object Get ( GetRootFolder request )
{
var user = _userManager . GetUserById ( request . UserId ) ;
var item = user . RootFolder ;
2015-01-24 19:03:55 +00:00
var dtoOptions = GetDtoOptions ( request ) ;
2013-03-09 02:34:54 +00:00
2014-12-27 05:08:39 +00:00
var result = _dtoService . GetBaseItemDto ( item , dtoOptions , user ) ;
2013-03-09 02:34:54 +00:00
2014-02-04 04:04:19 +00:00
return ToOptimizedSerializedResultUsingCache ( result ) ;
2013-03-09 02:34:54 +00:00
}
2013-04-24 16:37:12 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2014-09-22 21:56:54 +00:00
public async Task < object > Get ( GetIntros request )
2013-02-21 01:33:05 +00:00
{
2013-02-27 20:25:45 +00:00
var user = _userManager . GetUserById ( request . UserId ) ;
2013-02-21 01:33:05 +00:00
2014-04-25 20:15:50 +00:00
var item = string . IsNullOrEmpty ( request . Id ) ? user . RootFolder : _libraryManager . GetItemById ( request . Id ) ;
2013-02-21 01:33:05 +00:00
2014-09-22 21:56:54 +00:00
var items = await _libraryManager . GetIntros ( item , user ) . ConfigureAwait ( false ) ;
2013-10-04 17:04:18 +00:00
2015-01-24 19:03:55 +00:00
var dtoOptions = GetDtoOptions ( request ) ;
2013-10-04 17:04:18 +00:00
2014-12-27 05:08:39 +00:00
var dtos = items . Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user ) )
2013-10-04 17:04:18 +00:00
. ToArray ( ) ;
var result = new ItemsResult
{
Items = dtos ,
TotalRecordCount = dtos . Length
} ;
2013-02-21 01:33:05 +00:00
2014-02-04 04:04:19 +00:00
return ToOptimizedSerializedResultUsingCache ( result ) ;
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2013-08-22 21:56:19 +00:00
public object Post ( MarkFavoriteItem request )
2013-02-21 01:33:05 +00:00
{
2013-08-23 01:36:34 +00:00
var dto = MarkFavorite ( request . UserId , request . Id , true ) . Result ;
2013-08-22 21:56:19 +00:00
return ToOptimizedResult ( dto ) ;
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
2013-08-22 21:56:19 +00:00
public object Delete ( UnmarkFavoriteItem request )
2013-02-21 01:33:05 +00:00
{
2013-08-23 01:36:34 +00:00
var dto = MarkFavorite ( request . UserId , request . Id , false ) . Result ;
2013-02-21 01:33:05 +00:00
2013-08-23 01:36:34 +00:00
return ToOptimizedResult ( dto ) ;
}
2013-10-04 17:04:18 +00:00
/// <summary>
/// Marks the favorite.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="itemId">The item id.</param>
/// <param name="isFavorite">if set to <c>true</c> [is favorite].</param>
/// <returns>Task{UserItemDataDto}.</returns>
2013-08-23 01:36:34 +00:00
private async Task < UserItemDataDto > MarkFavorite ( Guid userId , string itemId , bool isFavorite )
{
var user = _userManager . GetUserById ( userId ) ;
2014-04-25 20:15:50 +00:00
var item = string . IsNullOrEmpty ( itemId ) ? user . RootFolder : _libraryManager . GetItemById ( itemId ) ;
2013-02-21 01:33:05 +00:00
2013-04-13 18:02:30 +00:00
var key = item . GetUserDataKey ( ) ;
2013-04-24 16:37:12 +00:00
2013-02-21 01:33:05 +00:00
// Get the user data for this item
2013-06-17 20:35:43 +00:00
var data = _userDataRepository . GetUserData ( user . Id , key ) ;
2013-02-21 01:33:05 +00:00
// Set favorite status
2013-08-23 01:36:34 +00:00
data . IsFavorite = isFavorite ;
2013-02-21 01:33:05 +00:00
2013-10-23 16:03:12 +00:00
await _userDataRepository . SaveUserData ( user . Id , item , data , UserDataSaveReason . UpdateUserRating , CancellationToken . None ) . ConfigureAwait ( false ) ;
2013-08-22 21:56:19 +00:00
2014-07-04 02:22:57 +00:00
return _userDataRepository . GetUserDataDto ( item , user ) ;
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
2013-08-22 21:56:19 +00:00
public object Delete ( DeleteUserItemRating request )
2013-02-21 01:33:05 +00:00
{
2013-08-23 01:36:34 +00:00
var dto = UpdateUserItemRating ( request . UserId , request . Id , null ) . Result ;
2013-08-22 21:56:19 +00:00
return ToOptimizedResult ( dto ) ;
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2013-08-22 21:56:19 +00:00
public object Post ( UpdateUserItemRating request )
2013-02-21 01:33:05 +00:00
{
2013-08-23 01:36:34 +00:00
var dto = UpdateUserItemRating ( request . UserId , request . Id , request . Likes ) . Result ;
2013-02-21 01:33:05 +00:00
2013-08-23 01:36:34 +00:00
return ToOptimizedResult ( dto ) ;
}
2013-10-04 17:04:18 +00:00
/// <summary>
/// Updates the user item rating.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="itemId">The item id.</param>
/// <param name="likes">if set to <c>true</c> [likes].</param>
/// <returns>Task{UserItemDataDto}.</returns>
2013-08-23 01:36:34 +00:00
private async Task < UserItemDataDto > UpdateUserItemRating ( Guid userId , string itemId , bool? likes )
{
var user = _userManager . GetUserById ( userId ) ;
2014-04-25 20:15:50 +00:00
var item = string . IsNullOrEmpty ( itemId ) ? user . RootFolder : _libraryManager . GetItemById ( itemId ) ;
2013-02-21 01:33:05 +00:00
2013-04-13 18:02:30 +00:00
var key = item . GetUserDataKey ( ) ;
2013-04-24 16:37:12 +00:00
2013-02-21 01:33:05 +00:00
// Get the user data for this item
2013-06-17 20:35:43 +00:00
var data = _userDataRepository . GetUserData ( user . Id , key ) ;
2013-02-21 01:33:05 +00:00
2013-08-23 01:36:34 +00:00
data . Likes = likes ;
2013-02-21 01:33:05 +00:00
2013-10-23 16:03:12 +00:00
await _userDataRepository . SaveUserData ( user . Id , item , data , UserDataSaveReason . UpdateUserRating , CancellationToken . None ) . ConfigureAwait ( false ) ;
2013-08-22 21:56:19 +00:00
2014-07-04 02:22:57 +00:00
return _userDataRepository . GetUserDataDto ( item , user ) ;
2013-02-21 01:33:05 +00:00
}
}
}