using System; using System.Collections.Generic; using System.Linq; 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.AspNetCore.Mvc; using Microsoft.Extensions.Options; namespace Kyoo.Core.Api { [Route("api/item")] [Route("api/items")] [ApiController] [ResourceView] public class LibraryItemApi : ControllerBase { private readonly ILibraryItemRepository _libraryItems; private readonly Uri _baseURL; public LibraryItemApi(ILibraryItemRepository libraryItems, IOptions options) { _libraryItems = libraryItems; _baseURL = options.Value.PublicUrl; } [HttpGet] [Permission(nameof(LibraryItemApi), Kind.Read)] public async Task>> GetAll([FromQuery] string sortBy, [FromQuery] int afterID, [FromQuery] Dictionary where, [FromQuery] int limit = 50) { try { ICollection resources = await _libraryItems.GetAll( ApiHelper.ParseWhere(where), new Sort(sortBy), new Pagination(limit, afterID)); return new Page(resources, new Uri(_baseURL, Request.Path), Request.Query.ToDictionary(x => x.Key, x => x.Value.ToString(), StringComparer.InvariantCultureIgnoreCase), limit); } catch (ItemNotFoundException) { return NotFound(); } catch (ArgumentException ex) { return BadRequest(new { Error = ex.Message }); } } } }