Adding library items

This commit is contained in:
Zoe Roux 2020-07-28 18:33:10 +02:00
parent 6d29298073
commit e69eda8df3
6 changed files with 146 additions and 50 deletions

View File

@ -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<Func<Show, LibraryItem>> 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<Func<Collection, LibraryItem>> 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
};
}
}

View File

@ -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<CollectionLink> Links { get; set; }
public virtual IEnumerable<Show> 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;
}
}
}

View File

@ -28,7 +28,6 @@ namespace Kyoo.Models
public bool IsMovie { get; set; }
public bool IsCollection;
public virtual IEnumerable<Genre> 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 }
}

View File

@ -136,5 +136,29 @@ namespace Kyoo.Controllers
throw new ItemNotFound();
return libraries;
}
public async Task<ICollection<LibraryItem>> GetItems(string librarySlug,
Expression<Func<LibraryItem, bool>> where = null,
Sort<LibraryItem> sort = default,
Pagination limit = default)
{
IQueryable<LibraryItem> query = _database.Shows
.Where(x => !_database.CollectionLinks.Any(y => y.ShowID == x.ID))
.Select(LibraryItem.FromShow)
.Concat(_database.Collections
.Select(LibraryItem.FromCollection));
ICollection<LibraryItem> 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;
}
}
}

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

@ -167,38 +167,38 @@ namespace Kyoo.Api
}
}
// [HttpGet("{id:int}/item")]
// [HttpGet("{id:int}/items")]
// [Authorize(Policy = "Read")]
// public async Task<ActionResult<Page<Collection>>> GetItems(int id,
// [FromQuery] string sortBy,
// [FromQuery] int afterID,
// [FromQuery] Dictionary<string, string> where,
// [FromQuery] int limit = 50)
// {
// where.Remove("id");
// where.Remove("sortBy");
// where.Remove("limit");
// where.Remove("afterID");
//
// try
// {
// ICollection<Collection> ressources = await _libraryManager.GetItemsFromLibrary(id,
// ApiHelper.ParseWhere<Collection>(where),
// new Sort<Collection>(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<ActionResult<Page<LibraryItem>>> GetItems(int id,
[FromQuery] string sortBy,
[FromQuery] int afterID,
[FromQuery] Dictionary<string, string> where,
[FromQuery] int limit = 50)
{
where.Remove("id");
where.Remove("sortBy");
where.Remove("limit");
where.Remove("afterID");
try
{
ICollection<LibraryItem> ressources = await ((LibraryRepository)_libraryManager.LibraryRepository).GetItems("",
ApiHelper.ParseWhere<LibraryItem>(where),
new Sort<LibraryItem>(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")]