add movie count sort order

This commit is contained in:
Luke Pulverenti 2013-09-10 15:30:56 -04:00
parent 3c2ec758be
commit 9d7b3fdda6
6 changed files with 190 additions and 33 deletions

View File

@ -3,6 +3,7 @@ using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using ServiceStack.ServiceHost;
using System;
@ -81,7 +82,7 @@ namespace MediaBrowser.Api.UserLibrary
}
else
{
items = request.Recursive ? folder.RecursiveChildren: folder.Children;
items = request.Recursive ? folder.RecursiveChildren : folder.Children;
}
}
else
@ -149,7 +150,7 @@ namespace MediaBrowser.Api.UserLibrary
{
items = items.Where(item => imageTypes.Any(imageType => ItemsService.HasImage(item.GetItem().Result, imageType)));
}
var filters = request.GetFilters().ToList();
if (filters.Count == 0)
@ -162,18 +163,20 @@ namespace MediaBrowser.Api.UserLibrary
if (filters.Contains(ItemFilter.Dislikes))
{
items = items.Where(i =>
{
var userdata = i.GetUserItemData(UserDataRepository, user.Id).Result;
{
var item = i.GetItem().Result;
var userdata = UserDataRepository.GetUserData(user.Id, item.GetUserDataKey());
return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
});
return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
});
}
if (filters.Contains(ItemFilter.Likes))
{
items = items.Where(i =>
{
var userdata = i.GetUserItemData(UserDataRepository, user.Id).Result;
var item = i.GetItem().Result;
var userdata = UserDataRepository.GetUserData(user.Id, item.GetUserDataKey());
return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value;
});
@ -183,12 +186,8 @@ namespace MediaBrowser.Api.UserLibrary
{
items = items.Where(i =>
{
var userdata = i.GetUserItemData(UserDataRepository, user.Id).Result;
if (userdata == null)
{
return false;
}
var item = i.GetItem().Result;
var userdata = UserDataRepository.GetUserData(user.Id, item.GetUserDataKey());
var likes = userdata.Likes ?? false;
var favorite = userdata.IsFavorite;
@ -196,20 +195,21 @@ namespace MediaBrowser.Api.UserLibrary
return likes || favorite;
});
}
if (filters.Contains(ItemFilter.IsFavorite))
{
items = items.Where(i =>
{
var userdata = i.GetUserItemData(UserDataRepository, user.Id).Result;
var item = i.GetItem().Result;
var userdata = UserDataRepository.GetUserData(user.Id, item.GetUserDataKey());
return userdata != null && userdata.Likes.HasValue && userdata.IsFavorite;
});
}
return items.AsEnumerable();
}
/// <summary>
/// Sorts the items.
/// </summary>
@ -220,7 +220,7 @@ namespace MediaBrowser.Api.UserLibrary
{
if (string.Equals(request.SortBy, "SortName", StringComparison.OrdinalIgnoreCase))
{
if (request.SortOrder.HasValue && request.SortOrder.Value == Model.Entities.SortOrder.Descending)
if (request.SortOrder.HasValue && request.SortOrder.Value == SortOrder.Descending)
{
items = items.OrderByDescending(i => i.Name);
}
@ -231,7 +231,7 @@ namespace MediaBrowser.Api.UserLibrary
}
else if (string.Equals(request.SortBy, "Random", StringComparison.OrdinalIgnoreCase))
{
if (request.SortOrder.HasValue && request.SortOrder.Value == Model.Entities.SortOrder.Descending)
if (request.SortOrder.HasValue && request.SortOrder.Value == SortOrder.Descending)
{
items = items.OrderByDescending(i => Guid.NewGuid());
}
@ -273,7 +273,7 @@ namespace MediaBrowser.Api.UserLibrary
items = items.Where(f => vals.Contains(f.MediaType ?? string.Empty, StringComparer.OrdinalIgnoreCase));
}
return items;
}
@ -344,7 +344,7 @@ namespace MediaBrowser.Api.UserLibrary
[ApiMember(Name = "NameStartsWithOrGreater", Description = "Optional filter by items whose name is sorted equally or greater than a given input string.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string NameStartsWithOrGreater { get; set; }
/// <summary>
/// What to sort the results by
/// </summary>
@ -361,26 +361,17 @@ namespace MediaBrowser.Api.UserLibrary
public class IbnStub<T>
where T : BaseItem
{
private readonly Func<string,Task<T>> _itemFunction;
private readonly Func<string, Task<T>> _itemFunction;
private Task<T> _itemTask;
public string Name;
private UserItemData _userData;
public string Name;
public Task<T> GetItem()
{
return _itemTask ?? (_itemTask = _itemFunction(Name));
}
public async Task<UserItemData> GetUserItemData(IUserDataRepository repo, Guid userId)
{
var item = await GetItem().ConfigureAwait(false);
return _userData ?? (_userData = repo.GetUserData(userId, item.GetUserDataKey()));
}
public IbnStub(string name, Func<string,Task<T>> item)
public IbnStub(string name, Func<string, Task<T>> item)
{
Name = name;
_itemFunction = item;

View File

@ -13,4 +13,24 @@ namespace MediaBrowser.Controller.Entities
Dictionary<Guid, ItemByNameCounts> UserItemCounts { get; set; }
}
public static class IItemByNameExtensions
{
public static ItemByNameCounts GetItemByNameCounts(this IItemByName item, User user)
{
if (user == null)
{
return item.ItemCounts;
}
ItemByNameCounts counts;
if (item.UserItemCounts.TryGetValue(user.Id, out counts))
{
return counts;
}
return null;
}
}
}

View File

@ -74,5 +74,7 @@ namespace MediaBrowser.Model.Querying
public const string IsFolder = "IsFolder";
public const string IsUnplayed = "IsUnplayed";
public const string IsPlayed = "IsPlayed";
public const string TrailerCount = "TrailerCount";
public const string MovieCount = "MovieCount";
}
}

View File

@ -189,6 +189,7 @@
<Compile Include="Sorting\DatePlayedComparer.cs" />
<Compile Include="Sorting\IsFolderComparer.cs" />
<Compile Include="Sorting\IsUnplayedComparer.cs" />
<Compile Include="Sorting\MovieCountComparer.cs" />
<Compile Include="Sorting\NameComparer.cs" />
<Compile Include="Sorting\OfficialRatingComparer.cs" />
<Compile Include="Sorting\PlayCountComparer.cs" />
@ -202,6 +203,7 @@
<Compile Include="Persistence\SqliteItemRepository.cs" />
<Compile Include="Persistence\SqliteUserDataRepository.cs" />
<Compile Include="Persistence\SqliteUserRepository.cs" />
<Compile Include="Sorting\TrailerCountComparer.cs" />
<Compile Include="Udp\UdpMessageReceivedEventArgs.cs" />
<Compile Include="Udp\UdpServer.cs" />
<Compile Include="WebSocket\AlchemyServer.cs" />

View File

@ -0,0 +1,71 @@
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Sorting;
using MediaBrowser.Model.Querying;
namespace MediaBrowser.Server.Implementations.Sorting
{
public class MovieCountComparer : IUserBaseItemComparer
{
/// <summary>
/// Gets or sets the user.
/// </summary>
/// <value>The user.</value>
public User User { get; set; }
/// <summary>
/// Gets or sets the user manager.
/// </summary>
/// <value>The user manager.</value>
public IUserManager UserManager { get; set; }
/// <summary>
/// Gets or sets the user data repository.
/// </summary>
/// <value>The user data repository.</value>
public IUserDataRepository UserDataRepository { get; set; }
/// <summary>
/// Compares the specified x.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>System.Int32.</returns>
public int Compare(BaseItem x, BaseItem y)
{
return GetValue(x).CompareTo(GetValue(y));
}
/// <summary>
/// Gets the date.
/// </summary>
/// <param name="x">The x.</param>
/// <returns>DateTime.</returns>
private int GetValue(BaseItem x)
{
var itemByName = x as IItemByName;
if (itemByName != null)
{
var counts = itemByName.GetItemByNameCounts(User);
if (counts != null)
{
return counts.MovieCount;
}
}
return 0;
}
/// <summary>
/// Gets the name.
/// </summary>
/// <value>The name.</value>
public string Name
{
get { return ItemSortBy.MovieCount; }
}
}
}

View File

@ -0,0 +1,71 @@
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Sorting;
using MediaBrowser.Model.Querying;
namespace MediaBrowser.Server.Implementations.Sorting
{
public class TrailerCountComparer : IUserBaseItemComparer
{
/// <summary>
/// Gets or sets the user.
/// </summary>
/// <value>The user.</value>
public User User { get; set; }
/// <summary>
/// Gets or sets the user manager.
/// </summary>
/// <value>The user manager.</value>
public IUserManager UserManager { get; set; }
/// <summary>
/// Gets or sets the user data repository.
/// </summary>
/// <value>The user data repository.</value>
public IUserDataRepository UserDataRepository { get; set; }
/// <summary>
/// Compares the specified x.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>System.Int32.</returns>
public int Compare(BaseItem x, BaseItem y)
{
return GetValue(x).CompareTo(GetValue(y));
}
/// <summary>
/// Gets the date.
/// </summary>
/// <param name="x">The x.</param>
/// <returns>DateTime.</returns>
private int GetValue(BaseItem x)
{
var itemByName = x as IItemByName;
if (itemByName != null)
{
var counts = itemByName.GetItemByNameCounts(User);
if (counts != null)
{
return counts.TrailerCount;
}
}
return 0;
}
/// <summary>
/// Gets the name.
/// </summary>
/// <value>The name.</value>
public string Name
{
get { return ItemSortBy.TrailerCount; }
}
}
}