[CHANGE] Reworked db with interceptors

This commit is contained in:
max
2025-09-10 23:49:41 +02:00
parent b1e5b0dc68
commit 0f83cf1ddc
23 changed files with 293 additions and 79 deletions

View File

@@ -1,34 +1,32 @@
using System.Net;
using DotBased.Logging;
using DotBased.Monads;
using Manager.App.Models.Library;
using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
using Manager.YouTube.Models.Innertube;
namespace Manager.App.Services.System;
public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientService> logger)
: ExtendedBackgroundService("ClientService", "Managing YouTube clients", logger, TimeSpan.FromMilliseconds(100))
: ExtendedBackgroundService(nameof(ClientService), "Managing YouTube clients", logger, TimeSpan.FromMinutes(10))
{
private readonly List<YouTubeClient> _clients = [];
private CancellationToken _cancellationToken;
private ILibraryService? _libraryService;
protected override async Task InitializeAsync(CancellationToken stoppingToken)
protected override Task InitializeAsync(CancellationToken stoppingToken)
{
_cancellationToken = stoppingToken;
stoppingToken.Register(CancellationRequested);
using var scope = scopeFactory.CreateScope();
_libraryService = scope.ServiceProvider.GetRequiredService<ILibraryService>();
LogEvent("Initializing service...");
//Pause();
return Task.CompletedTask;
}
protected override async Task ExecuteServiceAsync(CancellationToken stoppingToken)
protected override Task ExecuteServiceAsync(CancellationToken stoppingToken)
{
LogEvent("Sending event...");
LogEvent("Sending warning event...", LogSeverity.Warning);
LogEvent("Sending error event...", LogSeverity.Error);
return Task.CompletedTask;
}
private void CancellationRequested()
@@ -36,42 +34,54 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
// Clear up
}
public async Task<Result<ClientPrep>> PrepareClient()
public async Task<Result> SaveClientAsync(YouTubeClient client, Channel? channelInfo = null, CancellationToken cancellationToken = default)
{
return ResultError.Fail("Not implemented!");
}
if (_libraryService == null)
{
return ResultError.Fail("Library service is not initialized!.");
}
/*public async Task<Result> SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(client.Id))
{
LogEvent("Failed to store client no ID!", LogSeverity.Warning);
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
}
var channelResult = await libraryService.GetChannelByIdAsync(client.Id, cancellationToken);
var channelResult = await _libraryService.GetChannelByIdAsync(client.Id, cancellationToken);
ChannelEntity? channel;
if (channelResult.IsSuccess)
try
{
channel = channelResult.Value;
UpdateChannelEntity(channel, client);
if (channelResult.IsSuccess)
{
channel = channelResult.Value;
UpdateChannelEntity(client, channel, channelInfo);
}
else
{
channel = CreateNewChannelFromClient(client, channelInfo);
}
}
else
catch (Exception e)
{
channel = CreateNewChannelFromClient(client);
LogEvent("Failed to save client: " + e.Message, LogSeverity.Warning);
return ResultError.Error(e);
}
var saveResult = await libraryService.SaveChannelAsync(channel, cancellationToken);
var saveResult = await _libraryService.SaveChannelAsync(channel, cancellationToken);
return saveResult;
}*/
}
/*private void UpdateChannelEntity(ChannelEntity channel, YouTubeClient client)
private void UpdateChannelEntity(YouTubeClient client, ChannelEntity entity, Channel? channelInfo)
{
channel.Name = client.External.Channel?.ChannelName;
channel.Handle = client.External.Channel?.Handle;
channel.Description = client.External.Channel?.Description;
var clientAcc = channel.ClientAccount;
if (channelInfo != null)
{
entity.Name = channelInfo.ChannelName;
entity.Handle = channelInfo.Handle;
entity.Description = channelInfo.Description;
}
var clientAcc = entity.ClientAccount;
if (clientAcc != null)
{
clientAcc.UserAgent = clientAcc.UserAgent;
@@ -99,8 +109,13 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
}
}
private ChannelEntity CreateNewChannelFromClient(YouTubeClient client)
private ChannelEntity CreateNewChannelFromClient(YouTubeClient client, Channel? channelInfo)
{
if (channelInfo == null)
{
throw new ArgumentNullException(nameof(channelInfo), "Channel information required to store new client/account.");
}
var cookies = new List<HttpCookieEntity>();
foreach (var cookieObj in client.CookieContainer.GetAllCookies())
{
@@ -133,22 +148,21 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
var channel = new ChannelEntity
{
Id = client.Id,
Name = client.External.Channel?.ChannelName,
Handle = client.External.Channel?.Handle,
Description = client.External.Channel?.Description,
Name = channelInfo.ChannelName,
Handle = channelInfo.Handle,
Description = channelInfo.Description,
ClientAccount = clientAcc
};
return channel;
}*/
}
public async Task<Result<YouTubeClient>> LoadClientByIdAsync(string id)
/*public async Task<Result<YouTubeClient>> LoadClientByIdAsync(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
return ResultError.Fail("Client ID is empty!");
}
return ResultError.Fail("Not implemented");
}
}*/
}