mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
The track graph validation now works a bit
This commit is contained in:
parent
bd15d425df
commit
6e61b21e9a
@ -222,22 +222,37 @@ namespace Kyoo.Controllers
|
|||||||
|
|
||||||
private void ValidateChanges()
|
private void ValidateChanges()
|
||||||
{
|
{
|
||||||
foreach (EntityEntry sourceEntry in _database.ChangeTracker.Entries())
|
_database.ChangeTracker.AutoDetectChangesEnabled = false;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (sourceEntry.State != EntityState.Added && sourceEntry.State != EntityState.Modified)
|
foreach (EntityEntry sourceEntry in _database.ChangeTracker.Entries())
|
||||||
continue;
|
|
||||||
|
|
||||||
foreach (NavigationEntry navigation in sourceEntry.Navigations)
|
|
||||||
{
|
{
|
||||||
if (navigation.IsModified == false)
|
if (sourceEntry.State != EntityState.Added && sourceEntry.State != EntityState.Modified)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
object value = navigation.Metadata.PropertyInfo.GetValue(sourceEntry.Entity);
|
foreach (NavigationEntry navigation in sourceEntry.Navigations)
|
||||||
if (value == null)
|
{
|
||||||
continue;
|
if (navigation.IsModified == false)
|
||||||
navigation.Metadata.PropertyInfo.SetValue(sourceEntry.Entity, Validate(value));
|
continue;
|
||||||
|
|
||||||
|
object value = navigation.Metadata.PropertyInfo.GetValue(sourceEntry.Entity);
|
||||||
|
if (value == null)
|
||||||
|
continue;
|
||||||
|
object newValue = Validate(value);
|
||||||
|
if (newValue != value)
|
||||||
|
navigation.Metadata.PropertyInfo.SetValue(sourceEntry.Entity, newValue);
|
||||||
|
else
|
||||||
|
_database.Entry(value).State = EntityState.Detached;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_database.ChangeTracker.AutoDetectChangesEnabled = true;
|
||||||
|
_database.ChangeTracker.DetectChanges();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -439,28 +454,27 @@ namespace Kyoo.Controllers
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region ValidateValue
|
#region ValidateValue
|
||||||
|
|
||||||
private T Validate<T>(T obj) where T : class
|
private T Validate<T>(T obj) where T : class
|
||||||
{
|
{
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (!(obj is IEnumerable) && _database.Entry(obj).IsKeySet)
|
if (!(obj is IEnumerable) && _database.Entry(obj).State != EntityState.Added)
|
||||||
return obj;
|
return obj;
|
||||||
|
|
||||||
switch(obj)
|
switch(obj)
|
||||||
{
|
{
|
||||||
case ProviderLink link:
|
case ProviderLink link:
|
||||||
link.Provider = Validate(link.Provider);
|
link.Provider = ValidateLink(() => link.Provider); //TODO Calling this methods make the obj in a deteached state. Don't know why.
|
||||||
link.Library = Validate(link.Library);
|
link.Library = ValidateLink(() => link.Library);
|
||||||
return obj;
|
return obj;
|
||||||
case GenreLink link:
|
case GenreLink link:
|
||||||
link.Show = Validate(link.Show);
|
link.Show = ValidateLink(() => link.Show);
|
||||||
link.Genre = Validate(link.Genre);
|
link.Genre = ValidateLink(() => link.Genre);
|
||||||
return obj;
|
return obj;
|
||||||
case PeopleLink link:
|
case PeopleLink link:
|
||||||
link.Show = Validate(link.Show);
|
link.Show = ValidateLink(() => link.Show);
|
||||||
link.People = Validate(link.People);
|
link.People = ValidateLink(() => link.People);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -484,7 +498,24 @@ namespace Kyoo.Controllers
|
|||||||
|
|
||||||
public IEnumerable<T> ValidateList<T>(IEnumerable<T> list) where T : class
|
public IEnumerable<T> ValidateList<T>(IEnumerable<T> list) where T : class
|
||||||
{
|
{
|
||||||
return list.Select(Validate).Where(x => x != null);
|
return list.Select(x =>
|
||||||
|
{
|
||||||
|
T tmp = Validate(x);
|
||||||
|
if (tmp != x)
|
||||||
|
_database.Entry(x).State = EntityState.Detached;
|
||||||
|
return tmp ?? x;
|
||||||
|
}).Where(x => x != null).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private T ValidateLink<T>(Func<T> linkGet) where T : class
|
||||||
|
{
|
||||||
|
if (linkGet == null)
|
||||||
|
throw new ArgumentNullException(nameof(linkGet));
|
||||||
|
T oldValue = linkGet();
|
||||||
|
T newValue = Validate(oldValue);
|
||||||
|
if (!ReferenceEquals(oldValue, newValue))
|
||||||
|
_database.Entry(linkGet()).State = EntityState.Detached;
|
||||||
|
return newValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Library Validate(Library library)
|
public Library Validate(Library library)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user