Adding the /items route

This commit is contained in:
Zoe Roux 2020-07-29 00:46:24 +02:00
parent 5b0b2fbb7b
commit 24355bac84
2 changed files with 66 additions and 3 deletions

View File

@ -46,9 +46,9 @@ namespace Kyoo
services.AddDbContext<DatabaseContext>(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<IdentityDatabase>(options =>

View File

@ -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<string>("public_url").TrimEnd('/');
}
[HttpGet]
[Authorize(Policy = "Read")]
public async Task<ActionResult<Page<LibraryItem>>> GetAll([FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
[FromQuery] int limit = 50)
{
where.Remove("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<LibraryItem> ressources = await _libraryItems.GetAll(
ApiHelper.ParseWhere<LibraryItem>(where),
new Sort<LibraryItem>(sortBy),
new Pagination(limit, afterID));
return new Page<LibraryItem>(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});
}
}
}
}