[CHANGE] Client handling
This commit is contained in:
@@ -50,7 +50,6 @@ public partial class Accounts : ComponentBase
|
|||||||
await _table.ReloadServerData();
|
await _table.ReloadServerData();
|
||||||
}
|
}
|
||||||
Snackbar.Add($"Client {clientChannel.Channel?.Handle ?? clientChannel.YouTubeClient.Id} saved!", Severity.Success);
|
Snackbar.Add($"Client {clientChannel.Channel?.Handle ?? clientChannel.YouTubeClient.Id} saved!", Severity.Success);
|
||||||
ClientService.AddClient(clientChannel.YouTubeClient);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
16
Manager.App/Models/System/YouTubeClientItem.cs
Normal file
16
Manager.App/Models/System/YouTubeClientItem.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using Manager.App.Models.Library;
|
||||||
|
|
||||||
|
namespace Manager.App.Models.System;
|
||||||
|
|
||||||
|
public class YouTubeClientItem : AccountListView
|
||||||
|
{
|
||||||
|
public YouTubeClientItem(AccountListView accountListView)
|
||||||
|
{
|
||||||
|
Id = accountListView.Id;
|
||||||
|
Name = accountListView.Name;
|
||||||
|
Handle = accountListView.Handle;
|
||||||
|
HasCookies = accountListView.HasCookies;
|
||||||
|
AvatarFileId = accountListView.AvatarFileId;
|
||||||
|
}
|
||||||
|
public bool IsLoaded { get; set; }
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using DotBased.Logging;
|
using DotBased.Logging;
|
||||||
using DotBased.Monads;
|
using DotBased.Monads;
|
||||||
|
using Manager.App.Models.System;
|
||||||
using Manager.Data.Entities.LibraryContext;
|
using Manager.Data.Entities.LibraryContext;
|
||||||
using Manager.YouTube;
|
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)
|
if (_libraryService == null)
|
||||||
{
|
{
|
||||||
return ResultError.Fail("Library service is not initialized!.");
|
return ResultError.Fail("Library service is not initialized!.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var clientResult = await _libraryService.GetChannelByIdAsync(id, stoppingToken);
|
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!.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!forceLoad && _loadedClients.TryGetValue(id, out var client))
|
||||||
|
{
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
var clientResult = await _libraryService.GetChannelByIdAsync(id, cancellationToken);
|
||||||
if (!clientResult.IsSuccess)
|
if (!clientResult.IsSuccess)
|
||||||
{
|
{
|
||||||
return clientResult;
|
return clientResult.Error ?? ResultError.Fail("Failed to load channel from database!");
|
||||||
}
|
}
|
||||||
|
|
||||||
var clientAcc = clientResult.Value.ClientAccount;
|
var clientAcc = clientResult.Value.ClientAccount;
|
||||||
@@ -79,18 +103,8 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
|
|||||||
return ytClientResult;
|
return ytClientResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
AddClient(ytClientResult.Value);
|
_loadedClients.Add(ytClientResult.Value);
|
||||||
return Result.Success();
|
return ytClientResult.Value;
|
||||||
}
|
|
||||||
|
|
||||||
public void AddClient(YouTubeClient client)
|
|
||||||
{
|
|
||||||
if (_loadedClients.Contains(client))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_loadedClients.Add(client);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result> SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default)
|
public async Task<Result> SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default)
|
||||||
@@ -106,6 +120,8 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
|
|||||||
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
|
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_loadedClients.Add(client);
|
||||||
|
|
||||||
List<HttpCookieEntity> httpCookies = [];
|
List<HttpCookieEntity> httpCookies = [];
|
||||||
httpCookies.AddRange(client.CookieContainer.GetAllCookies().Where(c => c.Expires != DateTime.MinValue)
|
httpCookies.AddRange(client.CookieContainer.GetAllCookies().Where(c => c.Expires != DateTime.MinValue)
|
||||||
.ToList()
|
.ToList()
|
||||||
|
|||||||
@@ -8,4 +8,17 @@ public class YouTubeClientCollection : KeyedCollection<string, YouTubeClient>
|
|||||||
{
|
{
|
||||||
return item.Id;
|
return item.Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public new void Add(YouTubeClient client)
|
||||||
|
{
|
||||||
|
if (Items.Contains(client))
|
||||||
|
{
|
||||||
|
var index = Items.IndexOf(client);
|
||||||
|
Items[index] = client;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Items.Add(client);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user