2014-03-28 19:58:18 +00:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2013-09-04 17:02:19 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-07-02 04:57:18 +00:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
2013-09-21 21:00:04 +00:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-03-28 19:58:18 +00:00
|
|
|
|
using System.Globalization;
|
2013-09-21 21:00:04 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2016-06-19 06:18:29 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2016-06-01 05:50:00 +00:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
2016-10-25 19:02:04 +00:00
|
|
|
|
using MediaBrowser.Model.Services;
|
2017-08-09 19:56:38 +00:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetSimilarGames
|
|
|
|
|
/// </summary>
|
2014-03-28 19:58:18 +00:00
|
|
|
|
[Route("/Games/{Id}/Similar", "GET", Summary = "Finds games similar to a given game.")]
|
2013-08-09 15:55:22 +00:00
|
|
|
|
public class GetSimilarGames : BaseGetSimilarItemsFromItem
|
2013-05-25 23:52:41 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 21:00:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetGameSystemSummaries
|
|
|
|
|
/// </summary>
|
2014-03-28 19:58:18 +00:00
|
|
|
|
[Route("/Games/SystemSummaries", "GET", Summary = "Finds games similar to a given game.")]
|
2017-08-19 19:43:35 +00:00
|
|
|
|
public class GetGameSystemSummaries : IReturn<GameSystemSummary[]>
|
2013-10-06 03:03:08 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the user id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The user id.</value>
|
|
|
|
|
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
|
2015-05-29 23:51:33 +00:00
|
|
|
|
public string UserId { get; set; }
|
2013-10-06 03:03:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 23:52:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GamesService
|
|
|
|
|
/// </summary>
|
2014-07-02 04:57:18 +00:00
|
|
|
|
[Authenticated]
|
2013-05-25 23:52:41 +00:00
|
|
|
|
public class GamesService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _user manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _user data repository
|
|
|
|
|
/// </summary>
|
2013-10-02 16:08:58 +00:00
|
|
|
|
private readonly IUserDataManager _userDataRepository;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
2013-09-21 21:00:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _item repo
|
|
|
|
|
/// </summary>
|
2013-06-18 19:16:27 +00:00
|
|
|
|
private readonly IItemRepository _itemRepo;
|
2013-09-21 21:00:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _dto service
|
|
|
|
|
/// </summary>
|
2013-09-04 17:02:19 +00:00
|
|
|
|
private readonly IDtoService _dtoService;
|
2013-06-18 19:16:27 +00:00
|
|
|
|
|
2016-11-10 14:41:24 +00:00
|
|
|
|
private readonly IAuthorizationContext _authContext;
|
|
|
|
|
|
2013-05-25 23:52:41 +00:00
|
|
|
|
/// <summary>
|
2013-06-18 19:16:27 +00:00
|
|
|
|
/// Initializes a new instance of the <see cref="GamesService" /> class.
|
2013-05-25 23:52:41 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userManager">The user manager.</param>
|
|
|
|
|
/// <param name="userDataRepository">The user data repository.</param>
|
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
2013-06-18 19:16:27 +00:00
|
|
|
|
/// <param name="itemRepo">The item repo.</param>
|
2013-09-21 21:00:04 +00:00
|
|
|
|
/// <param name="dtoService">The dto service.</param>
|
2016-11-10 14:41:24 +00:00
|
|
|
|
public GamesService(IUserManager userManager, IUserDataManager userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService, IAuthorizationContext authContext)
|
2013-05-25 23:52:41 +00:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_userDataRepository = userDataRepository;
|
|
|
|
|
_libraryManager = libraryManager;
|
2013-06-18 19:16:27 +00:00
|
|
|
|
_itemRepo = itemRepo;
|
2013-09-04 17:02:19 +00:00
|
|
|
|
_dtoService = dtoService;
|
2016-11-10 14:41:24 +00:00
|
|
|
|
_authContext = authContext;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 21:00:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetGameSystemSummaries request)
|
|
|
|
|
{
|
2015-11-10 18:58:05 +00:00
|
|
|
|
var user = request.UserId == null ? null : _userManager.GetUserById(request.UserId);
|
|
|
|
|
var query = new InternalItemsQuery(user)
|
|
|
|
|
{
|
2017-05-21 07:25:49 +00:00
|
|
|
|
IncludeItemTypes = new[] { typeof(GameSystem).Name },
|
|
|
|
|
DtoOptions = new DtoOptions(false)
|
|
|
|
|
{
|
|
|
|
|
EnableImages = false
|
|
|
|
|
}
|
2015-11-10 18:58:05 +00:00
|
|
|
|
};
|
2013-09-21 21:00:04 +00:00
|
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
|
var result = _libraryManager.GetItemList(query)
|
|
|
|
|
.Cast<GameSystem>()
|
2013-09-21 21:00:04 +00:00
|
|
|
|
.Select(i => GetSummary(i, user))
|
2017-08-19 19:43:35 +00:00
|
|
|
|
.ToArray();
|
2013-09-21 21:00:04 +00:00
|
|
|
|
|
2014-02-04 04:04:19 +00:00
|
|
|
|
return ToOptimizedSerializedResultUsingCache(result);
|
2013-09-21 21:00:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-06 03:03:08 +00:00
|
|
|
|
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
2014-03-28 19:58:18 +00:00
|
|
|
|
|
2013-09-21 21:00:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the summary.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="system">The system.</param>
|
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
|
/// <returns>GameSystemSummary.</returns>
|
|
|
|
|
private GameSystemSummary GetSummary(GameSystem system, User user)
|
|
|
|
|
{
|
|
|
|
|
var summary = new GameSystemSummary
|
|
|
|
|
{
|
2013-09-22 16:49:55 +00:00
|
|
|
|
Name = system.GameSystemName,
|
|
|
|
|
DisplayName = system.Name
|
2013-09-21 21:00:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-01-25 06:34:50 +00:00
|
|
|
|
var items = user == null ?
|
|
|
|
|
system.GetRecursiveChildren(i => i is Game) :
|
2016-05-18 05:34:10 +00:00
|
|
|
|
system.GetRecursiveChildren(user, new InternalItemsQuery(user)
|
|
|
|
|
{
|
2017-05-21 07:25:49 +00:00
|
|
|
|
IncludeItemTypes = new[] { typeof(Game).Name },
|
|
|
|
|
DtoOptions = new DtoOptions(false)
|
|
|
|
|
{
|
|
|
|
|
EnableImages = false
|
|
|
|
|
}
|
2016-05-18 05:34:10 +00:00
|
|
|
|
});
|
2013-09-21 21:00:04 +00:00
|
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
|
var games = items.Cast<Game>().ToArray();
|
2013-09-21 21:00:04 +00:00
|
|
|
|
|
2014-03-12 19:56:12 +00:00
|
|
|
|
summary.ClientInstalledGameCount = games.Count(i => i.IsPlaceHolder);
|
2013-09-22 22:42:21 +00:00
|
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
|
summary.GameCount = games.Length;
|
2013-09-21 21:00:04 +00:00
|
|
|
|
|
2014-03-03 05:11:03 +00:00
|
|
|
|
summary.GameFileExtensions = games.Where(i => !i.IsPlaceHolder).Select(i => Path.GetExtension(i.Path))
|
2013-09-21 21:00:04 +00:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
2017-08-19 19:43:35 +00:00
|
|
|
|
.ToArray();
|
2013-09-21 21:00:04 +00:00
|
|
|
|
|
|
|
|
|
return summary;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 23:52:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
2016-06-19 06:18:29 +00:00
|
|
|
|
public async Task<object> Get(GetSimilarGames request)
|
2013-05-25 23:52:41 +00:00
|
|
|
|
{
|
2016-06-19 06:18:29 +00:00
|
|
|
|
var result = await GetSimilarItemsResult(request).ConfigureAwait(false);
|
2016-06-01 05:50:00 +00:00
|
|
|
|
|
|
|
|
|
return ToOptimizedSerializedResultUsingCache(result);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-19 06:18:29 +00:00
|
|
|
|
private async Task<QueryResult<BaseItemDto>> GetSimilarItemsResult(BaseGetSimilarItemsFromItem request)
|
2016-06-01 05:50:00 +00:00
|
|
|
|
{
|
|
|
|
|
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
|
|
|
|
|
|
|
|
|
|
var item = string.IsNullOrEmpty(request.Id) ?
|
|
|
|
|
(!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
|
|
|
|
|
_libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);
|
|
|
|
|
|
2016-11-10 14:41:24 +00:00
|
|
|
|
var dtoOptions = GetDtoOptions(_authContext, request);
|
2016-10-08 05:57:38 +00:00
|
|
|
|
|
2016-06-01 05:50:00 +00:00
|
|
|
|
var itemsResult = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
|
|
|
|
{
|
|
|
|
|
Limit = request.Limit,
|
|
|
|
|
IncludeItemTypes = new[]
|
|
|
|
|
{
|
|
|
|
|
typeof(Game).Name
|
|
|
|
|
},
|
2016-10-08 05:57:38 +00:00
|
|
|
|
SimilarTo = item,
|
2016-10-10 18:18:28 +00:00
|
|
|
|
DtoOptions = dtoOptions
|
2016-06-01 05:50:00 +00:00
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var returnList = (await _dtoService.GetBaseItemDtos(itemsResult, dtoOptions, user).ConfigureAwait(false));
|
2016-06-01 05:50:00 +00:00
|
|
|
|
|
|
|
|
|
var result = new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
2017-08-19 19:43:35 +00:00
|
|
|
|
Items = returnList,
|
2013-05-25 23:52:41 +00:00
|
|
|
|
|
2016-06-01 05:50:00 +00:00
|
|
|
|
TotalRecordCount = itemsResult.Count
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
2013-05-25 23:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|