mirror of
				https://github.com/zoriya/Kyoo.git
				synced 2025-11-03 19:17:16 -05:00 
			
		
		
		
	Adding the /items route
This commit is contained in:
		
							parent
							
								
									5b0b2fbb7b
								
							
						
					
					
						commit
						24355bac84
					
				@ -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 =>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										63
									
								
								Kyoo/Views/API/LibraryItemsApi.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								Kyoo/Views/API/LibraryItemsApi.cs
									
									
									
									
									
										Normal 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});
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user