Update csharpier

This commit is contained in:
Zoe Roux 2024-02-03 14:51:37 +01:00
parent 042cc018cb
commit a5638203a6
46 changed files with 811 additions and 958 deletions

View File

@ -9,7 +9,7 @@
] ]
}, },
"csharpier": { "csharpier": {
"version": "0.26.4", "version": "0.27.2",
"commands": [ "commands": [
"dotnet-csharpier" "dotnet-csharpier"
] ]

View File

@ -202,13 +202,11 @@ namespace Kyoo.Abstractions.Models
private Episode? _PreviousEpisode => private Episode? _PreviousEpisode =>
Show! Show!
.Episodes! .Episodes!.OrderBy(x => x.AbsoluteNumber == null)
.OrderBy(x => x.AbsoluteNumber == null)
.ThenByDescending(x => x.AbsoluteNumber) .ThenByDescending(x => x.AbsoluteNumber)
.ThenByDescending(x => x.SeasonNumber) .ThenByDescending(x => x.SeasonNumber)
.ThenByDescending(x => x.EpisodeNumber) .ThenByDescending(x => x.EpisodeNumber)
.FirstOrDefault( .FirstOrDefault(x =>
x =>
x.AbsoluteNumber < AbsoluteNumber x.AbsoluteNumber < AbsoluteNumber
|| x.SeasonNumber < SeasonNumber || x.SeasonNumber < SeasonNumber
|| (x.SeasonNumber == SeasonNumber && x.EpisodeNumber < EpisodeNumber) || (x.SeasonNumber == SeasonNumber && x.EpisodeNumber < EpisodeNumber)
@ -242,12 +240,10 @@ namespace Kyoo.Abstractions.Models
private Episode? _NextEpisode => private Episode? _NextEpisode =>
Show! Show!
.Episodes! .Episodes!.OrderBy(x => x.AbsoluteNumber)
.OrderBy(x => x.AbsoluteNumber)
.ThenBy(x => x.SeasonNumber) .ThenBy(x => x.SeasonNumber)
.ThenBy(x => x.EpisodeNumber) .ThenBy(x => x.EpisodeNumber)
.FirstOrDefault( .FirstOrDefault(x =>
x =>
x.AbsoluteNumber > AbsoluteNumber x.AbsoluteNumber > AbsoluteNumber
|| x.SeasonNumber > SeasonNumber || x.SeasonNumber > SeasonNumber
|| (x.SeasonNumber == SeasonNumber && x.EpisodeNumber > EpisodeNumber) || (x.SeasonNumber == SeasonNumber && x.EpisodeNumber > EpisodeNumber)

View File

@ -205,8 +205,7 @@ public abstract record Filter<T> : Filter
if (type.IsEnum) if (type.IsEnum)
{ {
return Parse return Parse
.LetterOrDigit .LetterOrDigit.Many()
.Many()
.Text() .Text()
.Then(x => .Then(x =>
{ {
@ -259,13 +258,10 @@ public abstract record Filter<T> : Filter
} }
PropertyInfo? propInfo = types PropertyInfo? propInfo = types
.Select( .Select(x =>
x =>
x.GetProperty( x.GetProperty(
prop, prop,
BindingFlags.IgnoreCase BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.Instance
) )
) )
.FirstOrDefault(); .FirstOrDefault();

View File

@ -62,17 +62,14 @@ public class Include<T> : Include
.SelectMany(key => .SelectMany(key =>
{ {
var relations = types var relations = types
.Select( .Select(x =>
x =>
x.GetProperty( x.GetProperty(
key, key,
BindingFlags.IgnoreCase BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.Instance
)! )!
) )
.Select( .Select(prop =>
prop => (prop, attr: prop?.GetCustomAttribute<LoadableRelationAttribute>()!) (prop, attr: prop?.GetCustomAttribute<LoadableRelationAttribute>()!)
) )
.Where(x => x.prop != null && x.attr != null) .Where(x => x.prop != null && x.attr != null)
.ToList(); .ToList();

View File

@ -120,8 +120,7 @@ namespace Kyoo.Abstractions.Controllers
Type[] types = Type[] types =
typeof(T).GetCustomAttribute<OneOfAttribute>()?.Types ?? new[] { typeof(T) }; typeof(T).GetCustomAttribute<OneOfAttribute>()?.Types ?? new[] { typeof(T) };
PropertyInfo? property = types PropertyInfo? property = types
.Select( .Select(x =>
x =>
x.GetProperty( x.GetProperty(
key, key,
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance

View File

@ -60,8 +60,8 @@ namespace Kyoo.Utils
hasChanged = false; hasChanged = false;
if (second == null) if (second == null)
return first; return first;
hasChanged = second.Any( hasChanged = second.Any(x =>
x => !first.ContainsKey(x.Key) || x.Value?.Equals(first[x.Key]) == false !first.ContainsKey(x.Key) || x.Value?.Equals(first[x.Key]) == false
); );
foreach ((T key, T2 value) in first) foreach ((T key, T2 value) in first)
second.TryAdd(key, value); second.TryAdd(key, value);
@ -98,8 +98,7 @@ namespace Kyoo.Utils
Type type = typeof(T); Type type = typeof(T);
IEnumerable<PropertyInfo> properties = type.GetProperties() IEnumerable<PropertyInfo> properties = type.GetProperties()
.Where( .Where(x =>
x =>
x is { CanRead: true, CanWrite: true } x is { CanRead: true, CanWrite: true }
&& Attribute.GetCustomAttribute(x, typeof(NotMergeableAttribute)) == null && Attribute.GetCustomAttribute(x, typeof(NotMergeableAttribute)) == null
); );

View File

@ -206,8 +206,8 @@ namespace Kyoo.Utils
: type.GetInheritanceTree(); : type.GetInheritanceTree();
return types return types
.Prepend(type) .Prepend(type)
.FirstOrDefault( .FirstOrDefault(x =>
x => x.IsGenericType && x.GetGenericTypeDefinition() == genericType x.IsGenericType && x.GetGenericTypeDefinition() == genericType
); );
} }

View File

@ -229,11 +229,10 @@ namespace Kyoo.Authentication
private AuthenticateResult _ApiKeyCheck(ActionContext context) private AuthenticateResult _ApiKeyCheck(ActionContext context)
{ {
if ( if (
!context !context.HttpContext.Request.Headers.TryGetValue(
.HttpContext "X-API-Key",
.Request out StringValues apiKey
.Headers )
.TryGetValue("X-API-Key", out StringValues apiKey)
) )
return AuthenticateResult.NoResult(); return AuthenticateResult.NoResult();
if (!_options.ApiKeys.Contains<string>(apiKey!)) if (!_options.ApiKeys.Contains<string>(apiKey!))
@ -262,9 +261,9 @@ namespace Kyoo.Authentication
private async Task<AuthenticateResult> _JwtCheck(ActionContext context) private async Task<AuthenticateResult> _JwtCheck(ActionContext context)
{ {
AuthenticateResult ret = await context AuthenticateResult ret = await context.HttpContext.AuthenticateAsync(
.HttpContext JwtBearerDefaults.AuthenticationScheme
.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme); );
// Change the failure message to make the API nice to use. // Change the failure message to make the API nice to use.
if (ret.Failure != null) if (ret.Failure != null)
return AuthenticateResult.Fail( return AuthenticateResult.Fail(

View File

@ -40,10 +40,8 @@ namespace Kyoo.Authentication.Models
get get
{ {
return Enum.GetNames<Group>() return Enum.GetNames<Group>()
.SelectMany( .SelectMany(group =>
group => Enum.GetNames<Kind>().Select(kind => $"{group}.{kind}".ToLowerInvariant())
Enum.GetNames<Kind>()
.Select(kind => $"{group}.{kind}".ToLowerInvariant())
) )
.ToArray(); .ToArray();
} }

View File

@ -65,8 +65,7 @@ public static class DapperHelper
.Where(x => !x.Key.StartsWith('_')) .Where(x => !x.Key.StartsWith('_'))
// If first char is lower, assume manual sql instead of reflection. // If first char is lower, assume manual sql instead of reflection.
.Where(x => char.IsLower(key.First()) || x.Value.GetProperty(key) != null) .Where(x => char.IsLower(key.First()) || x.Value.GetProperty(key) != null)
.Select( .Select(x =>
x =>
$"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}" $"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}"
) )
.ToArray(); .ToArray();
@ -149,8 +148,7 @@ public static class DapperHelper
T Map(T item, IEnumerable<object?> relations) T Map(T item, IEnumerable<object?> relations)
{ {
IEnumerable<string> metadatas = include IEnumerable<string> metadatas = include
.Metadatas .Metadatas.Where(x => x is not Include.ProjectedRelation)
.Where(x => x is not Include.ProjectedRelation)
.Select(x => x.Name); .Select(x => x.Name);
foreach ((string name, object? value) in metadatas.Zip(relations)) foreach ((string name, object? value) in metadatas.Zip(relations))
{ {
@ -179,8 +177,7 @@ public static class DapperHelper
'\n', '\n',
config config
.Skip(1) .Skip(1)
.Select( .Select(x =>
x =>
$"when {x.Key}.id is not null then '{x.Value.Name.ToLowerInvariant()}'" $"when {x.Key}.id is not null then '{x.Value.Name.ToLowerInvariant()}'"
) )
); );
@ -196,8 +193,7 @@ public static class DapperHelper
.Where(x => !x.Key.StartsWith('_')) .Where(x => !x.Key.StartsWith('_'))
// If first char is lower, assume manual sql instead of reflection. // If first char is lower, assume manual sql instead of reflection.
.Where(x => char.IsLower(key.First()) || x.Value.GetProperty(key) != null) .Where(x => char.IsLower(key.First()) || x.Value.GetProperty(key) != null)
.Select( .Select(x =>
x =>
$"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}" $"{x.Key}.{x.Value.GetProperty(key)?.GetCustomAttribute<ColumnAttribute>()?.Name ?? key.ToSnakeCase()}"
); );
@ -245,8 +241,7 @@ public static class DapperHelper
public static string ExpendProjections(Type type, string? prefix, Include include) public static string ExpendProjections(Type type, string? prefix, Include include)
{ {
IEnumerable<string> projections = include IEnumerable<string> projections = include
.Metadatas .Metadatas.Select(x => (x as Include.ProjectedRelation)!)
.Select(x => (x as Include.ProjectedRelation)!)
.Where(x => x != null) .Where(x => x != null)
.Where(x => type.GetProperty(x.Name) != null) .Where(x => type.GetProperty(x.Name) != null)
.Select(x => x.Sql.Replace("\"this\".", prefix)); .Select(x => x.Sql.Replace("\"this\".", prefix));
@ -336,8 +331,8 @@ public static class DapperHelper
{ {
string posterProj = string.Join( string posterProj = string.Join(
", ", ", ",
new[] { "poster", "thumbnail", "logo" }.Select( new[] { "poster", "thumbnail", "logo" }.Select(x =>
x => $"{prefix}{x}_source as source, {prefix}{x}_blurhash as blurhash" $"{prefix}{x}_source as source, {prefix}{x}_blurhash as blurhash"
) )
); );
projection = string.IsNullOrEmpty(projection) projection = string.IsNullOrEmpty(projection)

View File

@ -47,12 +47,10 @@ namespace Kyoo.Core.Controllers
IRepository<Show>.OnEdited += async (show) => IRepository<Show>.OnEdited += async (show) =>
{ {
await using AsyncServiceScope scope = CoreModule.Services.CreateAsyncScope(); await using AsyncServiceScope scope = CoreModule.Services.CreateAsyncScope();
DatabaseContext database = scope DatabaseContext database =
.ServiceProvider scope.ServiceProvider.GetRequiredService<DatabaseContext>();
.GetRequiredService<DatabaseContext>();
List<Episode> episodes = await database List<Episode> episodes = await database
.Episodes .Episodes.AsTracking()
.AsTracking()
.Where(x => x.ShowId == show.Id) .Where(x => x.ShowId == show.Id)
.ToListAsync(); .ToListAsync();
foreach (Episode ep in episodes) foreach (Episode ep in episodes)
@ -96,18 +94,13 @@ namespace Kyoo.Core.Controllers
protected override Task<Episode?> GetDuplicated(Episode item) protected override Task<Episode?> GetDuplicated(Episode item)
{ {
if (item is { SeasonNumber: not null, EpisodeNumber: not null }) if (item is { SeasonNumber: not null, EpisodeNumber: not null })
return _database return _database.Episodes.FirstOrDefaultAsync(x =>
.Episodes
.FirstOrDefaultAsync(
x =>
x.ShowId == item.ShowId x.ShowId == item.ShowId
&& x.SeasonNumber == item.SeasonNumber && x.SeasonNumber == item.SeasonNumber
&& x.EpisodeNumber == item.EpisodeNumber && x.EpisodeNumber == item.EpisodeNumber
); );
return _database return _database.Episodes.FirstOrDefaultAsync(x =>
.Episodes x.ShowId == item.ShowId && x.AbsoluteNumber == item.AbsoluteNumber
.FirstOrDefaultAsync(
x => x.ShowId == item.ShowId && x.AbsoluteNumber == item.AbsoluteNumber
); );
} }
@ -140,10 +133,8 @@ namespace Kyoo.Core.Controllers
} }
if (resource.SeasonId == null && resource.SeasonNumber != null) if (resource.SeasonId == null && resource.SeasonNumber != null)
{ {
resource.Season = await _database resource.Season = await _database.Seasons.FirstOrDefaultAsync(x =>
.Seasons x.ShowId == resource.ShowId && x.SeasonNumber == resource.SeasonNumber
.FirstOrDefaultAsync(
x => x.ShowId == resource.ShowId && x.SeasonNumber == resource.SeasonNumber
); );
} }
} }
@ -152,8 +143,7 @@ namespace Kyoo.Core.Controllers
public override async Task Delete(Episode obj) public override async Task Delete(Episode obj)
{ {
int epCount = await _database int epCount = await _database
.Episodes .Episodes.Where(x => x.ShowId == obj.ShowId)
.Where(x => x.ShowId == obj.ShowId)
.Take(2) .Take(2)
.CountAsync(); .CountAsync();
_database.Entry(obj).State = EntityState.Deleted; _database.Entry(obj).State = EntityState.Deleted;

View File

@ -47,12 +47,10 @@ namespace Kyoo.Core.Controllers
IRepository<Show>.OnEdited += async (show) => IRepository<Show>.OnEdited += async (show) =>
{ {
await using AsyncServiceScope scope = CoreModule.Services.CreateAsyncScope(); await using AsyncServiceScope scope = CoreModule.Services.CreateAsyncScope();
DatabaseContext database = scope DatabaseContext database =
.ServiceProvider scope.ServiceProvider.GetRequiredService<DatabaseContext>();
.GetRequiredService<DatabaseContext>();
List<Season> seasons = await database List<Season> seasons = await database
.Seasons .Seasons.AsTracking()
.AsTracking()
.Where(x => x.ShowId == show.Id) .Where(x => x.ShowId == show.Id)
.ToListAsync(); .ToListAsync();
foreach (Season season in seasons) foreach (Season season in seasons)
@ -77,10 +75,8 @@ namespace Kyoo.Core.Controllers
protected override Task<Season?> GetDuplicated(Season item) protected override Task<Season?> GetDuplicated(Season item)
{ {
return _database return _database.Seasons.FirstOrDefaultAsync(x =>
.Seasons x.ShowId == item.ShowId && x.SeasonNumber == item.SeasonNumber
.FirstOrDefaultAsync(
x => x.ShowId == item.ShowId && x.SeasonNumber == item.SeasonNumber
); );
} }

View File

@ -66,11 +66,10 @@ public class WatchStatusRepository : IWatchStatusRepository
{ {
await using AsyncServiceScope scope = CoreModule.Services.CreateAsyncScope(); await using AsyncServiceScope scope = CoreModule.Services.CreateAsyncScope();
DatabaseContext db = scope.ServiceProvider.GetRequiredService<DatabaseContext>(); DatabaseContext db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
WatchStatusRepository repo = scope WatchStatusRepository repo =
.ServiceProvider scope.ServiceProvider.GetRequiredService<WatchStatusRepository>();
.GetRequiredService<WatchStatusRepository>(); List<Guid> users = await db
List<Guid> users = await db.ShowWatchStatus .ShowWatchStatus.IgnoreQueryFilters()
.IgnoreQueryFilters()
.Where(x => x.ShowId == ep.ShowId && x.Status == WatchStatus.Completed) .Where(x => x.ShowId == ep.ShowId && x.Status == WatchStatus.Completed)
.Select(x => x.UserId) .Select(x => x.UserId)
.ToListAsync(); .ToListAsync();
@ -189,8 +188,7 @@ public class WatchStatusRepository : IWatchStatusRepository
{ {
if (include != null) if (include != null)
include.Metadatas = include include.Metadatas = include
.Metadatas .Metadatas.Where(x => x.Name != nameof(Show.WatchStatus))
.Where(x => x.Name != nameof(Show.WatchStatus))
.ToList(); .ToList();
// We can't use the generic after id hanler since the sort depends on a relation. // We can't use the generic after id hanler since the sort depends on a relation.
@ -226,9 +224,9 @@ public class WatchStatusRepository : IWatchStatusRepository
/// <inheritdoc /> /// <inheritdoc />
public Task<MovieWatchStatus?> GetMovieStatus(Guid movieId, Guid userId) public Task<MovieWatchStatus?> GetMovieStatus(Guid movieId, Guid userId)
{ {
return _database return _database.MovieWatchStatus.FirstOrDefaultAsync(x =>
.MovieWatchStatus x.MovieId == movieId && x.UserId == userId
.FirstOrDefaultAsync(x => x.MovieId == movieId && x.UserId == userId); );
} }
/// <inheritdoc /> /// <inheritdoc />
@ -277,8 +275,7 @@ public class WatchStatusRepository : IWatchStatusRepository
PlayedDate = status == WatchStatus.Completed ? DateTime.UtcNow : null, PlayedDate = status == WatchStatus.Completed ? DateTime.UtcNow : null,
}; };
await _database await _database
.MovieWatchStatus .MovieWatchStatus.Upsert(ret)
.Upsert(ret)
.UpdateIf(x => status != Watching || x.Status != Completed) .UpdateIf(x => status != Watching || x.Status != Completed)
.RunAsync(); .RunAsync();
return ret; return ret;
@ -288,17 +285,16 @@ public class WatchStatusRepository : IWatchStatusRepository
public async Task DeleteMovieStatus(Guid movieId, Guid userId) public async Task DeleteMovieStatus(Guid movieId, Guid userId)
{ {
await _database await _database
.MovieWatchStatus .MovieWatchStatus.Where(x => x.MovieId == movieId && x.UserId == userId)
.Where(x => x.MovieId == movieId && x.UserId == userId)
.ExecuteDeleteAsync(); .ExecuteDeleteAsync();
} }
/// <inheritdoc /> /// <inheritdoc />
public Task<ShowWatchStatus?> GetShowStatus(Guid showId, Guid userId) public Task<ShowWatchStatus?> GetShowStatus(Guid showId, Guid userId)
{ {
return _database return _database.ShowWatchStatus.FirstOrDefaultAsync(x =>
.ShowWatchStatus x.ShowId == showId && x.UserId == userId
.FirstOrDefaultAsync(x => x.ShowId == showId && x.UserId == userId); );
} }
/// <inheritdoc /> /// <inheritdoc />
@ -315,12 +311,9 @@ public class WatchStatusRepository : IWatchStatusRepository
int unseenEpisodeCount = int unseenEpisodeCount =
status != WatchStatus.Completed status != WatchStatus.Completed
? await _database ? await _database
.Episodes .Episodes.Where(x => x.ShowId == showId)
.Where(x => x.ShowId == showId) .Where(x =>
.Where( x.Watched!.First(x => x.UserId == userId)!.Status != WatchStatus.Completed
x =>
x.Watched!.First(x => x.UserId == userId)!.Status
!= WatchStatus.Completed
) )
.CountAsync() .CountAsync()
: 0; : 0;
@ -332,25 +325,20 @@ public class WatchStatusRepository : IWatchStatusRepository
if (status == WatchStatus.Watching) if (status == WatchStatus.Watching)
{ {
var cursor = await _database var cursor = await _database
.Episodes .Episodes.IgnoreQueryFilters()
.IgnoreQueryFilters()
.Where(x => x.ShowId == showId) .Where(x => x.ShowId == showId)
.OrderByDescending(x => x.AbsoluteNumber) .OrderByDescending(x => x.AbsoluteNumber)
.ThenByDescending(x => x.SeasonNumber) .ThenByDescending(x => x.SeasonNumber)
.ThenByDescending(x => x.EpisodeNumber) .ThenByDescending(x => x.EpisodeNumber)
.Select( .Select(x => new
x =>
new
{ {
x.Id, x.Id,
x.AbsoluteNumber, x.AbsoluteNumber,
x.SeasonNumber, x.SeasonNumber,
x.EpisodeNumber, x.EpisodeNumber,
Status = x.Watched!.First(x => x.UserId == userId) Status = x.Watched!.First(x => x.UserId == userId)
} })
) .FirstOrDefaultAsync(x =>
.FirstOrDefaultAsync(
x =>
x.Status.Status == WatchStatus.Completed x.Status.Status == WatchStatus.Completed
|| x.Status.Status == WatchStatus.Watching || x.Status.Status == WatchStatus.Watching
); );
@ -359,14 +347,12 @@ public class WatchStatusRepository : IWatchStatusRepository
cursor?.Status.Status == WatchStatus.Watching cursor?.Status.Status == WatchStatus.Watching
? cursor.Id ? cursor.Id
: await _database : await _database
.Episodes .Episodes.IgnoreQueryFilters()
.IgnoreQueryFilters()
.Where(x => x.ShowId == showId) .Where(x => x.ShowId == showId)
.OrderBy(x => x.AbsoluteNumber) .OrderBy(x => x.AbsoluteNumber)
.ThenBy(x => x.SeasonNumber) .ThenBy(x => x.SeasonNumber)
.ThenBy(x => x.EpisodeNumber) .ThenBy(x => x.EpisodeNumber)
.Where( .Where(x =>
x =>
cursor == null cursor == null
|| x.AbsoluteNumber > cursor.AbsoluteNumber || x.AbsoluteNumber > cursor.AbsoluteNumber
|| x.SeasonNumber > cursor.SeasonNumber || x.SeasonNumber > cursor.SeasonNumber
@ -375,14 +361,11 @@ public class WatchStatusRepository : IWatchStatusRepository
&& x.EpisodeNumber > cursor.EpisodeNumber && x.EpisodeNumber > cursor.EpisodeNumber
) )
) )
.Select( .Select(x => new
x =>
new
{ {
x.Id, x.Id,
Status = x.Watched!.FirstOrDefault(x => x.UserId == userId) Status = x.Watched!.FirstOrDefault(x => x.UserId == userId)
} })
)
.Where(x => x.Status == null || x.Status.Status != WatchStatus.Completed) .Where(x => x.Status == null || x.Status.Status != WatchStatus.Completed)
// The as Guid? is here to add the nullability status of the queryable. // The as Guid? is here to add the nullability status of the queryable.
// Without this, FirstOrDefault returns new Guid() when no result is found (which is 16 0s and invalid in sql). // Without this, FirstOrDefault returns new Guid() when no result is found (which is 16 0s and invalid in sql).
@ -392,24 +375,19 @@ public class WatchStatusRepository : IWatchStatusRepository
else if (status == WatchStatus.Completed) else if (status == WatchStatus.Completed)
{ {
List<Guid> episodes = await _database List<Guid> episodes = await _database
.Episodes .Episodes.Where(x => x.ShowId == showId)
.Where(x => x.ShowId == showId)
.Select(x => x.Id) .Select(x => x.Id)
.ToListAsync(); .ToListAsync();
await _database await _database
.EpisodeWatchStatus .EpisodeWatchStatus.UpsertRange(
.UpsertRange( episodes.Select(episodeId => new EpisodeWatchStatus
episodes.Select(
episodeId =>
new EpisodeWatchStatus
{ {
UserId = userId, UserId = userId,
EpisodeId = episodeId, EpisodeId = episodeId,
Status = WatchStatus.Completed, Status = WatchStatus.Completed,
AddedDate = DateTime.UtcNow, AddedDate = DateTime.UtcNow,
PlayedDate = DateTime.UtcNow PlayedDate = DateTime.UtcNow
} })
)
) )
.UpdateIf(x => x.Status == Watching || x.Status == Planned) .UpdateIf(x => x.Status == Watching || x.Status == Planned)
.RunAsync(); .RunAsync();
@ -435,8 +413,7 @@ public class WatchStatusRepository : IWatchStatusRepository
PlayedDate = status == WatchStatus.Completed ? DateTime.UtcNow : null, PlayedDate = status == WatchStatus.Completed ? DateTime.UtcNow : null,
}; };
await _database await _database
.ShowWatchStatus .ShowWatchStatus.Upsert(ret)
.Upsert(ret)
.UpdateIf(x => status != Watching || x.Status != Completed || newEpisode) .UpdateIf(x => status != Watching || x.Status != Completed || newEpisode)
.RunAsync(); .RunAsync();
return ret; return ret;
@ -446,22 +423,20 @@ public class WatchStatusRepository : IWatchStatusRepository
public async Task DeleteShowStatus(Guid showId, Guid userId) public async Task DeleteShowStatus(Guid showId, Guid userId)
{ {
await _database await _database
.ShowWatchStatus .ShowWatchStatus.IgnoreAutoIncludes()
.IgnoreAutoIncludes()
.Where(x => x.ShowId == showId && x.UserId == userId) .Where(x => x.ShowId == showId && x.UserId == userId)
.ExecuteDeleteAsync(); .ExecuteDeleteAsync();
await _database await _database
.EpisodeWatchStatus .EpisodeWatchStatus.Where(x => x.Episode.ShowId == showId && x.UserId == userId)
.Where(x => x.Episode.ShowId == showId && x.UserId == userId)
.ExecuteDeleteAsync(); .ExecuteDeleteAsync();
} }
/// <inheritdoc /> /// <inheritdoc />
public Task<EpisodeWatchStatus?> GetEpisodeStatus(Guid episodeId, Guid userId) public Task<EpisodeWatchStatus?> GetEpisodeStatus(Guid episodeId, Guid userId)
{ {
return _database return _database.EpisodeWatchStatus.FirstOrDefaultAsync(x =>
.EpisodeWatchStatus x.EpisodeId == episodeId && x.UserId == userId
.FirstOrDefaultAsync(x => x.EpisodeId == episodeId && x.UserId == userId); );
} }
/// <inheritdoc /> /// <inheritdoc />
@ -510,8 +485,7 @@ public class WatchStatusRepository : IWatchStatusRepository
PlayedDate = status == WatchStatus.Completed ? DateTime.UtcNow : null, PlayedDate = status == WatchStatus.Completed ? DateTime.UtcNow : null,
}; };
await _database await _database
.EpisodeWatchStatus .EpisodeWatchStatus.Upsert(ret)
.Upsert(ret)
.UpdateIf(x => status != Watching || x.Status != Completed) .UpdateIf(x => status != Watching || x.Status != Completed)
.RunAsync(); .RunAsync();
await SetShowStatus(episode.ShowId, userId, WatchStatus.Watching); await SetShowStatus(episode.ShowId, userId, WatchStatus.Watching);
@ -522,8 +496,7 @@ public class WatchStatusRepository : IWatchStatusRepository
public async Task DeleteEpisodeStatus(Guid episodeId, Guid userId) public async Task DeleteEpisodeStatus(Guid episodeId, Guid userId)
{ {
await _database await _database
.EpisodeWatchStatus .EpisodeWatchStatus.Where(x => x.EpisodeId == episodeId && x.UserId == userId)
.Where(x => x.EpisodeId == episodeId && x.UserId == userId)
.ExecuteDeleteAsync(); .ExecuteDeleteAsync();
} }
} }

View File

@ -212,8 +212,7 @@ namespace Kyoo.Core.Controllers
{ {
IEnumerable<string> images = new[] { "poster", "thumbnail", "logo" } IEnumerable<string> images = new[] { "poster", "thumbnail", "logo" }
.SelectMany(x => _GetBaseImagePath(item, x)) .SelectMany(x => _GetBaseImagePath(item, x))
.SelectMany( .SelectMany(x =>
x =>
new[] new[]
{ {
ImageQuality.High.ToString().ToLowerInvariant(), ImageQuality.High.ToString().ToLowerInvariant(),

View File

@ -105,8 +105,8 @@ namespace Kyoo.Core
options.SuppressMapClientErrors = true; options.SuppressMapClientErrors = true;
options.InvalidModelStateResponseFactory = ctx => options.InvalidModelStateResponseFactory = ctx =>
{ {
string[] errors = ctx.ModelState string[] errors = ctx
.SelectMany(x => x.Value!.Errors) .ModelState.SelectMany(x => x.Value!.Errors)
.Select(x => x.ErrorMessage) .Select(x => x.ErrorMessage)
.ToArray(); .ToArray();
return new BadRequestObjectResult(new RequestError(errors)); return new BadRequestObjectResult(new RequestError(errors));

View File

@ -45,9 +45,7 @@ namespace Kyoo.Core.Api
protected Page<TResult> Page<TResult>(ICollection<TResult> resources, int limit) protected Page<TResult> Page<TResult>(ICollection<TResult> resources, int limit)
where TResult : IResource where TResult : IResource
{ {
Dictionary<string, string> query = Request Dictionary<string, string> query = Request.Query.ToDictionary(
.Query
.ToDictionary(
x => x.Key, x => x.Key,
x => x.Value.ToString(), x => x.Value.ToString(),
StringComparer.InvariantCultureIgnoreCase StringComparer.InvariantCultureIgnoreCase
@ -66,9 +64,7 @@ namespace Kyoo.Core.Api
protected SearchPage<TResult> SearchPage<TResult>(SearchPage<TResult>.SearchResult result) protected SearchPage<TResult> SearchPage<TResult>(SearchPage<TResult>.SearchResult result)
where TResult : IResource where TResult : IResource
{ {
Dictionary<string, string> query = Request Dictionary<string, string> query = Request.Query.ToDictionary(
.Query
.ToDictionary(
x => x.Key, x => x.Key,
x => x.Value.ToString(), x => x.Value.ToString(),
StringComparer.InvariantCultureIgnoreCase StringComparer.InvariantCultureIgnoreCase

View File

@ -28,14 +28,13 @@ public class FilterBinder : IModelBinder
{ {
public Task BindModelAsync(ModelBindingContext bindingContext) public Task BindModelAsync(ModelBindingContext bindingContext)
{ {
ValueProviderResult fields = bindingContext ValueProviderResult fields = bindingContext.ValueProvider.GetValue(
.ValueProvider bindingContext.FieldName
.GetValue(bindingContext.FieldName); );
try try
{ {
object? filter = bindingContext object? filter = bindingContext
.ModelType .ModelType.GetMethod(nameof(Filter<object>.From))!
.GetMethod(nameof(Filter<object>.From))!
.Invoke(null, new object?[] { fields.FirstValue }); .Invoke(null, new object?[] { fields.FirstValue });
bindingContext.Result = ModelBindingResult.Success(filter); bindingContext.Result = ModelBindingResult.Success(filter);
return Task.CompletedTask; return Task.CompletedTask;

View File

@ -31,14 +31,13 @@ public class IncludeBinder : IModelBinder
public Task BindModelAsync(ModelBindingContext bindingContext) public Task BindModelAsync(ModelBindingContext bindingContext)
{ {
ValueProviderResult fields = bindingContext ValueProviderResult fields = bindingContext.ValueProvider.GetValue(
.ValueProvider bindingContext.FieldName
.GetValue(bindingContext.FieldName); );
try try
{ {
object include = bindingContext object include = bindingContext
.ModelType .ModelType.GetMethod(nameof(Include<object>.From))!
.GetMethod(nameof(Include<object>.From))!
.Invoke(null, new object?[] { fields.FirstValue })!; .Invoke(null, new object?[] { fields.FirstValue })!;
bindingContext.Result = ModelBindingResult.Success(include); bindingContext.Result = ModelBindingResult.Success(include);
bindingContext.HttpContext.Items["fields"] = ((dynamic)include).Fields; bindingContext.HttpContext.Items["fields"] = ((dynamic)include).Fields;

View File

@ -32,9 +32,9 @@ public class SortBinder : IModelBinder
public Task BindModelAsync(ModelBindingContext bindingContext) public Task BindModelAsync(ModelBindingContext bindingContext)
{ {
ValueProviderResult sortBy = bindingContext ValueProviderResult sortBy = bindingContext.ValueProvider.GetValue(
.ValueProvider bindingContext.FieldName
.GetValue(bindingContext.FieldName); );
uint seed = BitConverter.ToUInt32( uint seed = BitConverter.ToUInt32(
BitConverter.GetBytes(_rng.Next(int.MinValue, int.MaxValue)), BitConverter.GetBytes(_rng.Next(int.MinValue, int.MaxValue)),
0 0
@ -42,8 +42,7 @@ public class SortBinder : IModelBinder
try try
{ {
object sort = bindingContext object sort = bindingContext
.ModelType .ModelType.GetMethod(nameof(Sort<Movie>.From))!
.GetMethod(nameof(Sort<Movie>.From))!
.Invoke(null, new object?[] { sortBy.FirstValue, seed })!; .Invoke(null, new object?[] { sortBy.FirstValue, seed })!;
bindingContext.Result = ModelBindingResult.Success(sort); bindingContext.Result = ModelBindingResult.Success(sort);
bindingContext.HttpContext.Items["seed"] = seed; bindingContext.HttpContext.Items["seed"] = seed;

View File

@ -85,13 +85,8 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Show> fields [FromQuery] Include<Show> fields
) )
{ {
ICollection<Show> resources = await _libraryManager ICollection<Show> resources = await _libraryManager.Shows.GetAll(
.Shows Filter.And(filter, identifier.Matcher<Show>(x => x.StudioId, x => x.Studio!.Slug)),
.GetAll(
Filter.And(
filter,
identifier.Matcher<Show>(x => x.StudioId, x => x.Studio!.Slug)
),
sortBy, sortBy,
fields, fields,
pagination pagination

View File

@ -200,13 +200,8 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Show>? fields [FromQuery] Include<Show>? fields
) )
{ {
ICollection<Show> resources = await _libraryManager ICollection<Show> resources = await _libraryManager.Shows.GetAll(
.Shows Filter.And(filter, identifier.IsContainedIn<Show, Collection>(x => x.Collections)),
.GetAll(
Filter.And(
filter,
identifier.IsContainedIn<Show, Collection>(x => x.Collections)
),
sortBy == new Sort<Show>.Default() ? new Sort<Show>.By(x => x.AirDate) : sortBy, sortBy == new Sort<Show>.Default() ? new Sort<Show>.By(x => x.AirDate) : sortBy,
fields, fields,
pagination pagination
@ -249,16 +244,9 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Movie>? fields [FromQuery] Include<Movie>? fields
) )
{ {
ICollection<Movie> resources = await _libraryManager ICollection<Movie> resources = await _libraryManager.Movies.GetAll(
.Movies Filter.And(filter, identifier.IsContainedIn<Movie, Collection>(x => x.Collections)),
.GetAll( sortBy == new Sort<Movie>.Default() ? new Sort<Movie>.By(x => x.AirDate) : sortBy,
Filter.And(
filter,
identifier.IsContainedIn<Movie, Collection>(x => x.Collections)
),
sortBy == new Sort<Movie>.Default()
? new Sort<Movie>.By(x => x.AirDate)
: sortBy,
fields, fields,
pagination pagination
); );

View File

@ -77,9 +77,10 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Show> fields [FromQuery] Include<Show> fields
) )
{ {
return await _libraryManager return await _libraryManager.Shows.Get(
.Shows identifier.IsContainedIn<Show, Episode>(x => x.Episodes!),
.Get(identifier.IsContainedIn<Show, Episode>(x => x.Episodes!), fields); fields
);
} }
/// <summary> /// <summary>
@ -103,9 +104,10 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Season> fields [FromQuery] Include<Season> fields
) )
{ {
Season? ret = await _libraryManager Season? ret = await _libraryManager.Seasons.GetOrDefault(
.Seasons identifier.IsContainedIn<Season, Episode>(x => x.Episodes!),
.GetOrDefault(identifier.IsContainedIn<Season, Episode>(x => x.Episodes!), fields); fields
);
if (ret != null) if (ret != null)
return ret; return ret;
Episode? episode = await identifier.Match( Episode? episode = await identifier.Match(
@ -170,9 +172,13 @@ namespace Kyoo.Core.Api
id => Task.FromResult(id), id => Task.FromResult(id),
async slug => (await _libraryManager.Episodes.Get(slug)).Id async slug => (await _libraryManager.Episodes.Get(slug)).Id
); );
return await _libraryManager return await _libraryManager.WatchStatus.SetEpisodeStatus(
.WatchStatus id,
.SetEpisodeStatus(id, User.GetIdOrThrow(), status, watchedTime, percent); User.GetIdOrThrow(),
status,
watchedTime,
percent
);
} }
/// <summary> /// <summary>

View File

@ -113,9 +113,10 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Studio> fields [FromQuery] Include<Studio> fields
) )
{ {
return await _libraryManager return await _libraryManager.Studios.Get(
.Studios identifier.IsContainedIn<Studio, Movie>(x => x.Movies!),
.Get(identifier.IsContainedIn<Studio, Movie>(x => x.Movies!), fields); fields
);
} }
/// <summary> /// <summary>
@ -146,9 +147,7 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Collection> fields [FromQuery] Include<Collection> fields
) )
{ {
ICollection<Collection> resources = await _libraryManager ICollection<Collection> resources = await _libraryManager.Collections.GetAll(
.Collections
.GetAll(
Filter.And(filter, identifier.IsContainedIn<Collection, Movie>(x => x.Movies)), Filter.And(filter, identifier.IsContainedIn<Collection, Movie>(x => x.Movies)),
sortBy, sortBy,
fields, fields,
@ -219,9 +218,13 @@ namespace Kyoo.Core.Api
id => Task.FromResult(id), id => Task.FromResult(id),
async slug => (await _libraryManager.Movies.Get(slug)).Id async slug => (await _libraryManager.Movies.Get(slug)).Id
); );
return await _libraryManager return await _libraryManager.WatchStatus.SetMovieStatus(
.WatchStatus id,
.SetMovieStatus(id, User.GetIdOrThrow(), status, watchedTime, percent); User.GetIdOrThrow(),
status,
watchedTime,
percent
);
} }
/// <summary> /// <summary>

View File

@ -86,9 +86,7 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Episode> fields [FromQuery] Include<Episode> fields
) )
{ {
ICollection<Episode> resources = await _libraryManager ICollection<Episode> resources = await _libraryManager.Episodes.GetAll(
.Episodes
.GetAll(
Filter.And( Filter.And(
filter, filter,
identifier.Matcher<Episode>(x => x.SeasonId, x => x.Season!.Slug) identifier.Matcher<Episode>(x => x.SeasonId, x => x.Season!.Slug)
@ -125,9 +123,10 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Show> fields [FromQuery] Include<Show> fields
) )
{ {
Show? ret = await _libraryManager Show? ret = await _libraryManager.Shows.GetOrDefault(
.Shows identifier.IsContainedIn<Show, Season>(x => x.Seasons!),
.GetOrDefault(identifier.IsContainedIn<Show, Season>(x => x.Seasons!), fields); fields
);
if (ret == null) if (ret == null)
return NotFound(); return NotFound();
return ret; return ret;

View File

@ -88,13 +88,8 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Season> fields [FromQuery] Include<Season> fields
) )
{ {
ICollection<Season> resources = await _libraryManager ICollection<Season> resources = await _libraryManager.Seasons.GetAll(
.Seasons Filter.And(filter, identifier.Matcher<Season>(x => x.ShowId, x => x.Show!.Slug)),
.GetAll(
Filter.And(
filter,
identifier.Matcher<Season>(x => x.ShowId, x => x.Show!.Slug)
),
sortBy, sortBy,
fields, fields,
pagination pagination
@ -136,13 +131,8 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Episode> fields [FromQuery] Include<Episode> fields
) )
{ {
ICollection<Episode> resources = await _libraryManager ICollection<Episode> resources = await _libraryManager.Episodes.GetAll(
.Episodes Filter.And(filter, identifier.Matcher<Episode>(x => x.ShowId, x => x.Show!.Slug)),
.GetAll(
Filter.And(
filter,
identifier.Matcher<Episode>(x => x.ShowId, x => x.Show!.Slug)
),
sortBy, sortBy,
fields, fields,
pagination pagination
@ -210,9 +200,10 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Studio> fields [FromQuery] Include<Studio> fields
) )
{ {
return await _libraryManager return await _libraryManager.Studios.Get(
.Studios identifier.IsContainedIn<Studio, Show>(x => x.Shows!),
.Get(identifier.IsContainedIn<Studio, Show>(x => x.Shows!), fields); fields
);
} }
/// <summary> /// <summary>
@ -243,9 +234,7 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Collection> fields [FromQuery] Include<Collection> fields
) )
{ {
ICollection<Collection> resources = await _libraryManager ICollection<Collection> resources = await _libraryManager.Collections.GetAll(
.Collections
.GetAll(
Filter.And(filter, identifier.IsContainedIn<Collection, Show>(x => x.Shows!)), Filter.And(filter, identifier.IsContainedIn<Collection, Show>(x => x.Shows!)),
sortBy, sortBy,
fields, fields,

View File

@ -35,14 +35,13 @@ namespace Kyoo.Core.Api
public class ProxyApi : Controller public class ProxyApi : Controller
{ {
private readonly HttpProxyOptions _proxyOptions = HttpProxyOptionsBuilder private readonly HttpProxyOptions _proxyOptions = HttpProxyOptionsBuilder
.Instance .Instance.WithHandleFailure(
.WithHandleFailure(
async (context, exception) => async (context, exception) =>
{ {
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable; context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
await context await context.Response.WriteAsJsonAsync(
.Response new RequestError("Service unavailable")
.WriteAsJsonAsync(new RequestError("Service unavailable")); );
} }
) )
.Build(); .Build();

View File

@ -150,8 +150,7 @@ namespace Kyoo.Host
.ConfigureAppConfiguration(x => _SetupConfig(x, args)) .ConfigureAppConfiguration(x => _SetupConfig(x, args))
.UseSerilog((host, services, builder) => _ConfigureLogging(builder)) .UseSerilog((host, services, builder) => _ConfigureLogging(builder))
.ConfigureServices(x => x.AddRouting()) .ConfigureServices(x => x.AddRouting())
.ConfigureWebHost( .ConfigureWebHost(x =>
x =>
x.UseKestrel(options => x.UseKestrel(options =>
{ {
options.AddServerHeader = false; options.AddServerHeader = false;
@ -159,15 +158,10 @@ namespace Kyoo.Host
.UseIIS() .UseIIS()
.UseIISIntegration() .UseIISIntegration()
.UseUrls( .UseUrls(
Environment.GetEnvironmentVariable("KYOO_BIND_URL") Environment.GetEnvironmentVariable("KYOO_BIND_URL") ?? "http://*:5000"
?? "http://*:5000"
)
.UseStartup(
host =>
PluginsStartup.FromWebHost(
host,
new LoggerFactory().AddSerilog()
) )
.UseStartup(host =>
PluginsStartup.FromWebHost(host, new LoggerFactory().AddSerilog())
) )
); );
} }
@ -196,20 +190,13 @@ namespace Kyoo.Host
"[{@t:HH:mm:ss} {@l:u3} {Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), 25} " "[{@t:HH:mm:ss} {@l:u3} {Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), 25} "
+ "({@i:D10})] {@m}{#if not EndsWith(@m, '\n')}\n{#end}{@x}"; + "({@i:D10})] {@m}{#if not EndsWith(@m, '\n')}\n{#end}{@x}";
builder builder
.MinimumLevel .MinimumLevel.Warning()
.Warning() .MinimumLevel.Override("Kyoo", LogEventLevel.Verbose)
.MinimumLevel .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Verbose)
.Override("Kyoo", LogEventLevel.Verbose) .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Fatal)
.MinimumLevel .WriteTo.Console(new ExpressionTemplate(template, theme: TemplateTheme.Code))
.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Verbose) .Enrich.WithThreadId()
.MinimumLevel .Enrich.FromLogContext();
.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Fatal)
.WriteTo
.Console(new ExpressionTemplate(template, theme: TemplateTheme.Code))
.Enrich
.WithThreadId()
.Enrich
.FromLogContext();
} }
} }
} }

View File

@ -127,8 +127,8 @@ namespace Kyoo.Host
.SelectMany(x => x.ConfigureSteps) .SelectMany(x => x.ConfigureSteps)
.OrderByDescending(x => x.Priority); .OrderByDescending(x => x.Priority);
using ILifetimeScope scope = container.BeginLifetimeScope( using ILifetimeScope scope = container.BeginLifetimeScope(x =>
x => x.RegisterInstance(app).SingleInstance().ExternallyOwned() x.RegisterInstance(app).SingleInstance().ExternallyOwned()
); );
IServiceProvider provider = scope.Resolve<IServiceProvider>(); IServiceProvider provider = scope.Resolve<IServiceProvider>();
foreach (IStartupAction step in steps) foreach (IStartupAction step in steps)

View File

@ -39,8 +39,10 @@ public class SearchManager : ISearchManager
Sort<T>.By @sortBy Sort<T>.By @sortBy
=> MeilisearchModule => MeilisearchModule
.IndexSettings[index] .IndexSettings[index]
.SortableAttributes .SortableAttributes.Contains(
.Contains(sortBy.Key, StringComparer.InvariantCultureIgnoreCase) sortBy.Key,
StringComparer.InvariantCultureIgnoreCase
)
? new[] ? new[]
{ {
$"{CamelCase.ConvertName(sortBy.Key)}:{(sortBy.Desendant ? "desc" : "asc")}" $"{CamelCase.ConvertName(sortBy.Key)}:{(sortBy.Desendant ? "desc" : "asc")}"

View File

@ -369,16 +369,13 @@ namespace Kyoo.Postgresql
modelBuilder.Entity<Season>().HasIndex(x => x.Slug).IsUnique(); modelBuilder.Entity<Season>().HasIndex(x => x.Slug).IsUnique();
modelBuilder modelBuilder
.Entity<Episode>() .Entity<Episode>()
.HasIndex( .HasIndex(x => new
x =>
new
{ {
ShowID = x.ShowId, ShowID = x.ShowId,
x.SeasonNumber, x.SeasonNumber,
x.EpisodeNumber, x.EpisodeNumber,
x.AbsoluteNumber x.AbsoluteNumber
} })
)
.IsUnique(); .IsUnique();
modelBuilder.Entity<Episode>().HasIndex(x => x.Slug).IsUnique(); modelBuilder.Entity<Episode>().HasIndex(x => x.Slug).IsUnique();
modelBuilder.Entity<User>().HasIndex(x => x.Slug).IsUnique(); modelBuilder.Entity<User>().HasIndex(x => x.Slug).IsUnique();

View File

@ -41,8 +41,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "collections", name: "collections",
columns: table => columns: table => new
new
{ {
id = table.Column<Guid>(type: "uuid", nullable: false), id = table.Column<Guid>(type: "uuid", nullable: false),
slug = table.Column<string>( slug = table.Column<string>(
@ -85,8 +84,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "studios", name: "studios",
columns: table => columns: table => new
new
{ {
id = table.Column<Guid>(type: "uuid", nullable: false), id = table.Column<Guid>(type: "uuid", nullable: false),
slug = table.Column<string>( slug = table.Column<string>(
@ -105,8 +103,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "users", name: "users",
columns: table => columns: table => new
new
{ {
id = table.Column<Guid>(type: "uuid", nullable: false), id = table.Column<Guid>(type: "uuid", nullable: false),
slug = table.Column<string>( slug = table.Column<string>(
@ -138,8 +135,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "movies", name: "movies",
columns: table => columns: table => new
new
{ {
id = table.Column<Guid>(type: "uuid", nullable: false), id = table.Column<Guid>(type: "uuid", nullable: false),
slug = table.Column<string>( slug = table.Column<string>(
@ -203,8 +199,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "shows", name: "shows",
columns: table => columns: table => new
new
{ {
id = table.Column<Guid>(type: "uuid", nullable: false), id = table.Column<Guid>(type: "uuid", nullable: false),
slug = table.Column<string>( slug = table.Column<string>(
@ -270,8 +265,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "link_collection_movie", name: "link_collection_movie",
columns: table => columns: table => new
new
{ {
collection_id = table.Column<Guid>(type: "uuid", nullable: false), collection_id = table.Column<Guid>(type: "uuid", nullable: false),
movie_id = table.Column<Guid>(type: "uuid", nullable: false) movie_id = table.Column<Guid>(type: "uuid", nullable: false)
@ -301,8 +295,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "link_collection_show", name: "link_collection_show",
columns: table => columns: table => new
new
{ {
collection_id = table.Column<Guid>(type: "uuid", nullable: false), collection_id = table.Column<Guid>(type: "uuid", nullable: false),
show_id = table.Column<Guid>(type: "uuid", nullable: false) show_id = table.Column<Guid>(type: "uuid", nullable: false)
@ -332,8 +325,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "seasons", name: "seasons",
columns: table => columns: table => new
new
{ {
id = table.Column<Guid>(type: "uuid", nullable: false), id = table.Column<Guid>(type: "uuid", nullable: false),
slug = table.Column<string>( slug = table.Column<string>(
@ -393,8 +385,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "episodes", name: "episodes",
columns: table => columns: table => new
new
{ {
id = table.Column<Guid>(type: "uuid", nullable: false), id = table.Column<Guid>(type: "uuid", nullable: false),
slug = table.Column<string>( slug = table.Column<string>(

View File

@ -46,8 +46,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "episode_watch_status", name: "episode_watch_status",
columns: table => columns: table => new
new
{ {
user_id = table.Column<Guid>(type: "uuid", nullable: false), user_id = table.Column<Guid>(type: "uuid", nullable: false),
episode_id = table.Column<Guid>(type: "uuid", nullable: false), episode_id = table.Column<Guid>(type: "uuid", nullable: false),
@ -89,8 +88,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "movie_watch_status", name: "movie_watch_status",
columns: table => columns: table => new
new
{ {
user_id = table.Column<Guid>(type: "uuid", nullable: false), user_id = table.Column<Guid>(type: "uuid", nullable: false),
movie_id = table.Column<Guid>(type: "uuid", nullable: false), movie_id = table.Column<Guid>(type: "uuid", nullable: false),
@ -129,8 +127,7 @@ namespace Kyoo.Postgresql.Migrations
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "show_watch_status", name: "show_watch_status",
columns: table => columns: table => new
new
{ {
user_id = table.Column<Guid>(type: "uuid", nullable: false), user_id = table.Column<Guid>(type: "uuid", nullable: false),
show_id = table.Column<Guid>(type: "uuid", nullable: false), show_id = table.Column<Guid>(type: "uuid", nullable: false),

View File

@ -99,17 +99,14 @@ namespace Kyoo.Postgresql
modelBuilder modelBuilder
.HasDbFunction(typeof(DatabaseContext).GetMethod(nameof(MD5))!) .HasDbFunction(typeof(DatabaseContext).GetMethod(nameof(MD5))!)
.HasTranslation( .HasTranslation(args => new SqlFunctionExpression(
args =>
new SqlFunctionExpression(
"md5", "md5",
args, args,
nullable: true, nullable: true,
argumentsPropagateNullability: new[] { false }, argumentsPropagateNullability: new[] { false },
type: args[0].Type, type: args[0].Type,
typeMapping: args[0].TypeMapping typeMapping: args[0].TypeMapping
) ));
);
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
} }

View File

@ -39,8 +39,7 @@ namespace Kyoo.Swagger
{ {
// We can't reorder items by assigning the sorted value to the Paths variable since it has no setter. // We can't reorder items by assigning the sorted value to the Paths variable since it has no setter.
List<KeyValuePair<string, OpenApiPathItem>> sorted = postProcess List<KeyValuePair<string, OpenApiPathItem>> sorted = postProcess
.Paths .Paths.OrderBy(x => x.Key)
.OrderBy(x => x.Key)
.ToList(); .ToList();
postProcess.Paths.Clear(); postProcess.Paths.Clear();
foreach ((string key, OpenApiPathItem value) in sorted) foreach ((string key, OpenApiPathItem value) in sorted)

View File

@ -21,10 +21,10 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using Kyoo.Abstractions.Models.Attributes; using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Swagger.Models; using Kyoo.Swagger.Models;
using Namotion.Reflection;
using NSwag; using NSwag;
using NSwag.Generation.AspNetCore; using NSwag.Generation.AspNetCore;
using NSwag.Generation.Processors.Contexts; using NSwag.Generation.Processors.Contexts;
using Namotion.Reflection;
namespace Kyoo.Swagger namespace Kyoo.Swagger
{ {
@ -42,24 +42,19 @@ namespace Kyoo.Swagger
/// <returns>This always return <c>true</c> since it should not remove operations.</returns> /// <returns>This always return <c>true</c> since it should not remove operations.</returns>
public static bool OperationFilter(OperationProcessorContext context) public static bool OperationFilter(OperationProcessorContext context)
{ {
ApiDefinitionAttribute def = context ApiDefinitionAttribute def =
.ControllerType context.ControllerType.GetCustomAttribute<ApiDefinitionAttribute>();
.GetCustomAttribute<ApiDefinitionAttribute>();
string name = def?.Name ?? context.ControllerType.Name; string name = def?.Name ?? context.ControllerType.Name;
ApiDefinitionAttribute methodOverride = context ApiDefinitionAttribute methodOverride =
.MethodInfo context.MethodInfo.GetCustomAttribute<ApiDefinitionAttribute>();
.GetCustomAttribute<ApiDefinitionAttribute>();
if (methodOverride != null) if (methodOverride != null)
name = methodOverride.Name; name = methodOverride.Name;
context.OperationDescription.Operation.Tags.Add(name); context.OperationDescription.Operation.Tags.Add(name);
if (context.Document.Tags.All(x => x.Name != name)) if (context.Document.Tags.All(x => x.Name != name))
{ {
context context.Document.Tags.Add(
.Document
.Tags
.Add(
new OpenApiTag new OpenApiTag
{ {
Name = name, Name = name,
@ -106,8 +101,7 @@ namespace Kyoo.Swagger
{ {
List<TagGroups> tagGroups = (List<TagGroups>)postProcess.ExtensionData["x-tagGroups"]; List<TagGroups> tagGroups = (List<TagGroups>)postProcess.ExtensionData["x-tagGroups"];
List<string> tagsWithoutGroup = postProcess List<string> tagsWithoutGroup = postProcess
.Tags .Tags.Select(x => x.Name)
.Select(x => x.Name)
.Where(x => tagGroups.SelectMany(y => y.Tags).All(y => y != x)) .Where(x => tagGroups.SelectMany(y => y.Tags).All(y => y != x))
.ToList(); .ToList();
if (tagsWithoutGroup.Any()) if (tagsWithoutGroup.Any())

View File

@ -49,8 +49,7 @@ namespace Kyoo.Swagger
foreach (ActionModel action in context.Result.Controllers.SelectMany(x => x.Actions)) foreach (ActionModel action in context.Result.Controllers.SelectMany(x => x.Actions))
{ {
IEnumerable<ProducesResponseTypeAttribute> responses = action IEnumerable<ProducesResponseTypeAttribute> responses = action
.Filters .Filters.OfType<ProducesResponseTypeAttribute>()
.OfType<ProducesResponseTypeAttribute>()
.Where(x => x.Type == typeof(ActionResult<>)); .Where(x => x.Type == typeof(ActionResult<>));
foreach (ProducesResponseTypeAttribute response in responses) foreach (ProducesResponseTypeAttribute response in responses)
{ {

View File

@ -17,8 +17,8 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System.Collections.Generic; using System.Collections.Generic;
using NSwag;
using Newtonsoft.Json; using Newtonsoft.Json;
using NSwag;
namespace Kyoo.Swagger.Models namespace Kyoo.Swagger.Models
{ {

View File

@ -39,8 +39,7 @@ namespace Kyoo.Swagger
context.OperationDescription.Operation.Security ??= context.OperationDescription.Operation.Security ??=
new List<OpenApiSecurityRequirement>(); new List<OpenApiSecurityRequirement>();
OpenApiSecurityRequirement perms = context OpenApiSecurityRequirement perms = context
.MethodInfo .MethodInfo.GetCustomAttributes<UserOnlyAttribute>()
.GetCustomAttributes<UserOnlyAttribute>()
.Aggregate( .Aggregate(
new OpenApiSecurityRequirement(), new OpenApiSecurityRequirement(),
(agg, _) => (agg, _) =>
@ -51,8 +50,7 @@ namespace Kyoo.Swagger
); );
perms = context perms = context
.MethodInfo .MethodInfo.GetCustomAttributes<PermissionAttribute>()
.GetCustomAttributes<PermissionAttribute>()
.Aggregate( .Aggregate(
perms, perms,
(agg, cur) => (agg, cur) =>
@ -64,14 +62,12 @@ namespace Kyoo.Swagger
} }
); );
PartialPermissionAttribute controller = context PartialPermissionAttribute controller =
.ControllerType context.ControllerType.GetCustomAttribute<PartialPermissionAttribute>();
.GetCustomAttribute<PartialPermissionAttribute>();
if (controller != null) if (controller != null)
{ {
perms = context perms = context
.MethodInfo .MethodInfo.GetCustomAttributes<PartialPermissionAttribute>()
.GetCustomAttributes<PartialPermissionAttribute>()
.Aggregate( .Aggregate(
perms, perms,
(agg, cur) => (agg, cur) =>

View File

@ -82,11 +82,7 @@ namespace Kyoo.Swagger
!= AlternativeRoute; != AlternativeRoute;
return true; return true;
}); });
document document.SchemaGenerator.Settings.TypeMappers.Add(
.SchemaGenerator
.Settings
.TypeMappers
.Add(
new PrimitiveTypeMapper( new PrimitiveTypeMapper(
typeof(Identifier), typeof(Identifier),
x => x =>

View File

@ -54,10 +54,7 @@ namespace Kyoo.Tests.Database
{ {
Episode episode = await _repository.Get(1.AsGuid()); Episode episode = await _repository.Get(1.AsGuid());
Assert.Equal($"{TestSample.Get<Show>().Slug}-s1e1", episode.Slug); Assert.Equal($"{TestSample.Get<Show>().Slug}-s1e1", episode.Slug);
await Repositories await Repositories.LibraryManager.Shows.Patch(
.LibraryManager
.Shows
.Patch(
episode.ShowId, episode.ShowId,
(x) => (x) =>
{ {
@ -92,10 +89,7 @@ namespace Kyoo.Tests.Database
{ {
Episode episode = await _repository.Get(1.AsGuid()); Episode episode = await _repository.Get(1.AsGuid());
Assert.Equal($"{TestSample.Get<Show>().Slug}-s1e1", episode.Slug); Assert.Equal($"{TestSample.Get<Show>().Slug}-s1e1", episode.Slug);
episode = await Repositories episode = await Repositories.LibraryManager.Episodes.Patch(
.LibraryManager
.Episodes
.Patch(
episode.Id, episode.Id,
(x) => (x) =>
{ {
@ -143,10 +137,7 @@ namespace Kyoo.Tests.Database
public async Task SlugEditAbsoluteTest() public async Task SlugEditAbsoluteTest()
{ {
Episode episode = await _repository.Create(TestSample.GetAbsoluteEpisode()); Episode episode = await _repository.Create(TestSample.GetAbsoluteEpisode());
await Repositories await Repositories.LibraryManager.Shows.Patch(
.LibraryManager
.Shows
.Patch(
episode.ShowId, episode.ShowId,
(x) => (x) =>
{ {

View File

@ -53,10 +53,7 @@ namespace Kyoo.Tests.Database
{ {
Season season = await _repository.Get(1.AsGuid()); Season season = await _repository.Get(1.AsGuid());
Assert.Equal("anohana-s1", season.Slug); Assert.Equal("anohana-s1", season.Slug);
await Repositories await Repositories.LibraryManager.Shows.Patch(
.LibraryManager
.Shows
.Patch(
season.ShowId, season.ShowId,
(x) => (x) =>
{ {

View File

@ -10,6 +10,7 @@
combinePackages [ combinePackages [
sdk_7_0 sdk_7_0
aspnetcore_7_0 aspnetcore_7_0
runtime_6_0
]; ];
in in
pkgs.mkShell { pkgs.mkShell {