diff --git a/back/src/Kyoo.Core/Controllers/Repositories/DapperHelper.cs b/back/src/Kyoo.Core/Controllers/Repositories/DapperHelper.cs index c692ee24..f98e84ce 100644 --- a/back/src/Kyoo.Core/Controllers/Repositories/DapperHelper.cs +++ b/back/src/Kyoo.Core/Controllers/Repositories/DapperHelper.cs @@ -431,20 +431,13 @@ public static class DapperHelper } } -public class SqlVariableContext +public class SqlVariableContext(IHttpContextAccessor accessor) { - private readonly IHttpContextAccessor _accessor; - - public SqlVariableContext(IHttpContextAccessor accessor) - { - _accessor = accessor; - } - public object? ReadVar(string var) { return var switch { - "current_user" => _accessor.HttpContext?.User.GetId(), + "current_user" => accessor.HttpContext?.User.GetId(), _ => throw new ArgumentException($"Invalid sql variable name: {var}") }; } diff --git a/back/src/Kyoo.Core/Controllers/Repositories/EpisodeRepository.cs b/back/src/Kyoo.Core/Controllers/Repositories/EpisodeRepository.cs index bea65d27..b89b3204 100644 --- a/back/src/Kyoo.Core/Controllers/Repositories/EpisodeRepository.cs +++ b/back/src/Kyoo.Core/Controllers/Repositories/EpisodeRepository.cs @@ -32,15 +32,12 @@ namespace Kyoo.Core.Controllers; /// /// A local repository to handle episodes. /// -public class EpisodeRepository : LocalRepository +public class EpisodeRepository( + DatabaseContext database, + IRepository shows, + IThumbnailsManager thumbs +) : LocalRepository(database, thumbs) { - /// - /// The database handle - /// - private readonly DatabaseContext _database; - - private readonly IRepository _shows; - static EpisodeRepository() { // Edit episode slugs when the show's slug changes. @@ -61,30 +58,13 @@ public class EpisodeRepository : LocalRepository }; } - /// - /// Create a new . - /// - /// The database handle to use. - /// A show repository - /// The thumbnail manager used to store images. - public EpisodeRepository( - DatabaseContext database, - IRepository shows, - IThumbnailsManager thumbs - ) - : base(database, thumbs) - { - _database = database; - _shows = shows; - } - /// public override async Task> Search( string query, Include? include = default ) { - return await AddIncludes(_database.Episodes, include) + return await AddIncludes(database.Episodes, include) .Where(x => EF.Functions.ILike(x.Name!, $"%{query}%")) .Take(20) .ToListAsync(); @@ -93,12 +73,12 @@ public class EpisodeRepository : LocalRepository protected override Task GetDuplicated(Episode item) { if (item is { SeasonNumber: not null, EpisodeNumber: not null }) - return _database.Episodes.FirstOrDefaultAsync(x => + return database.Episodes.FirstOrDefaultAsync(x => x.ShowId == item.ShowId && x.SeasonNumber == item.SeasonNumber && x.EpisodeNumber == item.EpisodeNumber ); - return _database.Episodes.FirstOrDefaultAsync(x => + return database.Episodes.FirstOrDefaultAsync(x => x.ShowId == item.ShowId && x.AbsoluteNumber == item.AbsoluteNumber ); } @@ -106,11 +86,10 @@ public class EpisodeRepository : LocalRepository /// public override async Task Create(Episode obj) { - obj.ShowSlug = - obj.Show?.Slug ?? (await _database.Shows.FirstAsync(x => x.Id == obj.ShowId)).Slug; + obj.ShowSlug = obj.Show?.Slug ?? (await shows.Get(obj.ShowId)).Slug; await base.Create(obj); - _database.Entry(obj).State = EntityState.Added; - await _database.SaveChangesAsync(() => GetDuplicated(obj)); + database.Entry(obj).State = EntityState.Added; + await database.SaveChangesAsync(() => GetDuplicated(obj)); await IRepository.OnResourceCreated(obj); return obj; } @@ -132,7 +111,7 @@ public class EpisodeRepository : LocalRepository } if (resource.SeasonId == null && resource.SeasonNumber != null) { - resource.Season = await _database.Seasons.FirstOrDefaultAsync(x => + resource.Season = await database.Seasons.FirstOrDefaultAsync(x => x.ShowId == resource.ShowId && x.SeasonNumber == resource.SeasonNumber ); } @@ -141,14 +120,14 @@ public class EpisodeRepository : LocalRepository /// public override async Task Delete(Episode obj) { - int epCount = await _database + int epCount = await database .Episodes.Where(x => x.ShowId == obj.ShowId) .Take(2) .CountAsync(); - _database.Entry(obj).State = EntityState.Deleted; - await _database.SaveChangesAsync(); + database.Entry(obj).State = EntityState.Deleted; + await database.SaveChangesAsync(); await base.Delete(obj); if (epCount == 1) - await _shows.Delete(obj.ShowId); + await shows.Delete(obj.ShowId); } } diff --git a/back/src/Kyoo.Core/Controllers/Repositories/WatchStatusRepository.cs b/back/src/Kyoo.Core/Controllers/Repositories/WatchStatusRepository.cs index 40162a38..e3137616 100644 --- a/back/src/Kyoo.Core/Controllers/Repositories/WatchStatusRepository.cs +++ b/back/src/Kyoo.Core/Controllers/Repositories/WatchStatusRepository.cs @@ -489,7 +489,7 @@ public class WatchStatusRepository( int? percent ) { - Episode episode = await database.Episodes.FirstAsync(x => x.Id == episodeId); + Episode episode = await episodes.Get(episodeId); if (percent == null && watchedTime != null && episode.Runtime > 0) percent = (int)Math.Round(watchedTime.Value / (episode.Runtime.Value * 60f) * 100f);