mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 12:14:46 -04:00
183 lines
5.1 KiB
C#
183 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Threading.Tasks;
|
|
using Kyoo.Abstractions.Controllers;
|
|
using Kyoo.Abstractions.Models;
|
|
using Kyoo.Abstractions.Models.Exceptions;
|
|
using Kyoo.Abstractions.Models.Permissions;
|
|
using Kyoo.Core.Models.Options;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Kyoo.Core.Api
|
|
{
|
|
[Route("api/collection")]
|
|
[Route("api/collections")]
|
|
[ApiController]
|
|
[PartialPermission(nameof(CollectionApi))]
|
|
public class CollectionApi : CrudApi<Collection>
|
|
{
|
|
private readonly ILibraryManager _libraryManager;
|
|
private readonly IFileSystem _files;
|
|
private readonly IThumbnailsManager _thumbs;
|
|
|
|
public CollectionApi(ILibraryManager libraryManager,
|
|
IFileSystem files,
|
|
IThumbnailsManager thumbs,
|
|
IOptions<BasicOptions> options)
|
|
: base(libraryManager.CollectionRepository, options.Value.PublicUrl)
|
|
{
|
|
_libraryManager = libraryManager;
|
|
_files = files;
|
|
_thumbs = thumbs;
|
|
}
|
|
|
|
[HttpGet("{id:int}/show")]
|
|
[HttpGet("{id:int}/shows")]
|
|
[PartialPermission(Kind.Read)]
|
|
public async Task<ActionResult<Page<Show>>> GetShows(int id,
|
|
[FromQuery] string sortBy,
|
|
[FromQuery] int afterID,
|
|
[FromQuery] Dictionary<string, string> where,
|
|
[FromQuery] int limit = 30)
|
|
{
|
|
try
|
|
{
|
|
ICollection<Show> resources = await _libraryManager.GetAll(
|
|
ApiHelper.ParseWhere<Show>(where, x => x.Collections.Any(y => y.ID == id)),
|
|
new Sort<Show>(sortBy),
|
|
new Pagination(limit, afterID));
|
|
|
|
if (!resources.Any() && await _libraryManager.GetOrDefault<Collection>(id) == null)
|
|
return NotFound();
|
|
return Page(resources, limit);
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
return BadRequest(new { Error = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet("{slug}/show")]
|
|
[HttpGet("{slug}/shows")]
|
|
[PartialPermission(Kind.Read)]
|
|
public async Task<ActionResult<Page<Show>>> GetShows(string slug,
|
|
[FromQuery] string sortBy,
|
|
[FromQuery] int afterID,
|
|
[FromQuery] Dictionary<string, string> where,
|
|
[FromQuery] int limit = 30)
|
|
{
|
|
try
|
|
{
|
|
ICollection<Show> resources = await _libraryManager.GetAll(
|
|
ApiHelper.ParseWhere<Show>(where, x => x.Collections.Any(y => y.Slug == slug)),
|
|
new Sort<Show>(sortBy),
|
|
new Pagination(limit, afterID));
|
|
|
|
if (!resources.Any() && await _libraryManager.GetOrDefault<Collection>(slug) == null)
|
|
return NotFound();
|
|
return Page(resources, limit);
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
return BadRequest(new { Error = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet("{id:int}/library")]
|
|
[HttpGet("{id:int}/libraries")]
|
|
[PartialPermission(Kind.Read)]
|
|
public async Task<ActionResult<Page<Library>>> GetLibraries(int id,
|
|
[FromQuery] string sortBy,
|
|
[FromQuery] int afterID,
|
|
[FromQuery] Dictionary<string, string> where,
|
|
[FromQuery] int limit = 30)
|
|
{
|
|
try
|
|
{
|
|
ICollection<Library> resources = await _libraryManager.GetAll(
|
|
ApiHelper.ParseWhere<Library>(where, x => x.Collections.Any(y => y.ID == id)),
|
|
new Sort<Library>(sortBy),
|
|
new Pagination(limit, afterID));
|
|
|
|
if (!resources.Any() && await _libraryManager.GetOrDefault<Collection>(id) == null)
|
|
return NotFound();
|
|
return Page(resources, limit);
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
return BadRequest(new { Error = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet("{slug}/library")]
|
|
[HttpGet("{slug}/libraries")]
|
|
[PartialPermission(Kind.Read)]
|
|
public async Task<ActionResult<Page<Library>>> GetLibraries(string slug,
|
|
[FromQuery] string sortBy,
|
|
[FromQuery] int afterID,
|
|
[FromQuery] Dictionary<string, string> where,
|
|
[FromQuery] int limit = 30)
|
|
{
|
|
try
|
|
{
|
|
ICollection<Library> resources = await _libraryManager.GetAll(
|
|
ApiHelper.ParseWhere<Library>(where, x => x.Collections.Any(y => y.Slug == slug)),
|
|
new Sort<Library>(sortBy),
|
|
new Pagination(limit, afterID));
|
|
|
|
if (!resources.Any() && await _libraryManager.GetOrDefault<Collection>(slug) == null)
|
|
return NotFound();
|
|
return Page(resources, limit);
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
return BadRequest(new { Error = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet("{slug}/poster")]
|
|
public async Task<IActionResult> GetPoster(string slug)
|
|
{
|
|
try
|
|
{
|
|
Collection collection = await _libraryManager.Get<Collection>(slug);
|
|
return _files.FileResult(await _thumbs.GetImagePath(collection, Images.Poster));
|
|
}
|
|
catch (ItemNotFoundException)
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpGet("{slug}/logo")]
|
|
public async Task<IActionResult> GetLogo(string slug)
|
|
{
|
|
try
|
|
{
|
|
Collection collection = await _libraryManager.Get<Collection>(slug);
|
|
return _files.FileResult(await _thumbs.GetImagePath(collection, Images.Logo));
|
|
}
|
|
catch (ItemNotFoundException)
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpGet("{slug}/backdrop")]
|
|
[HttpGet("{slug}/thumbnail")]
|
|
public async Task<IActionResult> GetBackdrop(string slug)
|
|
{
|
|
try
|
|
{
|
|
Collection collection = await _libraryManager.Get<Collection>(slug);
|
|
return _files.FileResult(await _thumbs.GetImagePath(collection, Images.Thumbnail));
|
|
}
|
|
catch (ItemNotFoundException)
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
}
|
|
} |