mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Add watchlist filters and fix after id
This commit is contained in:
parent
c289161400
commit
bab97fba5f
@ -37,6 +37,7 @@ public interface IWatchStatusRepository
|
|||||||
// public delegate Task ResourceEventHandler(T resource);
|
// public delegate Task ResourceEventHandler(T resource);
|
||||||
|
|
||||||
Task<ICollection<IWatchlist>> GetAll(
|
Task<ICollection<IWatchlist>> GetAll(
|
||||||
|
Filter<IWatchlist>? filter = default,
|
||||||
Include<IWatchlist>? include = default,
|
Include<IWatchlist>? include = default,
|
||||||
Pagination? limit = default);
|
Pagination? limit = default);
|
||||||
|
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
using Kyoo.Abstractions.Controllers;
|
|
||||||
using Kyoo.Abstractions.Models.Attributes;
|
using Kyoo.Abstractions.Models.Attributes;
|
||||||
|
|
||||||
namespace Kyoo.Abstractions.Models;
|
namespace Kyoo.Abstractions.Models;
|
||||||
@ -24,8 +23,6 @@ namespace Kyoo.Abstractions.Models;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// A watch list item.
|
/// A watch list item.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[OneOf(Types = new[] { typeof(Show), typeof(Movie), typeof(Episode) })]
|
[OneOf(Types = new[] { typeof(Show), typeof(Movie) })]
|
||||||
public interface IWatchlist : IResource, IThumbnails, IMetadata, IAddedDate, IQuery
|
public interface IWatchlist : IResource, IThumbnails, IMetadata, IAddedDate
|
||||||
{
|
{ }
|
||||||
static Sort IQuery.DefaultSort => new Sort<IWatchlist>.By(nameof(AddedDate), true);
|
|
||||||
}
|
|
||||||
|
@ -31,7 +31,7 @@ namespace Kyoo.Abstractions.Models
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// A class to represent a single show's episode.
|
/// A class to represent a single show's episode.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Episode : IQuery, IResource, IMetadata, IThumbnails, IAddedDate, INews, IWatchlist
|
public class Episode : IQuery, IResource, IMetadata, IThumbnails, IAddedDate, INews
|
||||||
{
|
{
|
||||||
// Use absolute numbers by default and fallback to season/episodes if it does not exists.
|
// Use absolute numbers by default and fallback to season/episodes if it does not exists.
|
||||||
public static Sort DefaultSort => new Sort<Episode>.Conglomerate(
|
public static Sort DefaultSort => new Sort<Episode>.Conglomerate(
|
||||||
|
@ -30,7 +30,7 @@ public class Include
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The aditional fields to include in the result.
|
/// The aditional fields to include in the result.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ICollection<Metadata> Metadatas { get; init; } = ArraySegment<Metadata>.Empty;
|
public ICollection<Metadata> Metadatas { get; set; } = ArraySegment<Metadata>.Empty;
|
||||||
|
|
||||||
public abstract record Metadata(string Name);
|
public abstract record Metadata(string Name);
|
||||||
|
|
||||||
|
@ -62,7 +62,9 @@ public static class DapperHelper
|
|||||||
if (key == "kind")
|
if (key == "kind")
|
||||||
return "kind";
|
return "kind";
|
||||||
string[] keys = config
|
string[] keys = config
|
||||||
.Where(x => key == "id" || x.Value.GetProperty(key) != null)
|
.Where(x => !x.Key.StartsWith('_'))
|
||||||
|
// If first char is lower, assume manual sql instead of reflection.
|
||||||
|
.Where(x => char.IsLower(key.First()) || x.Value.GetProperty(key) != null)
|
||||||
.Select(x => $"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}")
|
.Select(x => $"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}")
|
||||||
.ToArray();
|
.ToArray();
|
||||||
if (keys.Length == 1)
|
if (keys.Length == 1)
|
||||||
@ -167,7 +169,9 @@ public static class DapperHelper
|
|||||||
}
|
}
|
||||||
|
|
||||||
IEnumerable<string> properties = config
|
IEnumerable<string> properties = config
|
||||||
.Where(x => key == "id" || x.Value.GetProperty(key) != null)
|
.Where(x => !x.Key.StartsWith('_'))
|
||||||
|
// If first char is lower, assume manual sql instead of reflection.
|
||||||
|
.Where(x => char.IsLower(key.First()) || x.Value.GetProperty(key) != null)
|
||||||
.Select(x => $"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}");
|
.Select(x => $"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}");
|
||||||
|
|
||||||
FormattableString ret = $"{properties.First():raw} {op}";
|
FormattableString ret = $"{properties.First():raw} {op}";
|
||||||
@ -204,7 +208,7 @@ public static class DapperHelper
|
|||||||
_ => throw new NotImplementedException(),
|
_ => throw new NotImplementedException(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return $"\nwhere {Process(filter)}";
|
return Process(filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ExpendProjections(Type type, string? prefix, Include include)
|
public static string ExpendProjections(Type type, string? prefix, Include include)
|
||||||
@ -235,8 +239,10 @@ public static class DapperHelper
|
|||||||
// Include handling
|
// Include handling
|
||||||
include ??= new();
|
include ??= new();
|
||||||
var (includeProjection, includeJoin, includeTypes, mapIncludes) = ProcessInclude(include, config);
|
var (includeProjection, includeJoin, includeTypes, mapIncludes) = ProcessInclude(include, config);
|
||||||
query.AppendLiteral(includeJoin);
|
query.Replace("/* includesJoin */", $"{includeJoin:raw}", out bool replaced);
|
||||||
query.Replace("/* includes */", $"{includeProjection:raw}", out bool replaced);
|
if (!replaced)
|
||||||
|
query.AppendLiteral(includeJoin);
|
||||||
|
query.Replace("/* includes */", $"{includeProjection:raw}", out replaced);
|
||||||
if (!replaced)
|
if (!replaced)
|
||||||
throw new ArgumentException("Missing '/* includes */' placeholder in top level sql select to support includes.");
|
throw new ArgumentException("Missing '/* includes */' placeholder in top level sql select to support includes.");
|
||||||
|
|
||||||
@ -248,7 +254,12 @@ public static class DapperHelper
|
|||||||
filter = Filter.And(filter, keysetFilter);
|
filter = Filter.And(filter, keysetFilter);
|
||||||
}
|
}
|
||||||
if (filter != null)
|
if (filter != null)
|
||||||
query += ProcessFilter(filter, config);
|
{
|
||||||
|
FormattableString filterSql = ProcessFilter(filter, config);
|
||||||
|
query.Replace("/* where */", $"and {filterSql}", out replaced);
|
||||||
|
if (!replaced)
|
||||||
|
query += $"\nwhere {filterSql}";
|
||||||
|
}
|
||||||
if (sort != null)
|
if (sort != null)
|
||||||
query += $"\norder by {ProcessSort(sort, limit?.Reverse ?? false, config):raw}";
|
query += $"\norder by {ProcessSort(sort, limit?.Reverse ?? false, config):raw}";
|
||||||
if (limit != null)
|
if (limit != null)
|
||||||
|
@ -76,8 +76,7 @@ public class WatchStatusRepository : IWatchStatusRepository
|
|||||||
protected FormattableString Sql => $"""
|
protected FormattableString Sql => $"""
|
||||||
select
|
select
|
||||||
s.*,
|
s.*,
|
||||||
m.*,
|
m.*
|
||||||
e.*
|
|
||||||
/* includes */
|
/* includes */
|
||||||
from (
|
from (
|
||||||
select
|
select
|
||||||
@ -99,42 +98,36 @@ public class WatchStatusRepository : IWatchStatusRepository
|
|||||||
movies as m
|
movies as m
|
||||||
inner join movie_watch_status as mw on mw.movie_id = m.id
|
inner join movie_watch_status as mw on mw.movie_id = m.id
|
||||||
and mw.user_id = [current_user]) as m on false
|
and mw.user_id = [current_user]) as m on false
|
||||||
full outer join (
|
/* includesJoin */
|
||||||
select
|
|
||||||
e.*, -- Episode as e
|
|
||||||
ew.*,
|
|
||||||
ew.added_date as order,
|
|
||||||
ew.status as watch_status
|
|
||||||
from
|
|
||||||
episodes as e
|
|
||||||
inner join episode_watch_status as ew on ew.episode_id = e.id
|
|
||||||
and ew.user_id = [current_user]) as e on false
|
|
||||||
where
|
where
|
||||||
coalesce(s.watch_status, m.watch_status, e.watch_status) = 'watching'::watch_status
|
(coalesce(s.watch_status, m.watch_status) = 'watching'::watch_status
|
||||||
or coalesce(s.watch_status, m.watch_status, e.watch_status) = 'completed'::watch_status
|
or coalesce(s.watch_status, m.watch_status) = 'completed'::watch_status)
|
||||||
|
/* where */
|
||||||
order by
|
order by
|
||||||
coalesce(s.order, m.order, e.order) desc,
|
coalesce(s.order, m.order) desc,
|
||||||
coalesce(s.id, m.id, e.id) asc
|
coalesce(s.id, m.id) asc
|
||||||
""";
|
""";
|
||||||
|
|
||||||
protected Dictionary<string, Type> Config => new()
|
protected Dictionary<string, Type> Config => new()
|
||||||
{
|
{
|
||||||
{ "s", typeof(Show) },
|
{ "s", typeof(Show) },
|
||||||
{ "sw", typeof(ShowWatchStatus) },
|
{ "_sw", typeof(ShowWatchStatus) },
|
||||||
{ "m", typeof(Movie) },
|
{ "m", typeof(Movie) },
|
||||||
{ "mw", typeof(MovieWatchStatus) },
|
{ "_mw", typeof(MovieWatchStatus) },
|
||||||
{ "e", typeof(Episode) },
|
|
||||||
{ "ew", typeof(EpisodeWatchStatus) },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
protected IWatchlist Mapper(List<object?> items)
|
protected IWatchlist Mapper(List<object?> items)
|
||||||
{
|
{
|
||||||
if (items[0] is Show show && show.Id != Guid.Empty)
|
if (items[0] is Show show && show.Id != Guid.Empty)
|
||||||
|
{
|
||||||
|
show.WatchStatus = items[1] as ShowWatchStatus;
|
||||||
return show;
|
return show;
|
||||||
if (items[1] is Movie movie && movie.Id != Guid.Empty)
|
}
|
||||||
|
if (items[2] is Movie movie && movie.Id != Guid.Empty)
|
||||||
|
{
|
||||||
|
movie.WatchStatus = items[3] as MovieWatchStatus;
|
||||||
return movie;
|
return movie;
|
||||||
if (items[2] is Episode episode && episode.Id != Guid.Empty)
|
}
|
||||||
return episode;
|
|
||||||
throw new InvalidDataException();
|
throw new InvalidDataException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,18 +154,39 @@ public class WatchStatusRepository : IWatchStatusRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task<ICollection<IWatchlist>> GetAll(
|
public async Task<ICollection<IWatchlist>> GetAll(
|
||||||
|
Filter<IWatchlist>? filter = default,
|
||||||
Include<IWatchlist>? include = default,
|
Include<IWatchlist>? include = default,
|
||||||
Pagination? limit = default)
|
Pagination? limit = default)
|
||||||
{
|
{
|
||||||
return _db.Query(
|
if (include != null)
|
||||||
|
include.Metadatas = include.Metadatas.Where(x => x.Name != nameof(Show.WatchStatus)).ToList();
|
||||||
|
|
||||||
|
// We can't use the generic after id hanler since the sort depends on a relation.
|
||||||
|
if (limit?.AfterID != null)
|
||||||
|
{
|
||||||
|
dynamic cursor = await Get(limit.AfterID.Value);
|
||||||
|
filter = Filter.And(
|
||||||
|
filter,
|
||||||
|
Filter.Or(
|
||||||
|
new Filter<IWatchlist>.Lt("order", cursor.WatchStatus.AddedDate),
|
||||||
|
Filter.And(
|
||||||
|
new Filter<IWatchlist>.Eq("order", cursor.WatchStatus.AddedDate),
|
||||||
|
new Filter<IWatchlist>.Gt("Id", cursor.Id)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
limit.AfterID = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await _db.Query(
|
||||||
Sql,
|
Sql,
|
||||||
Config,
|
Config,
|
||||||
Mapper,
|
Mapper,
|
||||||
(id) => Get(id),
|
(id) => Get(id),
|
||||||
_context,
|
_context,
|
||||||
include,
|
include,
|
||||||
null,
|
filter,
|
||||||
null,
|
null,
|
||||||
limit ?? new()
|
limit ?? new()
|
||||||
);
|
);
|
||||||
|
@ -51,6 +51,7 @@ namespace Kyoo.Core.Api
|
|||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Get all resources that match the given filter.
|
/// Get all resources that match the given filter.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
|
/// <param name="filter">Filter the returned items.</param>
|
||||||
/// <param name="pagination">How many items per page should be returned, where should the page start...</param>
|
/// <param name="pagination">How many items per page should be returned, where should the page start...</param>
|
||||||
/// <param name="fields">The aditional fields to include in the result.</param>
|
/// <param name="fields">The aditional fields to include in the result.</param>
|
||||||
/// <returns>A list of resources that match every filters.</returns>
|
/// <returns>A list of resources that match every filters.</returns>
|
||||||
@ -60,10 +61,12 @@ namespace Kyoo.Core.Api
|
|||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(RequestError))]
|
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(RequestError))]
|
||||||
public async Task<ActionResult<Page<IWatchlist>>> GetAll(
|
public async Task<ActionResult<Page<IWatchlist>>> GetAll(
|
||||||
|
[FromQuery] Filter<IWatchlist>? filter,
|
||||||
[FromQuery] Pagination pagination,
|
[FromQuery] Pagination pagination,
|
||||||
[FromQuery] Include<IWatchlist>? fields)
|
[FromQuery] Include<IWatchlist>? fields)
|
||||||
{
|
{
|
||||||
ICollection<IWatchlist> resources = await _repository.GetAll(
|
ICollection<IWatchlist> resources = await _repository.GetAll(
|
||||||
|
filter,
|
||||||
fields,
|
fields,
|
||||||
pagination
|
pagination
|
||||||
);
|
);
|
||||||
|
@ -91,6 +91,7 @@ namespace Kyoo.Postgresql
|
|||||||
SqlMapper.AddTypeHandler(typeof(List<Genre>), new ListTypeHandler<Genre>());
|
SqlMapper.AddTypeHandler(typeof(List<Genre>), new ListTypeHandler<Genre>());
|
||||||
SqlMapper.AddTypeHandler(typeof(Wrapper), new Wrapper.Handler());
|
SqlMapper.AddTypeHandler(typeof(Wrapper), new Wrapper.Handler());
|
||||||
InterpolatedSqlBuilderOptions.DefaultOptions.ReuseIdenticalParameters = true;
|
InterpolatedSqlBuilderOptions.DefaultOptions.ReuseIdenticalParameters = true;
|
||||||
|
InterpolatedSqlBuilderOptions.DefaultOptions.AutoFixSingleQuotes = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user