[CHANGE] Client handling

This commit is contained in:
max
2025-10-19 23:21:07 +02:00
parent e4596df392
commit 34e029ec79
4 changed files with 60 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
using System.Net;
using DotBased.Logging;
using DotBased.Monads;
using Manager.App.Models.System;
using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
@@ -39,17 +40,40 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
}
}
public async Task<Result> AddClientByIdAsync(string id, CancellationToken stoppingToken = default)
public async Task<ListResult<YouTubeClientItem>> GetClientsAsync(string search, int offset = 0, int limit = 10, CancellationToken cancellationToken = default)
{
if (_libraryService == null)
{
return ResultError.Fail("Library service is not initialized!.");
}
var accountsResult = await _libraryService.GetAccountsAsync(search, offset, limit, cancellationToken);
if (!accountsResult.IsSuccess)
{
return accountsResult.Error ?? ResultError.Fail("Failed to get accounts!");
}
var comparedClients = accountsResult.Value.Select(x => new YouTubeClientItem(x)
{ Id = x.Id, IsLoaded = _loadedClients.Contains(x.Id) }).ToList();
return new ListResultReturn<YouTubeClientItem>(comparedClients, accountsResult.Total);
}
public async Task<Result<YouTubeClient>> LoadClientByIdAsync(string id, bool forceLoad = false, CancellationToken cancellationToken = default)
{
if (_libraryService == null)
{
return ResultError.Fail("Library service is not initialized!.");
}
var clientResult = await _libraryService.GetChannelByIdAsync(id, stoppingToken);
if (!forceLoad && _loadedClients.TryGetValue(id, out var client))
{
return client;
}
var clientResult = await _libraryService.GetChannelByIdAsync(id, cancellationToken);
if (!clientResult.IsSuccess)
{
return clientResult;
return clientResult.Error ?? ResultError.Fail("Failed to load channel from database!");
}
var clientAcc = clientResult.Value.ClientAccount;
@@ -79,18 +103,8 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
return ytClientResult;
}
AddClient(ytClientResult.Value);
return Result.Success();
}
public void AddClient(YouTubeClient client)
{
if (_loadedClients.Contains(client))
{
return;
}
_loadedClients.Add(client);
_loadedClients.Add(ytClientResult.Value);
return ytClientResult.Value;
}
public async Task<Result> SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default)
@@ -105,6 +119,8 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
LogEvent("Failed to store client no ID!", LogSeverity.Warning);
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
}
_loadedClients.Add(client);
List<HttpCookieEntity> httpCookies = [];
httpCookies.AddRange(client.CookieContainer.GetAllCookies().Where(c => c.Expires != DateTime.MinValue)