From 24355bac84826a7b06be7c7fcf23d1f7edf7aad8 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 29 Jul 2020 00:46:24 +0200 Subject: [PATCH] Adding the /items route --- Kyoo/Startup.cs | 6 +-- Kyoo/Views/API/LibraryItemsApi.cs | 63 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 Kyoo/Views/API/LibraryItemsApi.cs diff --git a/Kyoo/Startup.cs b/Kyoo/Startup.cs index 5e695133..d6ff4004 100644 --- a/Kyoo/Startup.cs +++ b/Kyoo/Startup.cs @@ -46,9 +46,9 @@ namespace Kyoo services.AddDbContext(options => { options.UseLazyLoadingProxies() - .UseNpgsql(_configuration.GetConnectionString("Database")) - .EnableSensitiveDataLogging() - .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole())); + .UseNpgsql(_configuration.GetConnectionString("Database")); + // .EnableSensitiveDataLogging() + // .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole())); }, ServiceLifetime.Transient); services.AddDbContext(options => diff --git a/Kyoo/Views/API/LibraryItemsApi.cs b/Kyoo/Views/API/LibraryItemsApi.cs new file mode 100644 index 00000000..9a32dade --- /dev/null +++ b/Kyoo/Views/API/LibraryItemsApi.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Kyoo.CommonApi; +using Kyoo.Controllers; +using Kyoo.Models; +using Kyoo.Models.Exceptions; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; + +namespace Kyoo.Api +{ + [Route("api/item")] + [Route("api/items")] + [ApiController] + public class LibraryItemsApi : ControllerBase + { + private readonly ILibraryItemRepository _libraryItems; + private readonly string _baseURL; + + + public LibraryItemsApi(ILibraryItemRepository libraryItems, IConfiguration configuration) + { + _libraryItems = libraryItems; + _baseURL = configuration.GetValue("public_url").TrimEnd('/'); + } + + [HttpGet] + [Authorize(Policy = "Read")] + public async Task>> GetAll([FromQuery] string sortBy, + [FromQuery] int afterID, + [FromQuery] Dictionary where, + [FromQuery] int limit = 50) + { + where.Remove("sortBy"); + where.Remove("limit"); + where.Remove("afterID"); + + try + { + ICollection ressources = await _libraryItems.GetAll( + ApiHelper.ParseWhere(where), + new Sort(sortBy), + new Pagination(limit, afterID)); + + return new Page(ressources, + _baseURL + Request.Path, + Request.Query.ToDictionary(x => x.Key, x => x.Value.ToString(), StringComparer.InvariantCultureIgnoreCase), + limit); + } + catch (ItemNotFound) + { + return NotFound(); + } + catch (ArgumentException ex) + { + return BadRequest(new {Error = ex.Message}); + } + } + } +} \ No newline at end of file