Create get all in watch status repository

This commit is contained in:
Zoe Roux 2023-12-06 11:52:50 +01:00
parent 7810f626c6
commit c289161400
5 changed files with 82 additions and 21 deletions

View File

@ -17,15 +17,17 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Abstractions.Models; using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Utils;
namespace Kyoo.Abstractions.Controllers; namespace Kyoo.Abstractions.Controllers;
/// <summary> /// <summary>
/// A local repository to handle watched items /// A local repository to handle watched items
/// </summary> /// </summary>
public interface IWatchStatusRepository : IRepository<IWatchlist> public interface IWatchStatusRepository
{ {
// /// <summary> // /// <summary>
// /// The event handler type for all events of this repository. // /// The event handler type for all events of this repository.
@ -34,6 +36,10 @@ public interface IWatchStatusRepository : IRepository<IWatchlist>
// /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> // /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
// public delegate Task ResourceEventHandler(T resource); // public delegate Task ResourceEventHandler(T resource);
Task<ICollection<IWatchlist>> GetAll(
Include<IWatchlist>? include = default,
Pagination? limit = default);
Task<MovieWatchStatus?> GetMovieStatus(Guid movieId, Guid userId); Task<MovieWatchStatus?> GetMovieStatus(Guid movieId, Guid userId);
Task<MovieWatchStatus?> SetMovieStatus(Guid movieId, Guid userId, WatchStatus status, int? watchedTime); Task<MovieWatchStatus?> SetMovieStatus(Guid movieId, Guid userId, WatchStatus status, int? watchedTime);

View File

@ -27,5 +27,5 @@ namespace Kyoo.Abstractions.Models;
[OneOf(Types = new[] { typeof(Show), typeof(Movie), typeof(Episode) })] [OneOf(Types = new[] { typeof(Show), typeof(Movie), typeof(Episode) })]
public interface IWatchlist : IResource, IThumbnails, IMetadata, IAddedDate, IQuery public interface IWatchlist : IResource, IThumbnails, IMetadata, IAddedDate, IQuery
{ {
static Sort IQuery.DefaultSort => new Sort<INews>.By(nameof(AddedDate), true); static Sort IQuery.DefaultSort => new Sort<IWatchlist>.By(nameof(AddedDate), true);
} }

View File

@ -36,7 +36,6 @@ using Kyoo.Abstractions.Models.Utils;
using Kyoo.Authentication; using Kyoo.Authentication;
using Kyoo.Utils; using Kyoo.Utils;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace Kyoo.Core.Controllers; namespace Kyoo.Core.Controllers;
@ -354,7 +353,7 @@ public static class DapperHelper
Filter<T>? filter) Filter<T>? filter)
where T : class, IResource where T : class, IResource
{ {
InterpolatedSql.Dapper.SqlBuilders.SqlBuilder query = new(db, command); SqlBuilder query = new(db, command);
if (filter != null) if (filter != null)
query += ProcessFilter(filter, config); query += ProcessFilter(filter, config);

View File

@ -25,13 +25,14 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models; using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Exceptions;
using Kyoo.Abstractions.Models.Utils; using Kyoo.Abstractions.Models.Utils;
using Kyoo.Postgresql; using Kyoo.Postgresql;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Kyoo.Core.Controllers; namespace Kyoo.Core.Controllers;
public class WatchStatusRepository : DapperRepository<IWatchlist>, IWatchStatusRepository public class WatchStatusRepository : IWatchStatusRepository
{ {
/// <summary> /// <summary>
/// If the watch percent is below this value, don't consider the item started. /// If the watch percent is below this value, don't consider the item started.
@ -55,21 +56,24 @@ public class WatchStatusRepository : DapperRepository<IWatchlist>, IWatchStatusR
private readonly DatabaseContext _database; private readonly DatabaseContext _database;
private readonly IRepository<Episode> _episodes; private readonly IRepository<Episode> _episodes;
private readonly IRepository<Movie> _movies; private readonly IRepository<Movie> _movies;
private readonly DbConnection _db;
private readonly SqlVariableContext _context;
public WatchStatusRepository(DatabaseContext database, public WatchStatusRepository(DatabaseContext database,
IRepository<Episode> episodes, IRepository<Episode> episodes,
IRepository<Movie> movies, IRepository<Movie> movies,
DbConnection db, DbConnection db,
SqlVariableContext context) SqlVariableContext context)
: base(db, context)
{ {
_database = database; _database = database;
_episodes = episodes; _episodes = episodes;
_movies = movies; _movies = movies;
_db = db;
_context = context;
} }
// language=PostgreSQL // language=PostgreSQL
protected override FormattableString Sql => $""" protected FormattableString Sql => $"""
select select
s.*, s.*,
m.*, m.*,
@ -77,35 +81,53 @@ public class WatchStatusRepository : DapperRepository<IWatchlist>, IWatchStatusR
/* includes */ /* includes */
from ( from (
select select
s.* -- Show as s s.*, -- Show as s
sw.*,
sw.added_date as order,
sw.status as watch_status
from from
shows as s shows as s
inner join show_watch_status as sw on sw.show_id = s.id inner join show_watch_status as sw on sw.show_id = s.id
and sw.user_id = [current_user]) as s and sw.user_id = [current_user]) as s
full outer join ( full outer join (
select select
m.* -- Movie as m m.*, -- Movie as m
mw.*,
mw.added_date as order,
mw.status as watch_status
from from
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 s) as m and mw.user_id = [current_user]) as m on false
full outer join ( full outer join (
select select
e.* -- Episode as e e.*, -- Episode as e
ew.*,
ew.added_date as order,
ew.status as watch_status
from from
episode as es episodes as e
inner join episode_watch_status as ew on ew.episode_id = e.id inner join episode_watch_status as ew on ew.episode_id = e.id
and ew.user_id = [current_user])) as e and ew.user_id = [current_user]) as e on false
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
order by
coalesce(s.order, m.order, e.order) desc,
coalesce(s.id, m.id, e.id) asc
"""; """;
protected override Dictionary<string, Type> Config => new() protected Dictionary<string, Type> Config => new()
{ {
{ "s", typeof(Show) }, { "s", typeof(Show) },
{ "sw", typeof(ShowWatchStatus) },
{ "m", typeof(Movie) }, { "m", typeof(Movie) },
{ "mw", typeof(MovieWatchStatus) },
{ "e", typeof(Episode) }, { "e", typeof(Episode) },
{ "ew", typeof(EpisodeWatchStatus) },
}; };
protected override 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)
return show; return show;
@ -116,6 +138,46 @@ public class WatchStatusRepository : DapperRepository<IWatchlist>, IWatchStatusR
throw new InvalidDataException(); throw new InvalidDataException();
} }
/// <inheritdoc/>
public virtual async Task<IWatchlist> Get(Guid id, Include<IWatchlist>? include = default)
{
IWatchlist? ret = await GetOrDefault(id, include);
if (ret == null)
throw new ItemNotFoundException($"No {nameof(IWatchlist)} found with the id {id}");
return ret;
}
/// <inheritdoc />
public Task<IWatchlist?> GetOrDefault(Guid id, Include<IWatchlist>? include = null)
{
return _db.QuerySingle<IWatchlist>(
Sql,
Config,
Mapper,
_context,
include,
new Filter<IWatchlist>.Eq(nameof(IResource.Id), id)
);
}
/// <inheritdoc />
public Task<ICollection<IWatchlist>> GetAll(
Include<IWatchlist>? include = default,
Pagination? limit = default)
{
return _db.Query(
Sql,
Config,
Mapper,
(id) => Get(id),
_context,
include,
null,
null,
limit ?? new()
);
}
/// <inheritdoc /> /// <inheritdoc />
public Task<MovieWatchStatus?> GetMovieStatus(Guid movieId, Guid userId) public Task<MovieWatchStatus?> GetMovieStatus(Guid movieId, Guid userId)
{ {

View File

@ -51,8 +51,6 @@ 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="sortBy">Sort information about the query (sort by, sort order).</param>
/// <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>
@ -62,14 +60,10 @@ 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] Sort<IWatchlist> sortBy,
[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,
sortBy,
fields, fields,
pagination pagination
); );