Fixing show's repository

This commit is contained in:
Zoe Roux 2021-03-16 23:20:56 +01:00
parent 93d271b192
commit 4b56ec4114
3 changed files with 38 additions and 17 deletions

View File

@ -28,6 +28,13 @@ namespace Kyoo.Models
return new(first, second); return new(first, second);
} }
public static Link<T, T2> UCreate<T, T2>(T first, T2 second)
where T : class, IResource
where T2 : class, IResource
{
return new(first, second, true);
}
public static Expression<Func<Link, object>> PrimaryKey public static Expression<Func<Link, object>> PrimaryKey
{ {
get get
@ -47,9 +54,11 @@ namespace Kyoo.Models
public Link() {} public Link() {}
public Link(T1 first, T2 second) public Link(T1 first, T2 second, bool privateItems = false)
: base(first, second) : base(first, second)
{ {
if (privateItems)
return;
First = first; First = first;
Second = second; Second = second;
} }

View File

@ -167,9 +167,8 @@ namespace Kyoo.Controllers
if (resetOld) if (resetOld)
Utility.Nullify(old); Utility.Nullify(old);
await EditRelations(old, edited, resetOld);
Utility.Complete(old, edited, x => x.GetCustomAttribute<LoadableRelationAttribute>() == null); Utility.Complete(old, edited, x => x.GetCustomAttribute<LoadableRelationAttribute>() == null);
await Validate(old); await EditRelations(old, edited, resetOld);
await Database.SaveChangesAsync(); await Database.SaveChangesAsync();
return old; return old;
} }

View File

@ -100,32 +100,45 @@ namespace Kyoo.Controllers
{ {
await base.Validate(resource); await base.Validate(resource);
resource.Studio = await _studios.CreateIfNotExists(resource.Studio, true); resource.Studio = await _studios.CreateIfNotExists(resource.Studio, true);
resource.Genres = await resource.Genres resource.GenreLinks = await resource.Genres
.SelectAsync(x => _genres.CreateIfNotExists(x, true)) .SelectAsync(async x => Link.UCreate(resource, await _genres.CreateIfNotExists(x, true)))
.ToListAsync(); .ToListAsync();
await resource.ExternalIDs.ForEachAsync(async id => await resource.ExternalIDs.ForEachAsync(async id =>
id.Provider = await _providers.CreateIfNotExists(id.Provider, true)); {
id.ProviderID = (await _providers.CreateIfNotExists(id.Provider, true)).ID;
id.Provider = null;
});
await resource.People.ForEachAsync(async role => await resource.People.ForEachAsync(async role =>
role.People = await _people.CreateIfNotExists(role.People, true)); {
role.PeopleID = (await _people.CreateIfNotExists(role.People, true)).ID;
role.People = null;
});
} }
protected override async Task EditRelations(Show resource, Show changed, bool resetOld) protected override async Task EditRelations(Show resource, Show changed, bool resetOld)
{ {
await Validate(changed);
if (changed.Aliases != null || resetOld) if (changed.Aliases != null || resetOld)
resource.Aliases = changed.Aliases; resource.Aliases = changed.Aliases;
if (changed.Genres != null || resetOld) if (changed.Genres != null || resetOld)
await Database.Entry(resource).Collection(x => x.Genres).LoadAsync(); {
resource.Genres = changed.Genres; await Database.Entry(resource).Collection(x => x.GenreLinks).LoadAsync();
resource.GenreLinks = changed.GenreLinks;
}
if (changed.People != null || resetOld) if (changed.People != null || resetOld)
{
await Database.Entry(resource).Collection(x => x.People).LoadAsync(); await Database.Entry(resource).Collection(x => x.People).LoadAsync();
resource.People = changed.People; resource.People = changed.People;
}
if (changed.ExternalIDs != null || resetOld) if (changed.ExternalIDs != null || resetOld)
{
await Database.Entry(resource).Collection(x => x.ExternalIDs).LoadAsync(); await Database.Entry(resource).Collection(x => x.ExternalIDs).LoadAsync();
resource.ExternalIDs = changed.ExternalIDs; resource.ExternalIDs = changed.ExternalIDs;
await base.EditRelations(resource, changed, resetOld); }
} }
public async Task AddShowLink(int showID, int? libraryID, int? collectionID) public async Task AddShowLink(int showID, int? libraryID, int? collectionID)