[CHANGE] Fixes library db
This commit is contained in:
@@ -8,7 +8,6 @@ namespace Manager.App.Services;
|
|||||||
|
|
||||||
public interface ILibraryService
|
public interface ILibraryService
|
||||||
{
|
{
|
||||||
public Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel);
|
|
||||||
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
|
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
|
||||||
public Task<Result<LibraryFile>> GetFileByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
public Task<Result<LibraryFile>> GetFileByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||||
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
|
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
|
||||||
|
|||||||
@@ -33,31 +33,6 @@ public class LibraryService : ILibraryService
|
|||||||
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirChannels));
|
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)
|
private async Task AddWebImagesAsync(LibraryDbContext context, List<WebImage> images, string foreignKey, string libSubDir, string fileType, string subDir)
|
||||||
{
|
{
|
||||||
foreach (var image in images)
|
foreach (var image in images)
|
||||||
@@ -192,12 +167,6 @@ public class LibraryService : ILibraryService
|
|||||||
{
|
{
|
||||||
try
|
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);
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
var channelResult = await GetChannelByIdAsync(innertubeChannel.Id, cancellationToken);
|
var channelResult = await GetChannelByIdAsync(innertubeChannel.Id, cancellationToken);
|
||||||
@@ -237,6 +206,9 @@ public class LibraryService : ILibraryService
|
|||||||
context.Channels.Add(channelEntity);
|
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);
|
var changed = await context.SaveChangesAsync(cancellationToken);
|
||||||
return changed <= 0 ? ResultError.Fail("Failed to save channel!") : Result.Success();
|
return changed <= 0 ? ResultError.Fail("Failed to save channel!") : Result.Success();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class AuditInterceptor : SaveChangesInterceptor
|
|||||||
EntityName = entry.Entity.GetType().Name,
|
EntityName = entry.Entity.GetType().Name,
|
||||||
EntityId = primaryKey ?? "Unknown",
|
EntityId = primaryKey ?? "Unknown",
|
||||||
PropertyName = prop.Metadata.Name,
|
PropertyName = prop.Metadata.Name,
|
||||||
OldValue = SerializeValue(prop.OriginalValue),
|
OldValue = changeType == EntityState.Added ? null : SerializeValue(prop.OriginalValue),
|
||||||
NewValue = SerializeValue(prop.CurrentValue),
|
NewValue = SerializeValue(prop.CurrentValue),
|
||||||
ModifiedUtc = DateTime.UtcNow,
|
ModifiedUtc = DateTime.UtcNow,
|
||||||
ChangedBy = "SYSTEM",
|
ChangedBy = "SYSTEM",
|
||||||
|
|||||||
Reference in New Issue
Block a user