diff --git a/Manager.App/Components/Pages/Accounts.razor.cs b/Manager.App/Components/Pages/Accounts.razor.cs index 7c25e66..8f8968a 100644 --- a/Manager.App/Components/Pages/Accounts.razor.cs +++ b/Manager.App/Components/Pages/Accounts.razor.cs @@ -50,7 +50,6 @@ public partial class Accounts : ComponentBase await _table.ReloadServerData(); } Snackbar.Add($"Client {clientChannel.Channel?.Handle ?? clientChannel.YouTubeClient.Id} saved!", Severity.Success); - ClientService.AddClient(clientChannel.YouTubeClient); } else { diff --git a/Manager.App/Models/System/YouTubeClientItem.cs b/Manager.App/Models/System/YouTubeClientItem.cs new file mode 100644 index 0000000..9f1180d --- /dev/null +++ b/Manager.App/Models/System/YouTubeClientItem.cs @@ -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; } +} \ No newline at end of file diff --git a/Manager.App/Services/System/ClientService.cs b/Manager.App/Services/System/ClientService.cs index 2c0b48a..461632d 100644 --- a/Manager.App/Services/System/ClientService.cs +++ b/Manager.App/Services/System/ClientService.cs @@ -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 AddClientByIdAsync(string id, CancellationToken stoppingToken = default) + public async Task> 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(comparedClients, accountsResult.Total); + } + + public async Task> 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 SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default) @@ -105,6 +119,8 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger httpCookies = []; httpCookies.AddRange(client.CookieContainer.GetAllCookies().Where(c => c.Expires != DateTime.MinValue) diff --git a/Manager.YouTube/YouTubeClientCollection.cs b/Manager.YouTube/YouTubeClientCollection.cs index 771caae..2e1dedb 100644 --- a/Manager.YouTube/YouTubeClientCollection.cs +++ b/Manager.YouTube/YouTubeClientCollection.cs @@ -8,4 +8,17 @@ public class YouTubeClientCollection : KeyedCollection { 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); + } + } } \ No newline at end of file