diff --git a/Kyoo.Common/Models/LibraryItem.cs b/Kyoo.Common/Models/LibraryItem.cs new file mode 100644 index 00000000..176c056f --- /dev/null +++ b/Kyoo.Common/Models/LibraryItem.cs @@ -0,0 +1,84 @@ +using System; +using System.Linq.Expressions; + +namespace Kyoo.Models +{ + public enum ItemType + { + Show, + Movie, + Collection + } + + public class LibraryItem : IRessource + { + public int ID { get; set; } + public string Slug { get; set; } + public string Title { get; set; } + public string Overview { get; set; } + public Status? Status { get; set; } + public string TrailerUrl { get; set; } + public int? StartYear { get; set; } + public int? EndYear { get; set; } + public string Poster { get; set; } + public ItemType Type { get; set; } + + public LibraryItem() {} + + public LibraryItem(Show show) + { + ID = show.ID; + Slug = show.Slug; + Title = show.Title; + Overview = show.Overview; + Status = show.Status; + TrailerUrl = show.TrailerUrl; + StartYear = show.StartYear; + EndYear = show.EndYear; + Poster = show.Poster; + Type = show.IsMovie ? ItemType.Movie : ItemType.Show; + } + + public LibraryItem(Collection collection) + { + ID = -collection.ID; + Slug = collection.Slug; + Title = collection.Name; + Overview = collection.Overview; + Status = Models.Status.Unknown; + TrailerUrl = null; + StartYear = null; + EndYear = null; + Poster = collection.Poster; + Type = ItemType.Collection; + } + + public static Expression> FromShow => x => new LibraryItem + { + ID = x.ID, + Slug = x.Slug, + Title = x.Title, + Overview = x.Overview, + Status = x.Status, + TrailerUrl = x.TrailerUrl, + StartYear = x.StartYear, + EndYear = x.EndYear, + Poster= x.Poster, + Type = x.IsMovie ? ItemType.Movie : ItemType.Show + }; + + public static Expression> FromCollection => x => new LibraryItem + { + ID = -x.ID, + Slug = x.Slug, + Title = x.Name, + Overview = x.Overview, + Status = Models.Status.Unknown, + TrailerUrl = null, + StartYear = null, + EndYear = null, + Poster= x.Poster, + Type = ItemType.Collection + }; + } +} \ No newline at end of file diff --git a/Kyoo.Common/Models/Ressources/Collection.cs b/Kyoo.Common/Models/Ressources/Collection.cs index a32478f4..e6fcee84 100644 --- a/Kyoo.Common/Models/Ressources/Collection.cs +++ b/Kyoo.Common/Models/Ressources/Collection.cs @@ -12,7 +12,6 @@ namespace Kyoo.Models public string Name { get; set; } public string Poster { get; set; } public string Overview { get; set; } - [JsonIgnore] public string ImgPrimary { get; set; } [NotMergable] [JsonIgnore] public virtual IEnumerable Links { get; set; } public virtual IEnumerable Shows { @@ -30,20 +29,12 @@ namespace Kyoo.Models public Collection() { } - public Collection(string slug, string name, string overview, string imgPrimary) + public Collection(string slug, string name, string overview, string poster) { Slug = slug; Name = name; Overview = overview; - ImgPrimary = imgPrimary; - } - - public Show AsShow() - { - return new Show(Slug, Name, null, null, Overview, null, null, null, null, null, null) - { - IsCollection = true - }; + Poster = poster; } } } diff --git a/Kyoo.Common/Models/Ressources/Show.cs b/Kyoo.Common/Models/Ressources/Show.cs index 5b71b7a4..3a129b65 100644 --- a/Kyoo.Common/Models/Ressources/Show.cs +++ b/Kyoo.Common/Models/Ressources/Show.cs @@ -28,7 +28,6 @@ namespace Kyoo.Models public bool IsMovie { get; set; } - public bool IsCollection; public virtual IEnumerable Genres { @@ -82,7 +81,6 @@ namespace Kyoo.Models StartYear = startYear; EndYear = endYear; ExternalIDs = externalIDs; - IsCollection = false; } public Show(string slug, @@ -112,7 +110,6 @@ namespace Kyoo.Models Logo = logo; Backdrop = backdrop; ExternalIDs = externalIDs; - IsCollection = false; } public string GetID(string provider) @@ -140,5 +137,5 @@ namespace Kyoo.Models } } - public enum Status { Finished, Airing, Planned } + public enum Status { Finished, Airing, Planned, Unknown } } diff --git a/Kyoo/Controllers/Repositories/LibraryRepository.cs b/Kyoo/Controllers/Repositories/LibraryRepository.cs index 5df3c071..e4718b76 100644 --- a/Kyoo/Controllers/Repositories/LibraryRepository.cs +++ b/Kyoo/Controllers/Repositories/LibraryRepository.cs @@ -136,5 +136,29 @@ namespace Kyoo.Controllers throw new ItemNotFound(); return libraries; } + + public async Task> GetItems(string librarySlug, + Expression> where = null, + Sort sort = default, + Pagination limit = default) + { + IQueryable query = _database.Shows + .Where(x => !_database.CollectionLinks.Any(y => y.ShowID == x.ID)) + .Select(LibraryItem.FromShow) + .Concat(_database.Collections + .Select(LibraryItem.FromCollection)); + + ICollection items = await ApplyFilters(query, + async id => id > 0 + ? new LibraryItem(await _database.Shows.FirstOrDefaultAsync(x => x.ID == id)) + : new LibraryItem(await _database.Collections.FirstOrDefaultAsync(x => x.ID == -id)), + x => x.Slug, + where, + sort, + limit); + if (!items.Any() && await Get(librarySlug) == null) + throw new ItemNotFound(); + return items; + } } } \ No newline at end of file diff --git a/Kyoo/Startup.cs b/Kyoo/Startup.cs index ef540054..cb160a92 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/LibrariesApi.cs b/Kyoo/Views/API/LibrariesApi.cs index 055df5b9..689e71e0 100644 --- a/Kyoo/Views/API/LibrariesApi.cs +++ b/Kyoo/Views/API/LibrariesApi.cs @@ -167,38 +167,38 @@ namespace Kyoo.Api } } - // [HttpGet("{id:int}/item")] - // [HttpGet("{id:int}/items")] - // [Authorize(Policy = "Read")] - // public async Task>> GetItems(int id, - // [FromQuery] string sortBy, - // [FromQuery] int afterID, - // [FromQuery] Dictionary where, - // [FromQuery] int limit = 50) - // { - // where.Remove("id"); - // where.Remove("sortBy"); - // where.Remove("limit"); - // where.Remove("afterID"); - // - // try - // { - // ICollection ressources = await _libraryManager.GetItemsFromLibrary(id, - // ApiHelper.ParseWhere(where), - // new Sort(sortBy), - // new Pagination(limit, afterID)); - // - // return Page(ressources, limit); - // } - // catch (ItemNotFound) - // { - // return NotFound(); - // } - // catch (ArgumentException ex) - // { - // return BadRequest(new {Error = ex.Message}); - // } - // } + [HttpGet("{id:int}/item")] + [HttpGet("{id:int}/items")] + [Authorize(Policy = "Read")] + public async Task>> GetItems(int id, + [FromQuery] string sortBy, + [FromQuery] int afterID, + [FromQuery] Dictionary where, + [FromQuery] int limit = 50) + { + where.Remove("id"); + where.Remove("sortBy"); + where.Remove("limit"); + where.Remove("afterID"); + + try + { + ICollection ressources = await ((LibraryRepository)_libraryManager.LibraryRepository).GetItems("", + ApiHelper.ParseWhere(where), + new Sort(sortBy), + new Pagination(limit, afterID)); + + return Page(ressources, limit); + } + catch (ItemNotFound) + { + return NotFound(); + } + catch (ArgumentException ex) + { + return BadRequest(new {Error = ex.Message}); + } + } // // [HttpGet("{slug}/collection")] // [HttpGet("{slug}/collections")]