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

View File

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

View File

@ -110,7 +110,7 @@ namespace Kyoo.Controllers
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
public override Task<LibraryItem> Edit(LibraryItem obj, bool reset) => 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(int id) => throw new InvalidOperationException();
public override Task Delete(string slug) => throw new InvalidOperationException(); public override Task Delete(string slug) => throw new InvalidOperationException();
public override Task Delete(LibraryItem obj) => 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) 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)) if (string.IsNullOrEmpty(resource.Slug))
throw new ArgumentException("The library's slug must be set and not empty"); 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()) if (resource.Paths == null || !resource.Paths.Any())
throw new ArgumentException("The library should have a least one path."); throw new ArgumentException("The library should have a least one path.");
await base.Validate(resource); return base.EditRelations(resource, changed);
if (resource.Providers != null)
{
resource.Providers = await resource.Providers
.SelectAsync(x => _providers.CreateIfNotExists(x, true))
.ToListAsync();
}
} }
public override async Task Delete(Library obj) public override async Task Delete(Library obj)