mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Add movie's watch status api
This commit is contained in:
parent
4f9c06c7bd
commit
2aa10c9b1f
@ -33,6 +33,16 @@ namespace Kyoo.Abstractions.Controllers
|
||||
/// </summary>
|
||||
IRepository<ILibraryItem> LibraryItems { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The repository that handle new items.
|
||||
/// </summary>
|
||||
IRepository<INews> News { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The repository that handle watched items.
|
||||
/// </summary>
|
||||
IWatchItemsRepository WatchItems { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The repository that handle collections.
|
||||
/// </summary>
|
||||
|
@ -0,0 +1,56 @@
|
||||
// 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.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Models;
|
||||
|
||||
namespace Kyoo.Abstractions.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// A local repository to handle watched items
|
||||
/// </summary>
|
||||
public interface IWatchItemsRepository : IBaseRepository
|
||||
{
|
||||
// /// <summary>
|
||||
// /// The event handler type for all events of this repository.
|
||||
// /// </summary>
|
||||
// /// <param name="resource">The resource created/modified/deleted</param>
|
||||
// /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
// public delegate Task ResourceEventHandler(T resource);
|
||||
|
||||
/// <summary>
|
||||
/// Get the watch status of a movie
|
||||
/// </summary>
|
||||
/// <param name="where">The movie selector.</param>
|
||||
/// <param name="userId">The id of the user.</param>
|
||||
/// <returns>The movie's status</returns>
|
||||
Task<MovieWatchStatus?> GetMovieStatus(Expression<Func<Movie, bool>> where, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Set the watch status of a movie
|
||||
/// </summary>
|
||||
/// <param name="movieId">The id of the movie.</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 movie's status</returns>
|
||||
Task<MovieWatchStatus> SetMovieStatus(int movieId, int userId, WatchStatus status, int? watchedTime);
|
||||
}
|
@ -31,6 +31,8 @@ namespace Kyoo.Core.Controllers
|
||||
|
||||
public LibraryManager(
|
||||
IRepository<ILibraryItem> libraryItemRepository,
|
||||
IRepository<INews> newsRepository,
|
||||
IWatchItemsRepository watchItemsRepository,
|
||||
IRepository<Collection> collectionRepository,
|
||||
IRepository<Movie> movieRepository,
|
||||
IRepository<Show> showRepository,
|
||||
@ -41,6 +43,8 @@ namespace Kyoo.Core.Controllers
|
||||
IRepository<User> userRepository)
|
||||
{
|
||||
LibraryItems = libraryItemRepository;
|
||||
News = newsRepository;
|
||||
WatchItems = watchItemsRepository;
|
||||
Collections = collectionRepository;
|
||||
Movies = movieRepository;
|
||||
Shows = showRepository;
|
||||
@ -53,6 +57,8 @@ namespace Kyoo.Core.Controllers
|
||||
_repositories = new IBaseRepository[]
|
||||
{
|
||||
LibraryItems,
|
||||
News,
|
||||
WatchItems,
|
||||
Collections,
|
||||
Movies,
|
||||
Shows,
|
||||
@ -67,6 +73,12 @@ namespace Kyoo.Core.Controllers
|
||||
/// <inheritdoc />
|
||||
public IRepository<ILibraryItem> LibraryItems { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IRepository<INews> News { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IWatchItemsRepository WatchItems { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IRepository<Collection> Collections { get; }
|
||||
|
||||
|
@ -0,0 +1,68 @@
|
||||
// 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;
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@
|
||||
<PackageReference Include="Blurhash.SkiaSharp" Version="2.0.0" />
|
||||
<PackageReference Include="Dapper" Version="2.1.24" />
|
||||
<PackageReference Include="InterpolatedSql.Dapper" Version="2.1.0" />
|
||||
<PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.12" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
|
@ -24,6 +24,7 @@ using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Abstractions.Models.Attributes;
|
||||
using Kyoo.Abstractions.Models.Permissions;
|
||||
using Kyoo.Abstractions.Models.Utils;
|
||||
using Kyoo.Authentication;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using static Kyoo.Abstractions.Models.Utils.Constants;
|
||||
@ -118,7 +119,7 @@ namespace Kyoo.Core.Api
|
||||
/// <remarks>
|
||||
/// List the collections that contain this show.
|
||||
/// </remarks>
|
||||
/// <param name="identifier">The ID or slug of the <see cref="Show"/>.</param>
|
||||
/// <param name="identifier">The ID or slug of the <see cref="Movie"/>.</param>
|
||||
/// <param name="sortBy">A key to sort collections by.</param>
|
||||
/// <param name="filter">An optional list of filters.</param>
|
||||
/// <param name="pagination">The number of collections to return.</param>
|
||||
@ -149,5 +150,64 @@ namespace Kyoo.Core.Api
|
||||
return NotFound();
|
||||
return Page(resources, pagination.Limit);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get watch status
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Get when an item has been wathed and if it was watched.
|
||||
/// </remarks>
|
||||
/// <param name="identifier">The ID or slug of the <see cref="Movie"/>.</param>
|
||||
/// <returns>The status.</returns>
|
||||
/// <response code="204">This movie does not have a specific status.</response>
|
||||
/// <response code="404">No movie with the given ID or slug could be found.</response>
|
||||
[HttpGet("{identifier:id}/watchStatus")]
|
||||
[HttpGet("{identifier:id}/watchStatus", Order = AlternativeRoute)]
|
||||
[PartialPermission(Kind.Read)]
|
||||
[UserOnly]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<MovieWatchStatus?> GetWatchStatus(Identifier identifier)
|
||||
{
|
||||
return await _libraryManager.WatchItems.GetMovieStatus(
|
||||
identifier.IsSame<Movie>(),
|
||||
User.GetId()!.Value
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set watch status
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Set when an item has been wathed and if it was watched.
|
||||
/// </remarks>
|
||||
/// <param name="identifier">The ID or slug of the <see cref="Movie"/>.</param>
|
||||
/// <param name="status">The new watch status.</param>
|
||||
/// <param name="watchedTime">Where the user stopped watching.</param>
|
||||
/// <returns>The newly set status.</returns>
|
||||
/// <response code="200">The status has been set</response>
|
||||
/// <response code="400">WatchedTime can't be specified if status is not watching.</response>
|
||||
/// <response code="404">No movie with the given ID or slug could be found.</response>
|
||||
[HttpGet("{identifier:id}/watchStatus")]
|
||||
[HttpGet("{identifier:id}/watchStatus", Order = AlternativeRoute)]
|
||||
[PartialPermission(Kind.Read)]
|
||||
[UserOnly]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<MovieWatchStatus> SetWatchStatus(Identifier identifier, WatchStatus status, int? watchedTime)
|
||||
{
|
||||
int id = await identifier.Match(
|
||||
id => Task.FromResult(id),
|
||||
async slug => (await _libraryManager.Movies.Get(slug)).Id
|
||||
);
|
||||
return await _libraryManager.WatchItems.SetMovieStatus(
|
||||
id,
|
||||
User.GetId()!.Value,
|
||||
status,
|
||||
watchedTime
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user