mirror of
				https://github.com/zoriya/Kyoo.git
				synced 2025-11-03 19:17:16 -05:00 
			
		
		
		
	Using where's argument of get all instead of a method in a repositroy
This commit is contained in:
		
							parent
							
								
									14e4772dd1
								
							
						
					
					
						commit
						438074a93e
					
				@ -1,5 +1,6 @@
 | 
				
			|||||||
using System;
 | 
					using System;
 | 
				
			||||||
using System.Collections.Generic;
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
using System.Linq.Expressions;
 | 
					using System.Linq.Expressions;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
using Kyoo.Models;
 | 
					using Kyoo.Models;
 | 
				
			||||||
@ -235,62 +236,83 @@ namespace Kyoo.Controllers
 | 
				
			|||||||
			return PeopleRepository.Get(where);
 | 
								return PeopleRepository.Get(where);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		public async Task Load<T, T2>(T obj, Expression<Func<T, T2>> member)
 | 
							public Task Load<T, T2>(T obj, Expression<Func<T, T2>> member)
 | 
				
			||||||
			where T : class, IResource
 | 
								where T : class, IResource
 | 
				
			||||||
			where T2 : class, IResource
 | 
								where T2 : class, IResource
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			dynamic identifier = obj.ID > 0 ? obj.ID : obj.Slug;
 | 
								return (obj, (T2)default) switch
 | 
				
			||||||
			switch (obj, (T2)default)
 | 
					 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				case (Show s, Studio): s.Studio = await StudioRepository.GetFromShow(identifier); break;
 | 
									(Show s, Studio) => StudioRepository.Get(x => x.Shows.Any(Utility.ResourceEqualsFunc<Show>(obj)))
 | 
				
			||||||
 | 
										.Then(x => s.Studio = x),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				case (Season s, Show): s.Show = await ShowRepository.GetFromSeason(identifier); break;
 | 
									(Season s, Show) => ShowRepository.Get(Utility.ResourceEquals<Show>(obj)).Then(x => s.Show = x),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				case (Episode e, Show): e.Show = await ShowRepository.GetFromEpisode(identifier); break;
 | 
									(Episode e, Show) => ShowRepository.Get(Utility.ResourceEquals<Show>(obj)).Then(x => e.Show = x),
 | 
				
			||||||
				case (Episode e, Season): e.Season = await SeasonRepository.GetFromEpisode(identifier); break;
 | 
									(Episode e, Season) => SeasonRepository.Get(Utility.ResourceEquals<Season>(obj))
 | 
				
			||||||
 | 
										.Then(x => e.Season = x),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				case (Track t, Episode): t.Episode = await EpisodeRepository.GetFromTrack(identifier); break;
 | 
									(Track t, Episode) => EpisodeRepository.Get(Utility.ResourceEquals<Episode>(obj))
 | 
				
			||||||
 | 
										.Then(x => t.Episode = x),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				default: throw new ArgumentException($"Couldn't find a way to load {member} of {typeof(T).Name}.");
 | 
									_ => throw new ArgumentException($"Couldn't find a way to load {member} of {typeof(T).Name}.")
 | 
				
			||||||
			}
 | 
								};
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		public async Task Load<T, T2>(T obj, Expression<Func<T, ICollection<T2>>> member)
 | 
							public Task Load<T, T2>(T obj, Expression<Func<T, ICollection<T2>>> member)
 | 
				
			||||||
			where T : class, IResource
 | 
								where T : class, IResource
 | 
				
			||||||
			where T2 : class
 | 
								where T2 : class
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			dynamic identifier = obj.ID > 0 ? obj.ID : obj.Slug;
 | 
								return (obj, (T2)default) switch
 | 
				
			||||||
			switch (obj, (T2)default)
 | 
					 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				case (Library l, ProviderID): l.Providers = await ProviderRepository.GetFromLibrary(identifier); break; 
 | 
									(Library l, ProviderID) => ProviderRepository.GetAll(Utility.ResourceEquals<ProviderID>(obj))
 | 
				
			||||||
				case (Library l, Show): l.Shows = await ShowRepository.GetFromLibrary(identifier); break; 
 | 
										.Then(x => l.Providers = x), 
 | 
				
			||||||
				case (Library l, Collection): l.Collections = await CollectionRepository.GetFromLibrary(identifier); break; 
 | 
									(Library l, Show) => ShowRepository.GetAll(Utility.ResourceEquals<Show>(obj))
 | 
				
			||||||
 | 
										.Then(x => l.Shows = x), 
 | 
				
			||||||
 | 
									(Library l, Collection) => CollectionRepository.GetAll(Utility.ResourceEquals<Collection>(obj))
 | 
				
			||||||
 | 
										.Then(x => l.Collections = x), 
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
				case (Collection c, Show): c.Shows = await ShowRepository.GetFromCollection(identifier); break; 
 | 
									(Collection c, Show) => ShowRepository.GetAll(Utility.ResourceEquals<Show>(obj))
 | 
				
			||||||
				case (Collection c, Library): c.Libraries = await LibraryRepository.GetFromCollection(identifier); break; 
 | 
										.Then(x => c.Shows = x), 
 | 
				
			||||||
 | 
									(Collection c, Library) => LibraryRepository.GetAll(Utility.ResourceEquals<Library>(obj))
 | 
				
			||||||
 | 
										.Then(x => c.Libraries = x), 
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
				case (Show s, MetadataID): s.ExternalIDs = await ProviderRepository.GetFromShow(identifier); break;
 | 
									(Show s, MetadataID) => ProviderRepository.Get(Utility.ResourceEquals<MetadataID>(obj))
 | 
				
			||||||
				case (Show s, Genre): s.Genres = await GenreRepository.GetFromShow(identifier); break;
 | 
										.Then(x => s.ExternalIDs = x),
 | 
				
			||||||
				case (Show s, PeopleRole): s.People = await PeopleRepository.GetFromShow(identifier); break;
 | 
									(Show s, Genre) => GenreRepository.GetAll(Utility.ResourceEquals<Genre>(obj))
 | 
				
			||||||
				case (Show s, Season): s.Seasons = await SeasonRepository.GetFromShow(identifier); break;
 | 
										.Then(x => s.Genres = x),
 | 
				
			||||||
				case (Show s, Episode): s.Episodes = await EpisodeRepository.GetFromShow(identifier); break;
 | 
									(Show s, PeopleRole) => PeopleRepository.GetFromShow(Utility.ResourceEquals<PeopleRole>(obj))
 | 
				
			||||||
				case (Show s, Library): s.Libraries = await LibraryRepository.GetFromShow(identifier); break;
 | 
										.Then(x => s.People = x),
 | 
				
			||||||
				case (Show s, Collection): s.Collections = await CollectionRepository.GetFromShow(identifier); break;
 | 
									(Show s, Season) => SeasonRepository.GetAll(Utility.ResourceEquals<Season>(obj))
 | 
				
			||||||
 | 
										.Then(x => s.Seasons = x),
 | 
				
			||||||
 | 
									(Show s, Episode) => EpisodeRepository.GetAll(Utility.ResourceEquals<Episode>(obj))
 | 
				
			||||||
 | 
										.Then(x => s.Episodes = x),
 | 
				
			||||||
 | 
									(Show s, Library) => LibraryRepository.GetAll(Utility.ResourceEquals<Library>(obj))
 | 
				
			||||||
 | 
										.Then(x => s.Libraries = x),
 | 
				
			||||||
 | 
									(Show s, Collection) => CollectionRepository.GetAll(Utility.ResourceEquals<Collection>(obj))
 | 
				
			||||||
 | 
										.Then(x => s.Collections = x),
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
				case (Season s, MetadataID): s.ExternalIDs = await ProviderRepository.GetFromSeason(identifier); break;
 | 
									(Season s, MetadataID) => ProviderRepository.GetAll(Utility.ResourceEquals<MetadataID>(obj))
 | 
				
			||||||
				case (Season s, Episode): s.Episodes = await EpisodeRepository.GetFromSeason(identifier); break;
 | 
										.Then(x => s.ExternalIDs = x),
 | 
				
			||||||
 | 
									(Season s, Episode) => EpisodeRepository.GetAll(Utility.ResourceEquals<Episode>(obj))
 | 
				
			||||||
 | 
										.Then(x => s.Episodes = x),
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
				case (Episode e, MetadataID): e.ExternalIDs = await ProviderRepository.GetFromEpisode(identifier); break;
 | 
									(Episode e, MetadataID) => ProviderRepository.GetAll(Utility.ResourceEquals<MetadataID>(obj))
 | 
				
			||||||
				case (Episode e, Track): e.Tracks = await TrackRepository.GetFromEpisode(identifier); break;
 | 
										.Then(x => e.ExternalIDs = x),
 | 
				
			||||||
 | 
									(Episode e, Track) => TrackRepository.GetAll(Utility.ResourceEquals<Track>(obj))
 | 
				
			||||||
 | 
										.Then(x => e.Tracks = x),
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
				case (Genre g, Show): g.Shows = await ShowRepository.GetFromGenre(identifier); break;
 | 
									(Genre g, Show) => ShowRepository.GetAll(Utility.ResourceEquals<Show>(obj))
 | 
				
			||||||
 | 
										.Then(x => g.Shows = x),
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
				case (Studio s, Show): s.Shows = await ShowRepository.GetFromStudio(identifier); break;
 | 
									(Studio s, Show) => ShowRepository.GetAll(Utility.ResourceEquals<Show>(obj))
 | 
				
			||||||
 | 
										.Then(x => s.Shows = x),
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
				case (People p, MetadataID): p.ExternalIDs = await ProviderRepository.GetFromPeople(identifier); break;
 | 
									(People p, MetadataID) => ProviderRepository.GetAll(Utility.ResourceEquals<MetadataID>(obj))
 | 
				
			||||||
				case (People p, PeopleRole): p.Roles = await ShowRepository.GetFromPeople(identifier); break;
 | 
										.Then(x => p.ExternalIDs = x),
 | 
				
			||||||
 | 
									(People p, PeopleRole) => PeopleRepository.GetFromPeople(Utility.ResourceEquals<PeopleRole>(obj))
 | 
				
			||||||
 | 
										.Then(x => p.Roles = x),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				default: throw new ArgumentException($"Couldn't find a way to load {member} of {typeof(T).Name}.");
 | 
									_ => throw new ArgumentException($"Couldn't find a way to load {member} of {typeof(T).Name}.")
 | 
				
			||||||
			};
 | 
								};
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,3 @@
 | 
				
			|||||||
using System;
 | 
					 | 
				
			||||||
using Kyoo.Models.Attributes;
 | 
					using Kyoo.Models.Attributes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Kyoo.Models
 | 
					namespace Kyoo.Models
 | 
				
			||||||
 | 
				
			|||||||
@ -424,6 +424,22 @@ namespace Kyoo
 | 
				
			|||||||
			}, TaskContinuationOptions.ExecuteSynchronously);
 | 
								}, TaskContinuationOptions.ExecuteSynchronously);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							public static Expression<Func<T, bool>> ResourceEquals<T>(IResource obj)
 | 
				
			||||||
 | 
								where T : IResource
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								if (obj.ID > 0)
 | 
				
			||||||
 | 
									return x => x.ID == obj.ID || x.Slug == obj.Slug;
 | 
				
			||||||
 | 
								return x => x.Slug == obj.Slug;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							public static Func<T, bool> ResourceEqualsFunc<T>(IResource obj)
 | 
				
			||||||
 | 
								where T : IResource
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								if (obj.ID > 0)
 | 
				
			||||||
 | 
									return x => x.ID == obj.ID || x.Slug == obj.Slug;
 | 
				
			||||||
 | 
								return x => x.Slug == obj.Slug;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							
 | 
				
			||||||
		public static bool ResourceEquals([CanBeNull] object first, [CanBeNull] object second)
 | 
							public static bool ResourceEquals([CanBeNull] object first, [CanBeNull] object second)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			if (ReferenceEquals(first, second))
 | 
								if (ReferenceEquals(first, second))
 | 
				
			||||||
@ -490,17 +506,6 @@ namespace Kyoo
 | 
				
			|||||||
			return first.SequenceEqual(second, new LinkComparer<T, T1, T2>());
 | 
								return first.SequenceEqual(second, new LinkComparer<T, T1, T2>());
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		public static string GetMemberName([NotNull] Expression expression)
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			var t = ExpressionRewrite.Rewrite(expression);
 | 
					 | 
				
			||||||
			return ExpressionRewrite.Rewrite(expression) switch
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				MemberExpression member => member.Member.Name,
 | 
					 | 
				
			||||||
				LambdaExpression lambda when IsPropertyExpression(lambda) => GetMemberName(lambda.Body),
 | 
					 | 
				
			||||||
				null => throw new ArgumentNullException(nameof(expression)),
 | 
					 | 
				
			||||||
				_ => throw new ArgumentException($"Can't get member.")
 | 
					 | 
				
			||||||
			};
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		public static Expression<T> Convert<T>([CanBeNull] this Expression expr)
 | 
							public static Expression<T> Convert<T>([CanBeNull] this Expression expr)
 | 
				
			||||||
			where T : Delegate
 | 
								where T : Delegate
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user