Add watchlist api

This commit is contained in:
Zoe Roux 2023-12-05 23:53:27 +01:00
parent 0294f538eb
commit 7810f626c6
9 changed files with 171 additions and 10 deletions

View File

@ -17,7 +17,6 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Kyoo.Abstractions.Models;
@ -26,7 +25,7 @@ namespace Kyoo.Abstractions.Controllers;
/// <summary>
/// A local repository to handle watched items
/// </summary>
public interface IWatchStatusRepository
public interface IWatchStatusRepository : IRepository<IWatchlist>
{
// /// <summary>
// /// The event handler type for all events of this repository.

View File

@ -0,0 +1,31 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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;
/// <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<INews>.By(nameof(AddedDate), true);
}

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
public class Episode : IQuery, IResource, IMetadata, IThumbnails, IAddedDate, INews, IWatchlist
{
// 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

@ -31,7 +31,7 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// A series or a movie.
/// </summary>
public class Movie : IQuery, IResource, IMetadata, IOnMerge, IThumbnails, IAddedDate, ILibraryItem, INews
public class Movie : IQuery, IResource, IMetadata, IOnMerge, IThumbnails, IAddedDate, ILibraryItem, INews, IWatchlist
{
public static Sort DefaultSort => new Sort<Movie>.By(x => x.Name);

View File

@ -32,7 +32,7 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// A series or a movie.
/// </summary>
public class Show : IQuery, IResource, IMetadata, IOnMerge, IThumbnails, IAddedDate, ILibraryItem
public class Show : IQuery, IResource, IMetadata, IOnMerge, IThumbnails, IAddedDate, ILibraryItem, IWatchlist
{
public static Sort DefaultSort => new Sort<Show>.By(x => x.Name);

View File

@ -17,9 +17,6 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using EntityFrameworkCore.Projectables;
using Kyoo.Abstractions.Models.Attributes;
namespace Kyoo.Abstractions.Models

View File

@ -17,7 +17,10 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
@ -28,7 +31,7 @@ using Microsoft.EntityFrameworkCore;
namespace Kyoo.Core.Controllers;
public class WatchStatusRepository : IWatchStatusRepository
public class WatchStatusRepository : DapperRepository<IWatchlist>, IWatchStatusRepository
{
/// <summary>
/// If the watch percent is below this value, don't consider the item started.
@ -55,13 +58,64 @@ public class WatchStatusRepository : IWatchStatusRepository
public WatchStatusRepository(DatabaseContext database,
IRepository<Episode> episodes,
IRepository<Movie> movies)
IRepository<Movie> movies,
DbConnection db,
SqlVariableContext context)
: base(db, context)
{
_database = database;
_episodes = episodes;
_movies = movies;
}
// language=PostgreSQL
protected override FormattableString Sql => $"""
select
s.*,
m.*,
e.*
/* includes */
from (
select
s.* -- Show as s
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
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
full outer join (
select
e.* -- Episode as e
from
episode as es
inner join episode_watch_status as ew on ew.episode_id = e.id
and ew.user_id = [current_user])) as e
""";
protected override Dictionary<string, Type> Config => new()
{
{ "s", typeof(Show) },
{ "m", typeof(Movie) },
{ "e", typeof(Episode) },
};
protected override IWatchlist Mapper(List<object?> items)
{
if (items[0] is Show show && show.Id != Guid.Empty)
return show;
if (items[1] is Movie movie && movie.Id != Guid.Empty)
return movie;
if (items[2] is Episode episode && episode.Id != Guid.Empty)
return episode;
throw new InvalidDataException();
}
/// <inheritdoc />
public Task<MovieWatchStatus?> GetMovieStatus(Guid movieId, Guid userId)
{

View File

@ -0,0 +1,80 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System.Collections.Generic;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Abstractions.Models.Permissions;
using Kyoo.Abstractions.Models.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Kyoo.Abstractions.Models.Utils.Constants;
namespace Kyoo.Core.Api
{
/// <summary>
/// List new items added to kyoo.
/// </summary>
[Route("watchlist")]
[ApiController]
[PartialPermission("LibraryItem")]
[ApiDefinition("News", Group = ResourcesGroup)]
public class WatchlistApi : BaseApi
{
private readonly IWatchStatusRepository _repository;
public WatchlistApi(IWatchStatusRepository repository)
{
_repository = repository;
}
/// <summary>
/// Get all
/// </summary>
/// <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>
/// <response code="400">Invalid filters or sort information.</response>
[HttpGet]
[PartialPermission(Kind.Read)]
[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
);
return Page(resources, pagination.Limit);
}
}
}