Add watchlist filters and fix after id

This commit is contained in:
Zoe Roux 2023-12-06 14:04:41 +01:00
parent c289161400
commit bab97fba5f
8 changed files with 67 additions and 40 deletions

View File

@ -37,6 +37,7 @@ public interface IWatchStatusRepository
// public delegate Task ResourceEventHandler(T resource);
Task<ICollection<IWatchlist>> GetAll(
Filter<IWatchlist>? filter = default,
Include<IWatchlist>? include = default,
Pagination? limit = default);

View File

@ -16,7 +16,6 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models.Attributes;
namespace Kyoo.Abstractions.Models;
@ -24,8 +23,6 @@ namespace Kyoo.Abstractions.Models;
/// <summary>
/// A watch list item.
/// </summary>
[OneOf(Types = new[] { typeof(Show), typeof(Movie), typeof(Episode) })]
public interface IWatchlist : IResource, IThumbnails, IMetadata, IAddedDate, IQuery
{
static Sort IQuery.DefaultSort => new Sort<IWatchlist>.By(nameof(AddedDate), true);
}
[OneOf(Types = new[] { typeof(Show), typeof(Movie) })]
public interface IWatchlist : IResource, IThumbnails, IMetadata, IAddedDate
{ }

View File

@ -31,7 +31,7 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// A class to represent a single show's episode.
/// </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.
public static Sort DefaultSort => new Sort<Episode>.Conglomerate(

View File

@ -30,7 +30,7 @@ public class Include
/// <summary>
/// The aditional fields to include in the result.
/// </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);

View File

@ -62,7 +62,9 @@ public static class DapperHelper
if (key == "kind")
return "kind";
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()}")
.ToArray();
if (keys.Length == 1)
@ -167,7 +169,9 @@ public static class DapperHelper
}
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()}");
FormattableString ret = $"{properties.First():raw} {op}";
@ -204,7 +208,7 @@ public static class DapperHelper
_ => throw new NotImplementedException(),
};
}
return $"\nwhere {Process(filter)}";
return Process(filter);
}
public static string ExpendProjections(Type type, string? prefix, Include include)
@ -235,8 +239,10 @@ public static class DapperHelper
// Include handling
include ??= new();
var (includeProjection, includeJoin, includeTypes, mapIncludes) = ProcessInclude(include, config);
query.AppendLiteral(includeJoin);
query.Replace("/* includes */", $"{includeProjection:raw}", out bool replaced);
query.Replace("/* includesJoin */", $"{includeJoin:raw}", out bool replaced);
if (!replaced)
query.AppendLiteral(includeJoin);
query.Replace("/* includes */", $"{includeProjection:raw}", out replaced);
if (!replaced)
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);
}
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)
query += $"\norder by {ProcessSort(sort, limit?.Reverse ?? false, config):raw}";
if (limit != null)

View File

@ -76,8 +76,7 @@ public class WatchStatusRepository : IWatchStatusRepository
protected FormattableString Sql => $"""
select
s.*,
m.*,
e.*
m.*
/* includes */
from (
select
@ -99,42 +98,36 @@ public class WatchStatusRepository : IWatchStatusRepository
movies as m
inner join movie_watch_status as mw on mw.movie_id = m.id
and mw.user_id = [current_user]) as m on false
full outer join (
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
/* includesJoin */
where
coalesce(s.watch_status, m.watch_status, e.watch_status) = 'watching'::watch_status
or coalesce(s.watch_status, m.watch_status, e.watch_status) = 'completed'::watch_status
(coalesce(s.watch_status, m.watch_status) = 'watching'::watch_status
or coalesce(s.watch_status, m.watch_status) = 'completed'::watch_status)
/* where */
order by
coalesce(s.order, m.order, e.order) desc,
coalesce(s.id, m.id, e.id) asc
coalesce(s.order, m.order) desc,
coalesce(s.id, m.id) asc
""";
protected Dictionary<string, Type> Config => new()
{
{ "s", typeof(Show) },
{ "sw", typeof(ShowWatchStatus) },
{ "_sw", typeof(ShowWatchStatus) },
{ "m", typeof(Movie) },
{ "mw", typeof(MovieWatchStatus) },
{ "e", typeof(Episode) },
{ "ew", typeof(EpisodeWatchStatus) },
{ "_mw", typeof(MovieWatchStatus) },
};
protected IWatchlist Mapper(List<object?> items)
{
if (items[0] is Show show && show.Id != Guid.Empty)
{
show.WatchStatus = items[1] as ShowWatchStatus;
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;
if (items[2] is Episode episode && episode.Id != Guid.Empty)
return episode;
}
throw new InvalidDataException();
}
@ -161,18 +154,39 @@ public class WatchStatusRepository : IWatchStatusRepository
}
/// <inheritdoc />
public Task<ICollection<IWatchlist>> GetAll(
public async Task<ICollection<IWatchlist>> GetAll(
Filter<IWatchlist>? filter = default,
Include<IWatchlist>? include = 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,
Config,
Mapper,
(id) => Get(id),
_context,
include,
null,
filter,
null,
limit ?? new()
);

View File

@ -51,6 +51,7 @@ namespace Kyoo.Core.Api
/// <remarks>
/// Get all resources that match the given filter.
/// </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="fields">The aditional fields to include in the result.</param>
/// <returns>A list of resources that match every filters.</returns>
@ -60,10 +61,12 @@ namespace Kyoo.Core.Api
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(RequestError))]
public async Task<ActionResult<Page<IWatchlist>>> GetAll(
[FromQuery] Filter<IWatchlist>? filter,
[FromQuery] Pagination pagination,
[FromQuery] Include<IWatchlist>? fields)
{
ICollection<IWatchlist> resources = await _repository.GetAll(
filter,
fields,
pagination
);

View File

@ -91,6 +91,7 @@ namespace Kyoo.Postgresql
SqlMapper.AddTypeHandler(typeof(List<Genre>), new ListTypeHandler<Genre>());
SqlMapper.AddTypeHandler(typeof(Wrapper), new Wrapper.Handler());
InterpolatedSqlBuilderOptions.DefaultOptions.ReuseIdenticalParameters = true;
InterpolatedSqlBuilderOptions.DefaultOptions.AutoFixSingleQuotes = false;
}
/// <inheritdoc />