mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Fix repository's relations handling
This commit is contained in:
parent
e846a1cc18
commit
00cd94d624
@ -18,6 +18,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
@ -79,22 +80,15 @@ public class EpisodeRepository(DatabaseContext database, IRepository<Show> shows
|
||||
protected override async Task Validate(Episode resource)
|
||||
{
|
||||
await base.Validate(resource);
|
||||
resource.Show = null;
|
||||
if (resource.ShowId == Guid.Empty)
|
||||
{
|
||||
if (resource.Show == null)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"Can't store an episode not related "
|
||||
+ $"to any show (showID: {resource.ShowId})."
|
||||
);
|
||||
}
|
||||
resource.ShowId = resource.Show.Id;
|
||||
}
|
||||
throw new ValidationException("Missing show id");
|
||||
resource.Season = null;
|
||||
if (resource.SeasonId == null && resource.SeasonNumber != null)
|
||||
{
|
||||
resource.Season = await Database.Seasons.FirstOrDefaultAsync(x =>
|
||||
resource.SeasonId = await Database.Seasons.Where(x =>
|
||||
x.ShowId == resource.ShowId && x.SeasonNumber == resource.SeasonNumber
|
||||
);
|
||||
).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
}
|
||||
await thumbnails.DownloadImages(resource);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
@ -268,7 +269,7 @@ public abstract class GenericRepository<T>(DatabaseContext database) : IReposito
|
||||
public virtual async Task<T> Create(T obj)
|
||||
{
|
||||
await Validate(obj);
|
||||
Database.Entry(obj).State = EntityState.Added;
|
||||
Database.Add(obj);
|
||||
await Database.SaveChangesAsync(() => Get(obj.Slug));
|
||||
await IRepository<T>.OnResourceCreated(obj);
|
||||
return obj;
|
||||
@ -295,7 +296,7 @@ public abstract class GenericRepository<T>(DatabaseContext database) : IReposito
|
||||
public virtual async Task<T> Edit(T edited)
|
||||
{
|
||||
await Validate(edited);
|
||||
Database.Entry(edited).State = EntityState.Modified;
|
||||
Database.Update(edited);
|
||||
await Database.SaveChangesAsync();
|
||||
await IRepository<T>.OnResourceEdited(edited);
|
||||
return edited;
|
||||
@ -323,7 +324,7 @@ public abstract class GenericRepository<T>(DatabaseContext database) : IReposito
|
||||
}
|
||||
}
|
||||
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <exception cref="ValidationException">
|
||||
/// You can throw this if the resource is illegal and should not be saved.
|
||||
/// </exception>
|
||||
protected virtual Task Validate(T resource)
|
||||
@ -334,9 +335,9 @@ public abstract class GenericRepository<T>(DatabaseContext database) : IReposito
|
||||
)
|
||||
return Task.CompletedTask;
|
||||
if (string.IsNullOrEmpty(resource.Slug))
|
||||
throw new ArgumentException("Resource can't have null as a slug.");
|
||||
throw new ValidationException("Resource can't have null as a slug.");
|
||||
if (resource.Slug == "random")
|
||||
throw new ArgumentException("Resources slug can't be the literal \"random\".");
|
||||
throw new ValidationException("Resources slug can't be the literal \"random\".");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,8 @@ namespace Kyoo.Core.Controllers;
|
||||
/// <summary>
|
||||
/// A local repository to handle library items.
|
||||
/// </summary>
|
||||
public class LibraryItemRepository : DapperRepository<ILibraryItem>
|
||||
public class LibraryItemRepository(DbConnection database, SqlVariableContext context)
|
||||
: DapperRepository<ILibraryItem>(database, context)
|
||||
{
|
||||
// language=PostgreSQL
|
||||
protected override FormattableString Sql =>
|
||||
@ -78,9 +79,6 @@ public class LibraryItemRepository : DapperRepository<ILibraryItem>
|
||||
throw new InvalidDataException();
|
||||
}
|
||||
|
||||
public LibraryItemRepository(DbConnection database, SqlVariableContext context)
|
||||
: base(database, context) { }
|
||||
|
||||
public async Task<ICollection<ILibraryItem>> GetAllOfCollection(
|
||||
Guid collectionId,
|
||||
Filter<ILibraryItem>? filter = default,
|
||||
|
@ -71,8 +71,8 @@ public class MovieRepository(
|
||||
await base.Validate(resource);
|
||||
if (resource.Studio != null)
|
||||
{
|
||||
resource.Studio = await studios.CreateIfNotExists(resource.Studio);
|
||||
resource.StudioId = resource.Studio.Id;
|
||||
resource.StudioId = (await studios.CreateIfNotExists(resource.Studio)).Id;
|
||||
resource.Studio = null;
|
||||
}
|
||||
await thumbnails.DownloadImages(resource);
|
||||
}
|
||||
|
@ -80,17 +80,9 @@ public class SeasonRepository(DatabaseContext database, IThumbnailsManager thumb
|
||||
protected override async Task Validate(Season resource)
|
||||
{
|
||||
await base.Validate(resource);
|
||||
resource.Show = null;
|
||||
if (resource.ShowId == Guid.Empty)
|
||||
{
|
||||
if (resource.Show == null)
|
||||
{
|
||||
throw new ValidationException(
|
||||
$"Can't store a season not related to any show "
|
||||
+ $"(showID: {resource.ShowId})."
|
||||
);
|
||||
}
|
||||
resource.ShowId = resource.Show.Id;
|
||||
}
|
||||
throw new ValidationException("Missing show id");
|
||||
await thumbnails.DownloadImages(resource);
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ public class ShowRepository(
|
||||
&& existing.StartAir?.Year != obj.StartAir?.Year
|
||||
)
|
||||
{
|
||||
obj.Slug = $"{obj.Slug}-{obj.StartAir!.Value.Year}";
|
||||
obj.Slug = $"{obj.Slug}-{obj.AirDate!.Value.Year}";
|
||||
return base.Create(obj);
|
||||
}
|
||||
}
|
||||
@ -71,8 +71,8 @@ public class ShowRepository(
|
||||
await base.Validate(resource);
|
||||
if (resource.Studio != null)
|
||||
{
|
||||
resource.Studio = await studios.CreateIfNotExists(resource.Studio);
|
||||
resource.StudioId = resource.Studio.Id;
|
||||
resource.StudioId = (await studios.CreateIfNotExists(resource.Studio)).Id;
|
||||
resource.Studio = null;
|
||||
}
|
||||
await thumbnails.DownloadImages(resource);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user