diff --git a/Manager.App/Services/ILibraryService.cs b/Manager.App/Services/ILibraryService.cs index b4f5038..43e7be3 100644 --- a/Manager.App/Services/ILibraryService.cs +++ b/Manager.App/Services/ILibraryService.cs @@ -8,7 +8,6 @@ namespace Manager.App.Services; public interface ILibraryService { - public Task FetchChannelImagesAsync(InnertubeChannel innertubeChannel); public Task SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default); public Task> GetFileByIdAsync(Guid id, CancellationToken cancellationToken = default); public Task> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default); diff --git a/Manager.App/Services/LibraryService.cs b/Manager.App/Services/LibraryService.cs index 88d965b..89408ac 100644 --- a/Manager.App/Services/LibraryService.cs +++ b/Manager.App/Services/LibraryService.cs @@ -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 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 images, string foreignKey, string libSubDir, string fileType, string subDir) { @@ -191,13 +166,7 @@ public class LibraryService : ILibraryService public async Task 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(); } diff --git a/Manager.Data/Contexts/AuditInterceptor.cs b/Manager.Data/Contexts/AuditInterceptor.cs index d73e610..2f5b3ba 100644 --- a/Manager.Data/Contexts/AuditInterceptor.cs +++ b/Manager.Data/Contexts/AuditInterceptor.cs @@ -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",