[REWORK] Changes saving client and channel info

This commit is contained in:
max
2025-09-18 02:01:45 +02:00
parent 9e173258ed
commit 5250b9f3f9
10 changed files with 201 additions and 153 deletions

View File

@@ -32,14 +32,14 @@ public class LibraryService : ILibraryService
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirChannels));
}
public async Task<Result> FetchChannelImagesAsync(Channel channel)
public async Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel)
{
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync();
await AddWebImagesAsync(context, channel.AvatarImages, channel.Id, "avatars", LibraryConstants.FileTypes.ChannelAvatar, LibraryConstants.Directories.SubDirChannels);
await AddWebImagesAsync(context, channel.BannerImages, channel.Id, "banners", LibraryConstants.FileTypes.ChannelBanner, LibraryConstants.Directories.SubDirChannels);
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())
{
@@ -101,6 +101,43 @@ public class LibraryService : ILibraryService
}
}
public async Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default)
{
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var updateEntity = false;
var dbClient = context.ClientAccounts.FirstOrDefault(c => c.Id == client.Id);
if (dbClient == null)
{
dbClient = client;
}
else
{
updateEntity = true;
dbClient.HttpCookies = client.HttpCookies;
dbClient.UserAgent = client.UserAgent;
}
if (updateEntity)
{
context.ClientAccounts.Update(dbClient);
}
else
{
context.ClientAccounts.Add(dbClient);
}
var savedResult= await context.SaveChangesAsync(cancellationToken);
return savedResult <= 0 ? ResultError.Fail("Could not save changes!") : Result.Success();
}
catch (Exception e)
{
return ResultError.Error(e);
}
}
public async Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(id))
@@ -111,7 +148,11 @@ public class LibraryService : ILibraryService
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var channel = await context.Channels.Include(c => c.ClientAccount).FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
var channel = await context.Channels
.Include(c => c.ClientAccount)
.ThenInclude(p => p!.HttpCookies)
.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
if (channel == null)
{
return ResultError.Fail("Channel not found!");
@@ -125,18 +166,53 @@ public class LibraryService : ILibraryService
}
}
public async Task<Result> SaveChannelAsync(ChannelEntity channel, CancellationToken cancellationToken = default)
public async Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default)
{
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
if (context.Channels.Any(c => c.Id == channel.Id))
var imagesResult = await FetchChannelImagesAsync(innertubeChannel);
if (!imagesResult.IsSuccess)
{
context.Channels.Update(channel);
return ResultError.Fail("Failed to fetch channel images!");
}
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var channelResult = await GetChannelByIdAsync(innertubeChannel.Id, cancellationToken);
ChannelEntity? channelEntity;
try
{
if (channelResult.IsSuccess)
{
channelEntity = channelResult.Value;
channelEntity.Name = innertubeChannel.ChannelName;
channelEntity.Handle = innertubeChannel.Handle;
channelEntity.Description = innertubeChannel.Description;
}
else
{
channelEntity = new ChannelEntity
{
Id = innertubeChannel.Id,
Name = innertubeChannel.ChannelName,
Handle = innertubeChannel.Handle,
Description = innertubeChannel.Description
};
}
}
catch (Exception e)
{
return ResultError.Error(e);
}
if (context.Channels.Any(c => c.Id == innertubeChannel.Id))
{
context.Channels.Update(channelEntity);
}
else
{
context.Channels.Add(channel);
context.Channels.Add(channelEntity);
}
var changed = await context.SaveChangesAsync(cancellationToken);
@@ -144,7 +220,7 @@ public class LibraryService : ILibraryService
}
catch (Exception e)
{
return ResultError.Error(e);
return HandleException(e);
}
}