mirror of
				https://github.com/zoriya/Kyoo.git
				synced 2025-11-04 03:27:14 -05:00 
			
		
		
		
	Adding library items
This commit is contained in:
		
							parent
							
								
									6d29298073
								
							
						
					
					
						commit
						e69eda8df3
					
				
							
								
								
									
										84
									
								
								Kyoo.Common/Models/LibraryItem.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								Kyoo.Common/Models/LibraryItem.cs
									
									
									
									
									
										Normal 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
 | 
				
			||||||
 | 
							};
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -12,7 +12,6 @@ namespace Kyoo.Models
 | 
				
			|||||||
		public string Name { get; set; }
 | 
							public string Name { get; set; }
 | 
				
			||||||
		public string Poster { get; set; }
 | 
							public string Poster { get; set; }
 | 
				
			||||||
		public string Overview { get; set; }
 | 
							public string Overview { get; set; }
 | 
				
			||||||
		[JsonIgnore] public string ImgPrimary { get; set; }
 | 
					 | 
				
			||||||
		[NotMergable] [JsonIgnore] public virtual IEnumerable<CollectionLink> Links { get; set; }
 | 
							[NotMergable] [JsonIgnore] public virtual IEnumerable<CollectionLink> Links { get; set; }
 | 
				
			||||||
		public virtual IEnumerable<Show> Shows
 | 
							public virtual IEnumerable<Show> Shows
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@ -30,20 +29,12 @@ namespace Kyoo.Models
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		public Collection() { }
 | 
							public Collection() { }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		public Collection(string slug, string name, string overview, string imgPrimary)
 | 
							public Collection(string slug, string name, string overview, string poster)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			Slug = slug;
 | 
								Slug = slug;
 | 
				
			||||||
			Name = name;
 | 
								Name = name;
 | 
				
			||||||
			Overview = overview;
 | 
								Overview = overview;
 | 
				
			||||||
			ImgPrimary = imgPrimary;
 | 
								Poster = poster;
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		public Show AsShow()
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			return new Show(Slug, Name, null, null, Overview, null, null, null, null, null, null)
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				IsCollection = true
 | 
					 | 
				
			||||||
			};
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -28,7 +28,6 @@ namespace Kyoo.Models
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		public bool IsMovie { get; set; }
 | 
							public bool IsMovie { get; set; }
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		public bool IsCollection;
 | 
					 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		public virtual IEnumerable<Genre> Genres
 | 
							public virtual IEnumerable<Genre> Genres
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@ -82,7 +81,6 @@ namespace Kyoo.Models
 | 
				
			|||||||
			StartYear = startYear;
 | 
								StartYear = startYear;
 | 
				
			||||||
			EndYear = endYear;
 | 
								EndYear = endYear;
 | 
				
			||||||
			ExternalIDs = externalIDs;
 | 
								ExternalIDs = externalIDs;
 | 
				
			||||||
			IsCollection = false;
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		public Show(string slug,
 | 
							public Show(string slug,
 | 
				
			||||||
@ -112,7 +110,6 @@ namespace Kyoo.Models
 | 
				
			|||||||
			Logo = logo;
 | 
								Logo = logo;
 | 
				
			||||||
			Backdrop = backdrop;
 | 
								Backdrop = backdrop;
 | 
				
			||||||
			ExternalIDs = externalIDs;
 | 
								ExternalIDs = externalIDs;
 | 
				
			||||||
			IsCollection = false;
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		public string GetID(string provider)
 | 
							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 }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -136,5 +136,29 @@ namespace Kyoo.Controllers
 | 
				
			|||||||
				throw new ItemNotFound();
 | 
									throw new ItemNotFound();
 | 
				
			||||||
			return libraries;
 | 
								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;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -46,9 +46,9 @@ namespace Kyoo
 | 
				
			|||||||
			services.AddDbContext<DatabaseContext>(options =>
 | 
								services.AddDbContext<DatabaseContext>(options =>
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				options.UseLazyLoadingProxies()
 | 
									options.UseLazyLoadingProxies()
 | 
				
			||||||
					.UseNpgsql(_configuration.GetConnectionString("Database"));
 | 
										.UseNpgsql(_configuration.GetConnectionString("Database"))
 | 
				
			||||||
				// .EnableSensitiveDataLogging()
 | 
									 .EnableSensitiveDataLogging()
 | 
				
			||||||
				// .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()));
 | 
									 .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()));
 | 
				
			||||||
			}, ServiceLifetime.Transient);
 | 
								}, ServiceLifetime.Transient);
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
			services.AddDbContext<IdentityDatabase>(options =>
 | 
								services.AddDbContext<IdentityDatabase>(options =>
 | 
				
			||||||
 | 
				
			|||||||
@ -167,38 +167,38 @@ namespace Kyoo.Api
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		// [HttpGet("{id:int}/item")]
 | 
							[HttpGet("{id:int}/item")]
 | 
				
			||||||
		// [HttpGet("{id:int}/items")]
 | 
							[HttpGet("{id:int}/items")]
 | 
				
			||||||
		// [Authorize(Policy = "Read")]
 | 
							[Authorize(Policy = "Read")]
 | 
				
			||||||
		// public async Task<ActionResult<Page<Collection>>> GetItems(int id,
 | 
							public async Task<ActionResult<Page<LibraryItem>>> GetItems(int id,
 | 
				
			||||||
		// 	[FromQuery] string sortBy,
 | 
								[FromQuery] string sortBy,
 | 
				
			||||||
		// 	[FromQuery] int afterID,
 | 
								[FromQuery] int afterID,
 | 
				
			||||||
		// 	[FromQuery] Dictionary<string, string> where,
 | 
								[FromQuery] Dictionary<string, string> where,
 | 
				
			||||||
		// 	[FromQuery] int limit = 50)
 | 
								[FromQuery] int limit = 50)
 | 
				
			||||||
		// {
 | 
							{
 | 
				
			||||||
		// 	where.Remove("id");
 | 
								where.Remove("id");
 | 
				
			||||||
		// 	where.Remove("sortBy");
 | 
								where.Remove("sortBy");
 | 
				
			||||||
		// 	where.Remove("limit");
 | 
								where.Remove("limit");
 | 
				
			||||||
		// 	where.Remove("afterID");
 | 
								where.Remove("afterID");
 | 
				
			||||||
		//
 | 
					
 | 
				
			||||||
		// 	try
 | 
								try
 | 
				
			||||||
		// 	{
 | 
								{
 | 
				
			||||||
		// 		ICollection<Collection> ressources = await _libraryManager.GetItemsFromLibrary(id,
 | 
									ICollection<LibraryItem> ressources = await ((LibraryRepository)_libraryManager.LibraryRepository).GetItems("",
 | 
				
			||||||
		// 			ApiHelper.ParseWhere<Collection>(where),
 | 
										ApiHelper.ParseWhere<LibraryItem>(where),
 | 
				
			||||||
		// 			new Sort<Collection>(sortBy),
 | 
										new Sort<LibraryItem>(sortBy),
 | 
				
			||||||
		// 			new Pagination(limit, afterID));
 | 
										new Pagination(limit, afterID));
 | 
				
			||||||
		//
 | 
					
 | 
				
			||||||
		// 		return Page(ressources, limit);
 | 
									return Page(ressources, limit);
 | 
				
			||||||
		// 	}
 | 
								}
 | 
				
			||||||
		// 	catch (ItemNotFound)
 | 
								catch (ItemNotFound)
 | 
				
			||||||
		// 	{
 | 
								{
 | 
				
			||||||
		// 		return NotFound();
 | 
									return NotFound();
 | 
				
			||||||
		// 	}
 | 
								}
 | 
				
			||||||
		// 	catch (ArgumentException ex)
 | 
								catch (ArgumentException ex)
 | 
				
			||||||
		// 	{
 | 
								{
 | 
				
			||||||
		// 		return BadRequest(new {Error = ex.Message});
 | 
									return BadRequest(new {Error = ex.Message});
 | 
				
			||||||
		// 	}
 | 
								}
 | 
				
			||||||
		// }
 | 
							}
 | 
				
			||||||
		//
 | 
							//
 | 
				
			||||||
		// [HttpGet("{slug}/collection")]
 | 
							// [HttpGet("{slug}/collection")]
 | 
				
			||||||
		// [HttpGet("{slug}/collections")]
 | 
							// [HttpGet("{slug}/collections")]
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user