mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Add episodes and shows watch status apis
This commit is contained in:
parent
2aa10c9b1f
commit
32050bcdcd
@ -41,7 +41,7 @@ namespace Kyoo.Abstractions.Controllers
|
||||
/// <summary>
|
||||
/// The repository that handle watched items.
|
||||
/// </summary>
|
||||
IWatchItemsRepository WatchItems { get; }
|
||||
IWatchStatusRepository WatchStatus { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The repository that handle collections.
|
||||
|
@ -88,10 +88,12 @@ namespace Kyoo.Abstractions.Controllers
|
||||
/// <param name="filter">A predicate to filter the resource.</param>
|
||||
/// <param name="include">The related fields to include.</param>
|
||||
/// <param name="sortBy">A custom sort method to handle cases where multiples items match the filters.</param>
|
||||
/// <param name="reverse">Reverse the sort.</param>
|
||||
/// <returns>The resource found</returns>
|
||||
Task<T?> GetOrDefault(Filter<T>? filter,
|
||||
Include<T>? include = default,
|
||||
Sort<T>? sortBy = default);
|
||||
Sort<T>? sortBy = default,
|
||||
bool reverse = false);
|
||||
|
||||
/// <summary>
|
||||
/// Search for resources with the database.
|
||||
|
@ -26,7 +26,7 @@ namespace Kyoo.Abstractions.Controllers;
|
||||
/// <summary>
|
||||
/// A local repository to handle watched items
|
||||
/// </summary>
|
||||
public interface IWatchItemsRepository : IBaseRepository
|
||||
public interface IWatchStatusRepository
|
||||
{
|
||||
// /// <summary>
|
||||
// /// The event handler type for all events of this repository.
|
||||
@ -53,4 +53,40 @@ public interface IWatchItemsRepository : IBaseRepository
|
||||
/// is <see cref="WatchStatus.Watching"/></param>
|
||||
/// <returns>The movie's status</returns>
|
||||
Task<MovieWatchStatus> SetMovieStatus(int movieId, int userId, WatchStatus status, int? watchedTime);
|
||||
|
||||
/// <summary>
|
||||
/// Get the watch status of a show.
|
||||
/// </summary>
|
||||
/// <param name="where">The show selector.</param>
|
||||
/// <param name="userId">The id of the user.</param>
|
||||
/// <returns>The show's status</returns>
|
||||
Task<ShowWatchStatus?> GetShowStatus(Expression<Func<Show, bool>> where, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Set the watch status of a show.
|
||||
/// </summary>
|
||||
/// <param name="showId">The id of the movie.</param>
|
||||
/// <param name="userId">The id of the user.</param>
|
||||
/// <param name="status">The new status.</param>
|
||||
/// <returns>The shows's status</returns>
|
||||
Task<ShowWatchStatus> SetShowStatus(int showId, int userId, WatchStatus status);
|
||||
|
||||
/// <summary>
|
||||
/// Get the watch status of an episode.
|
||||
/// </summary>
|
||||
/// <param name="where">The episode selector.</param>
|
||||
/// <param name="userId">The id of the user.</param>
|
||||
/// <returns>The episode's status</returns>
|
||||
Task<EpisodeWatchStatus?> GetEpisodeStatus(Expression<Func<Episode, bool>> where, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Set the watch status of an episode.
|
||||
/// </summary>
|
||||
/// <param name="episodeId">The id of the episode.</param>
|
||||
/// <param name="userId">The id of the user.</param>
|
||||
/// <param name="status">The new status.</param>
|
||||
/// <param name="watchedTime">Where the user has stopped watching. Only usable if Status
|
||||
/// is <see cref="WatchStatus.Watching"/></param>
|
||||
/// <returns>The episode's status</returns>
|
||||
Task<EpisodeWatchStatus> SetEpisodeStatus(int episodeId, int userId, WatchStatus status, int? watchedTime);
|
||||
}
|
@ -32,7 +32,7 @@ namespace Kyoo.Core.Controllers
|
||||
public LibraryManager(
|
||||
IRepository<ILibraryItem> libraryItemRepository,
|
||||
IRepository<INews> newsRepository,
|
||||
IWatchItemsRepository watchItemsRepository,
|
||||
IWatchStatusRepository watchStatusRepository,
|
||||
IRepository<Collection> collectionRepository,
|
||||
IRepository<Movie> movieRepository,
|
||||
IRepository<Show> showRepository,
|
||||
@ -44,7 +44,7 @@ namespace Kyoo.Core.Controllers
|
||||
{
|
||||
LibraryItems = libraryItemRepository;
|
||||
News = newsRepository;
|
||||
WatchItems = watchItemsRepository;
|
||||
WatchStatus = watchStatusRepository;
|
||||
Collections = collectionRepository;
|
||||
Movies = movieRepository;
|
||||
Shows = showRepository;
|
||||
@ -58,7 +58,6 @@ namespace Kyoo.Core.Controllers
|
||||
{
|
||||
LibraryItems,
|
||||
News,
|
||||
WatchItems,
|
||||
Collections,
|
||||
Movies,
|
||||
Shows,
|
||||
@ -77,7 +76,7 @@ namespace Kyoo.Core.Controllers
|
||||
public IRepository<INews> News { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IWatchItemsRepository WatchItems { get; }
|
||||
public IWatchStatusRepository WatchStatus { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IRepository<Collection> Collections { get; }
|
||||
|
@ -257,13 +257,16 @@ namespace Kyoo.Core.Controllers
|
||||
/// <inheritdoc />
|
||||
public virtual Task<T?> GetOrDefault(Filter<T>? filter,
|
||||
Include<T>? include = default,
|
||||
Sort<T>? sortBy = default)
|
||||
Sort<T>? sortBy = default,
|
||||
bool reverse = false)
|
||||
{
|
||||
return Sort(
|
||||
AddIncludes(Database.Set<T>(), include),
|
||||
sortBy
|
||||
)
|
||||
.FirstOrDefaultAsync(ParseFilter(filter));
|
||||
IQueryable<T> query = Sort(
|
||||
AddIncludes(Database.Set<T>(), include),
|
||||
sortBy
|
||||
);
|
||||
if (reverse)
|
||||
query = query.Reverse();
|
||||
return query.FirstOrDefaultAsync(ParseFilter(filter));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
@ -1,68 +0,0 @@
|
||||
// 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;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Postgresql;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Kyoo.Core.Controllers;
|
||||
|
||||
public class WatchItemsRepository : IWatchItemsRepository
|
||||
{
|
||||
private readonly DatabaseContext _database;
|
||||
|
||||
public WatchItemsRepository(DatabaseContext database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<MovieWatchStatus?> GetMovieStatus(Expression<Func<Movie, bool>> where, int userId)
|
||||
{
|
||||
return _database.MovieWatchInfo.FirstOrDefaultAsync(x =>
|
||||
x.Movie == _database.Movies.FirstOrDefault(where)
|
||||
&& x.UserId == userId
|
||||
);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<MovieWatchStatus> SetMovieStatus(
|
||||
int movieId,
|
||||
int userId,
|
||||
WatchStatus status,
|
||||
int? watchedTime)
|
||||
{
|
||||
if (watchedTime.HasValue && status != WatchStatus.Watching)
|
||||
throw new ValidationException("Can't have a watched time if the status is not watching.");
|
||||
MovieWatchStatus ret = new()
|
||||
{
|
||||
UserId = userId,
|
||||
MovieId = movieId,
|
||||
Status = status,
|
||||
WatchedTime = watchedTime,
|
||||
};
|
||||
await _database.MovieWatchInfo.Upsert(ret).RunAsync();
|
||||
return ret;
|
||||
}
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
// 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;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Postgresql;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Kyoo.Core.Controllers;
|
||||
|
||||
public class WatchStatusRepository : IWatchStatusRepository
|
||||
{
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IRepository<Episode> _episodes;
|
||||
|
||||
public WatchStatusRepository(DatabaseContext database, IRepository<Episode> episodes)
|
||||
{
|
||||
_database = database;
|
||||
_episodes = episodes;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<MovieWatchStatus?> GetMovieStatus(Expression<Func<Movie, bool>> where, int userId)
|
||||
{
|
||||
return _database.MovieWatchInfo.FirstOrDefaultAsync(x =>
|
||||
x.Movie == _database.Movies.FirstOrDefault(where)
|
||||
&& x.UserId == userId
|
||||
);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<MovieWatchStatus> SetMovieStatus(
|
||||
int movieId,
|
||||
int userId,
|
||||
WatchStatus status,
|
||||
int? watchedTime)
|
||||
{
|
||||
if (watchedTime.HasValue && status != WatchStatus.Watching)
|
||||
throw new ValidationException("Can't have a watched time if the status is not watching.");
|
||||
MovieWatchStatus ret = new()
|
||||
{
|
||||
UserId = userId,
|
||||
MovieId = movieId,
|
||||
Status = status,
|
||||
WatchedTime = watchedTime,
|
||||
};
|
||||
await _database.MovieWatchInfo.Upsert(ret)
|
||||
.UpdateIf(x => !(status == WatchStatus.Watching && x.Status == WatchStatus.Completed))
|
||||
.RunAsync();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<ShowWatchStatus?> GetShowStatus(Expression<Func<Show, bool>> where, int userId)
|
||||
{
|
||||
return _database.ShowWatchInfo.FirstOrDefaultAsync(x =>
|
||||
x.Show == _database.Shows.FirstOrDefault(where)
|
||||
&& x.UserId == userId
|
||||
);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ShowWatchStatus> SetShowStatus(
|
||||
int showId,
|
||||
int userId,
|
||||
WatchStatus status)
|
||||
{
|
||||
ShowWatchStatus ret = new()
|
||||
{
|
||||
UserId = userId,
|
||||
ShowId = showId,
|
||||
Status = status,
|
||||
NextEpisode = status == WatchStatus.Watching
|
||||
? await _episodes.GetOrDefault(
|
||||
where: x => x.ShowId == showId
|
||||
&& (x.WatchStatus!.Status == WatchStatus.Watching
|
||||
|| x.WatchStatus.Status == WatchStatus.Completed),
|
||||
reverse: true
|
||||
)
|
||||
: null,
|
||||
};
|
||||
await _database.ShowWatchInfo.Upsert(ret)
|
||||
.UpdateIf(x => !(status == WatchStatus.Watching && x.Status == WatchStatus.Completed))
|
||||
.RunAsync();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<EpisodeWatchStatus?> GetEpisodeStatus(Expression<Func<Episode, bool>> where, int userId)
|
||||
{
|
||||
return _database.EpisodeWatchInfo.FirstOrDefaultAsync(x =>
|
||||
x.Episode == _database.Episodes.FirstOrDefault(where)
|
||||
&& x.UserId == userId
|
||||
);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<EpisodeWatchStatus> SetEpisodeStatus(
|
||||
int episodeId,
|
||||
int userId,
|
||||
WatchStatus status,
|
||||
int? watchedTime)
|
||||
{
|
||||
Episode episode = await _episodes.Get(episodeId);
|
||||
if (watchedTime.HasValue && status != WatchStatus.Watching)
|
||||
throw new ValidationException("Can't have a watched time if the status is not watching.");
|
||||
EpisodeWatchStatus ret = new()
|
||||
{
|
||||
UserId = userId,
|
||||
EpisodeId = episodeId,
|
||||
Status = status,
|
||||
WatchedTime = watchedTime,
|
||||
};
|
||||
await _database.EpisodeWatchInfo.Upsert(ret).RunAsync();
|
||||
await SetShowStatus(episode.ShowId, userId, WatchStatus.Watching);
|
||||
return ret;
|
||||
}
|
||||
}
|
@ -170,7 +170,7 @@ namespace Kyoo.Core.Api
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<MovieWatchStatus?> GetWatchStatus(Identifier identifier)
|
||||
{
|
||||
return await _libraryManager.WatchItems.GetMovieStatus(
|
||||
return await _libraryManager.WatchStatus.GetMovieStatus(
|
||||
identifier.IsSame<Movie>(),
|
||||
User.GetId()!.Value
|
||||
);
|
||||
@ -202,7 +202,7 @@ namespace Kyoo.Core.Api
|
||||
id => Task.FromResult(id),
|
||||
async slug => (await _libraryManager.Movies.Get(slug)).Id
|
||||
);
|
||||
return await _libraryManager.WatchItems.SetMovieStatus(
|
||||
return await _libraryManager.WatchStatus.SetMovieStatus(
|
||||
id,
|
||||
User.GetId()!.Value,
|
||||
status,
|
||||
|
Loading…
x
Reference in New Issue
Block a user