[CHANGE] Fixes library db
This commit is contained in:
@@ -8,7 +8,6 @@ namespace Manager.App.Services;
|
||||
|
||||
public interface ILibraryService
|
||||
{
|
||||
public Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel);
|
||||
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
|
||||
public Task<Result<LibraryFile>> GetFileByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
|
||||
|
||||
@@ -32,31 +32,6 @@ public class LibraryService : ILibraryService
|
||||
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirMedia));
|
||||
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirChannels));
|
||||
}
|
||||
|
||||
public async Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||
|
||||
await AddWebImagesAsync(context, innertubeChannel.AvatarImages, innertubeChannel.Id, "avatars", LibraryConstants.FileTypes.ChannelAvatar, LibraryConstants.Directories.SubDirChannels);
|
||||
await AddWebImagesAsync(context, innertubeChannel.BannerImages, innertubeChannel.Id, "banners", LibraryConstants.FileTypes.ChannelBanner, LibraryConstants.Directories.SubDirChannels);
|
||||
|
||||
if (!context.ChangeTracker.HasChanges())
|
||||
{
|
||||
_logger.LogInformation("No changes detected. Skipping.");
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleException(e);
|
||||
}
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
private async Task AddWebImagesAsync(LibraryDbContext context, List<WebImage> images, string foreignKey, string libSubDir, string fileType, string subDir)
|
||||
{
|
||||
@@ -191,13 +166,7 @@ public class LibraryService : ILibraryService
|
||||
public async Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var imagesResult = await FetchChannelImagesAsync(innertubeChannel);
|
||||
if (!imagesResult.IsSuccess)
|
||||
{
|
||||
return ResultError.Fail("Failed to fetch channel images!");
|
||||
}
|
||||
|
||||
{
|
||||
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var channelResult = await GetChannelByIdAsync(innertubeChannel.Id, cancellationToken);
|
||||
@@ -237,6 +206,9 @@ public class LibraryService : ILibraryService
|
||||
context.Channels.Add(channelEntity);
|
||||
}
|
||||
|
||||
await AddWebImagesAsync(context, innertubeChannel.AvatarImages, innertubeChannel.Id, "avatars", LibraryConstants.FileTypes.ChannelAvatar, LibraryConstants.Directories.SubDirChannels);
|
||||
await AddWebImagesAsync(context, innertubeChannel.BannerImages, innertubeChannel.Id, "banners", LibraryConstants.FileTypes.ChannelBanner, LibraryConstants.Directories.SubDirChannels);
|
||||
|
||||
var changed = await context.SaveChangesAsync(cancellationToken);
|
||||
return changed <= 0 ? ResultError.Fail("Failed to save channel!") : Result.Success();
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ public class AuditInterceptor : SaveChangesInterceptor
|
||||
EntityName = entry.Entity.GetType().Name,
|
||||
EntityId = primaryKey ?? "Unknown",
|
||||
PropertyName = prop.Metadata.Name,
|
||||
OldValue = SerializeValue(prop.OriginalValue),
|
||||
OldValue = changeType == EntityState.Added ? null : SerializeValue(prop.OriginalValue),
|
||||
NewValue = SerializeValue(prop.CurrentValue),
|
||||
ModifiedUtc = DateTime.UtcNow,
|
||||
ChangedBy = "SYSTEM",
|
||||
|
||||
Reference in New Issue
Block a user