mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Create get all in watch status repository
This commit is contained in:
parent
7810f626c6
commit
c289161400
@ -17,15 +17,17 @@
|
||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Abstractions.Models.Utils;
|
||||
|
||||
namespace Kyoo.Abstractions.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// A local repository to handle watched items
|
||||
/// </summary>
|
||||
public interface IWatchStatusRepository : IRepository<IWatchlist>
|
||||
public interface IWatchStatusRepository
|
||||
{
|
||||
// /// <summary>
|
||||
// /// 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>
|
||||
// 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?> SetMovieStatus(Guid movieId, Guid userId, WatchStatus status, int? watchedTime);
|
||||
|
@ -27,5 +27,5 @@ namespace Kyoo.Abstractions.Models;
|
||||
[OneOf(Types = new[] { typeof(Show), typeof(Movie), typeof(Episode) })]
|
||||
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);
|
||||
}
|
||||
|
@ -36,7 +36,6 @@ using Kyoo.Abstractions.Models.Utils;
|
||||
using Kyoo.Authentication;
|
||||
using Kyoo.Utils;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Kyoo.Core.Controllers;
|
||||
|
||||
@ -354,7 +353,7 @@ public static class DapperHelper
|
||||
Filter<T>? filter)
|
||||
where T : class, IResource
|
||||
{
|
||||
InterpolatedSql.Dapper.SqlBuilders.SqlBuilder query = new(db, command);
|
||||
SqlBuilder query = new(db, command);
|
||||
|
||||
if (filter != null)
|
||||
query += ProcessFilter(filter, config);
|
||||
|
@ -25,13 +25,14 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Abstractions.Models.Exceptions;
|
||||
using Kyoo.Abstractions.Models.Utils;
|
||||
using Kyoo.Postgresql;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Kyoo.Core.Controllers;
|
||||
|
||||
public class WatchStatusRepository : DapperRepository<IWatchlist>, IWatchStatusRepository
|
||||
public class WatchStatusRepository : IWatchStatusRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// 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 IRepository<Episode> _episodes;
|
||||
private readonly IRepository<Movie> _movies;
|
||||
private readonly DbConnection _db;
|
||||
private readonly SqlVariableContext _context;
|
||||
|
||||
public WatchStatusRepository(DatabaseContext database,
|
||||
IRepository<Episode> episodes,
|
||||
IRepository<Movie> movies,
|
||||
DbConnection db,
|
||||
SqlVariableContext context)
|
||||
: base(db, context)
|
||||
{
|
||||
_database = database;
|
||||
_episodes = episodes;
|
||||
_movies = movies;
|
||||
_db = db;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// language=PostgreSQL
|
||||
protected override FormattableString Sql => $"""
|
||||
protected FormattableString Sql => $"""
|
||||
select
|
||||
s.*,
|
||||
m.*,
|
||||
@ -77,35 +81,53 @@ public class WatchStatusRepository : DapperRepository<IWatchlist>, IWatchStatusR
|
||||
/* includes */
|
||||
from (
|
||||
select
|
||||
s.* -- Show as s
|
||||
s.*, -- Show as s
|
||||
sw.*,
|
||||
sw.added_date as order,
|
||||
sw.status as watch_status
|
||||
from
|
||||
shows as s
|
||||
inner join show_watch_status as sw on sw.show_id = s.id
|
||||
and sw.user_id = [current_user]) as s
|
||||
full outer join (
|
||||
select
|
||||
m.* -- Movie as m
|
||||
m.*, -- Movie as m
|
||||
mw.*,
|
||||
mw.added_date as order,
|
||||
mw.status as watch_status
|
||||
from
|
||||
movies as m
|
||||
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 (
|
||||
select
|
||||
e.* -- Episode as e
|
||||
e.*, -- Episode as e
|
||||
ew.*,
|
||||
ew.added_date as order,
|
||||
ew.status as watch_status
|
||||
from
|
||||
episode as es
|
||||
episodes as e
|
||||
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) },
|
||||
{ "sw", typeof(ShowWatchStatus) },
|
||||
{ "m", typeof(Movie) },
|
||||
{ "mw", typeof(MovieWatchStatus) },
|
||||
{ "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)
|
||||
return show;
|
||||
@ -116,6 +138,46 @@ public class WatchStatusRepository : DapperRepository<IWatchlist>, IWatchStatusR
|
||||
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 />
|
||||
public Task<MovieWatchStatus?> GetMovieStatus(Guid movieId, Guid userId)
|
||||
{
|
||||
|
@ -51,8 +51,6 @@ namespace Kyoo.Core.Api
|
||||
/// <remarks>
|
||||
/// Get all resources that match the given filter.
|
||||
/// </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="fields">The aditional fields to include in the result.</param>
|
||||
/// <returns>A list of resources that match every filters.</returns>
|
||||
@ -62,14 +60,10 @@ namespace Kyoo.Core.Api
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(RequestError))]
|
||||
public async Task<ActionResult<Page<IWatchlist>>> GetAll(
|
||||
[FromQuery] Sort<IWatchlist> sortBy,
|
||||
[FromQuery] Filter<IWatchlist>? filter,
|
||||
[FromQuery] Pagination pagination,
|
||||
[FromQuery] Include<IWatchlist>? fields)
|
||||
{
|
||||
ICollection<IWatchlist> resources = await _repository.GetAll(
|
||||
filter,
|
||||
sortBy,
|
||||
fields,
|
||||
pagination
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user