Fixing episode & library's repository

This commit is contained in:
Zoe Roux 2021-03-16 09:49:48 +01:00
parent 2317445053
commit 28bb719b06
4 changed files with 22 additions and 21 deletions

View File

@ -171,6 +171,7 @@ namespace Kyoo.Controllers
if (resetOld)
Utility.Nullify(old);
Utility.Complete(old, edited, x => x.GetCustomAttribute<EditableRelationAttribute>() != null);
await Validate(old);
await Database.SaveChangesAsync();
return old;
}
@ -185,7 +186,7 @@ namespace Kyoo.Controllers
return Validate(resource);
}
private Task Validate(T resource)
protected virtual Task Validate(T resource)
{
if (string.IsNullOrEmpty(resource.Slug))
throw new ArgumentException("Resource can't have null as a slug.");

View File

@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
@ -179,13 +178,15 @@ namespace Kyoo.Controllers
.ToListAsync();
foreach (Track x in oldTracks)
await _tracks.Delete(x);
resource.ExternalIDs = changed.ExternalIDs;
}
if (resource.ExternalIDs != null)
{
foreach (MetadataID link in resource.ExternalIDs)
if (ShouldValidate(link))
link.Provider = await _providers.CreateIfNotExists(link.Provider, true);
}
protected override async Task Validate(Episode resource)
{
await base.Validate(resource);
await resource.ExternalIDs.ForEachAsync(async id =>
id.Provider = await _providers.CreateIfNotExists(id.Provider, true));
}
public async Task Delete(string showSlug, int seasonNumber, int episodeNumber)
@ -200,10 +201,8 @@ namespace Kyoo.Controllers
throw new ArgumentNullException(nameof(obj));
_database.Entry(obj).State = EntityState.Deleted;
// await obj.Tracks.ForEachAsync(x => _tracks.CreateIfNotExists(x, true));
if (obj.ExternalIDs != null)
foreach (MetadataID entry in obj.ExternalIDs)
_database.Entry(entry).State = EntityState.Deleted;
await obj.Tracks.ForEachAsync(x => _tracks.Delete(x));
obj.ExternalIDs.ForEach(x => _database.Entry(x).State = EntityState.Deleted);
await _database.SaveChangesAsync();
}
}

View File

@ -110,7 +110,7 @@ namespace Kyoo.Controllers
throw new InvalidOperationException();
}
public override Task<LibraryItem> Edit(LibraryItem obj, bool reset) => throw new InvalidOperationException();
protected override Task Validate(LibraryItem resource) => throw new InvalidOperationException();
protected override Task EditRelations(LibraryItem _, LibraryItem _2) => throw new InvalidOperationException();
public override Task Delete(int id) => throw new InvalidOperationException();
public override Task Delete(string slug) => throw new InvalidOperationException();
public override Task Delete(LibraryItem obj) => throw new InvalidOperationException();

View File

@ -62,6 +62,14 @@ namespace Kyoo.Controllers
}
protected override async Task Validate(Library resource)
{
await base.Validate(resource);
resource.Providers = await resource.Providers
.SelectAsync(x => _providers.CreateIfNotExists(x, true))
.ToListAsync();
}
protected override Task EditRelations(Library resource, Library changed)
{
if (string.IsNullOrEmpty(resource.Slug))
throw new ArgumentException("The library's slug must be set and not empty");
@ -70,14 +78,7 @@ namespace Kyoo.Controllers
if (resource.Paths == null || !resource.Paths.Any())
throw new ArgumentException("The library should have a least one path.");
await base.Validate(resource);
if (resource.Providers != null)
{
resource.Providers = await resource.Providers
.SelectAsync(x => _providers.CreateIfNotExists(x, true))
.ToListAsync();
}
return base.EditRelations(resource, changed);
}
public override async Task Delete(Library obj)