mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Fixing many bugs with the edit
This commit is contained in:
parent
664147f6eb
commit
135f8b3e5f
@ -177,6 +177,11 @@ namespace Kyoo
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static bool IsOfType([NotNull] object obj, [NotNull] Type type)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static object RunGenericMethod(
|
||||
[NotNull] Type owner,
|
||||
[NotNull] string methodName,
|
||||
|
@ -12,6 +12,7 @@ using Kyoo.Models.Attributes;
|
||||
using Kyoo.Models.Exceptions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
|
||||
namespace Kyoo.Controllers
|
||||
{
|
||||
@ -161,10 +162,35 @@ namespace Kyoo.Controllers
|
||||
throw new ItemNotFound($"No resource found with the ID {edited.ID}.");
|
||||
|
||||
foreach (NavigationEntry navigation in Database.Entry(old).Navigations)
|
||||
if (navigation.Metadata.PropertyInfo.GetCustomAttribute<EditableRelation>() != null
|
||||
&& navigation.Metadata.GetGetter().GetClrValue(edited) != default)
|
||||
await navigation.LoadAsync();
|
||||
{
|
||||
if (navigation.Metadata.PropertyInfo.GetCustomAttribute<EditableRelation>() != null)
|
||||
{
|
||||
if (resetOld)
|
||||
{
|
||||
await navigation.LoadAsync();
|
||||
continue;
|
||||
}
|
||||
IClrPropertyGetter getter = navigation.Metadata.GetGetter();
|
||||
|
||||
if (getter.HasDefaultValue(edited))
|
||||
continue;
|
||||
await navigation.LoadAsync();
|
||||
if (getter.GetClrValue(edited) != getter.GetClrValue(old))
|
||||
{
|
||||
navigation.Metadata.PropertyInfo.SetValue(edited, default);
|
||||
Console.WriteLine($"Loaded: {navigation.Metadata.Name}");
|
||||
}
|
||||
else
|
||||
Console.WriteLine($"Using: {navigation.Metadata.Name}");
|
||||
}
|
||||
else
|
||||
{
|
||||
navigation.Metadata.PropertyInfo.SetValue(edited, default);
|
||||
Console.WriteLine($"Skipping: {navigation.Metadata.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Loading done.");
|
||||
if (resetOld)
|
||||
Utility.Nullify(old);
|
||||
Utility.Complete(old, edited);
|
||||
@ -178,6 +204,11 @@ namespace Kyoo.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
protected bool ShouldValidate<T2>(T2 value)
|
||||
{
|
||||
return value != null && Database.Entry(value).State == EntityState.Detached;
|
||||
}
|
||||
|
||||
protected virtual Task Validate(T resource)
|
||||
{
|
||||
if (string.IsNullOrEmpty(resource.Slug))
|
||||
@ -203,7 +234,7 @@ namespace Kyoo.Controllers
|
||||
&& !typeof(string).IsAssignableFrom(x.PropertyType)))
|
||||
{
|
||||
object value = property.GetValue(resource);
|
||||
if (value is ICollection || value == null)
|
||||
if (value == null || value is ICollection || Utility.IsOfType(value, typeof(ICollection<>)))
|
||||
continue;
|
||||
value = Utility.RunGenericMethod(typeof(Enumerable), "ToList", Utility.GetEnumerableType((IEnumerable)value), value);
|
||||
property.SetValue(resource, value);
|
||||
@ -299,6 +330,8 @@ namespace Kyoo.Controllers
|
||||
|
||||
Task<T> IRepository<T>.Create(T item)
|
||||
{
|
||||
if (item == null)
|
||||
throw new ArgumentNullException(nameof(item));
|
||||
TInternal obj = item as TInternal ?? new TInternal();
|
||||
if (!(item is TInternal))
|
||||
Utility.Assign(obj, item);
|
||||
@ -309,6 +342,8 @@ namespace Kyoo.Controllers
|
||||
|
||||
Task<T> IRepository<T>.CreateIfNotExists(T item, bool silentFail)
|
||||
{
|
||||
if (item == null)
|
||||
throw new ArgumentNullException(nameof(item));
|
||||
TInternal obj = item as TInternal ?? new TInternal();
|
||||
if (!(item is TInternal))
|
||||
Utility.Assign(obj, item);
|
||||
@ -319,6 +354,8 @@ namespace Kyoo.Controllers
|
||||
|
||||
public Task<T> Edit(T edited, bool resetOld)
|
||||
{
|
||||
if (edited == null)
|
||||
throw new ArgumentNullException(nameof(edited));
|
||||
if (edited is TInternal intern)
|
||||
return Edit(intern, resetOld).Cast<T>();
|
||||
TInternal obj = new TInternal();
|
||||
@ -330,6 +367,8 @@ namespace Kyoo.Controllers
|
||||
|
||||
Task IRepository<T>.Delete(T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
if (obj is TInternal intern)
|
||||
return Delete(intern);
|
||||
TInternal item = new TInternal();
|
||||
|
@ -120,7 +120,8 @@ namespace Kyoo.Controllers
|
||||
if (resource.ExternalIDs != null)
|
||||
{
|
||||
foreach (MetadataID link in resource.ExternalIDs)
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
if (ShouldValidate(link))
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,8 @@ namespace Kyoo.Controllers
|
||||
|
||||
if (resource.ProviderLinks != null)
|
||||
foreach (ProviderLink link in resource.ProviderLinks)
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
if (ShouldValidate(link))
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
}
|
||||
|
||||
public override async Task Delete(LibraryDE obj)
|
||||
|
@ -75,7 +75,8 @@ namespace Kyoo.Controllers
|
||||
|
||||
if (resource.ExternalIDs != null)
|
||||
foreach (MetadataID link in resource.ExternalIDs)
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
if (ShouldValidate(link))
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
}
|
||||
|
||||
public override async Task Delete(People obj)
|
||||
|
@ -104,7 +104,8 @@ namespace Kyoo.Controllers
|
||||
if (resource.ExternalIDs != null)
|
||||
{
|
||||
foreach (MetadataID link in resource.ExternalIDs)
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
if (ShouldValidate(link))
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -111,20 +111,23 @@ namespace Kyoo.Controllers
|
||||
{
|
||||
await base.Validate(resource);
|
||||
|
||||
if (resource.Studio != null)
|
||||
if (ShouldValidate(resource.Studio))
|
||||
resource.Studio = await _studios.CreateIfNotExists(resource.Studio, true);
|
||||
|
||||
if (resource.GenreLinks != null)
|
||||
foreach (GenreLink link in resource.GenreLinks)
|
||||
link.Genre = await _genres.CreateIfNotExists(link.Genre, true);
|
||||
if (ShouldValidate(link))
|
||||
link.Genre = await _genres.CreateIfNotExists(link.Genre, true);
|
||||
|
||||
if (resource.People != null)
|
||||
foreach (PeopleRole link in resource.People)
|
||||
link.People = await _people.CreateIfNotExists(link.People, true);
|
||||
if (ShouldValidate(link))
|
||||
link.People = await _people.CreateIfNotExists(link.People, true);
|
||||
|
||||
if (resource.ExternalIDs != null)
|
||||
foreach (MetadataID link in resource.ExternalIDs)
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
if (ShouldValidate(link))
|
||||
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
|
||||
}
|
||||
|
||||
public async Task AddShowLink(int showID, int? libraryID, int? collectionID)
|
||||
|
Loading…
x
Reference in New Issue
Block a user