[CHANGE] Fixed storing account/channel
This commit is contained in:
@@ -29,9 +29,9 @@
|
|||||||
<MudTh>Has login</MudTh>
|
<MudTh>Has login</MudTh>
|
||||||
</HeaderContent>
|
</HeaderContent>
|
||||||
<RowTemplate>
|
<RowTemplate>
|
||||||
<MudTd>@context.Name</MudTd>
|
<MudTd>@context.Channel?.Name</MudTd>
|
||||||
<MudTd>@context.Id</MudTd>
|
<MudTd>@context.Id</MudTd>
|
||||||
<MudTd>@(context.ClientAccount != null)</MudTd>
|
<MudTd>@(context.HttpCookies.Any())</MudTd>
|
||||||
</RowTemplate>
|
</RowTemplate>
|
||||||
<NoRecordsContent>
|
<NoRecordsContent>
|
||||||
<MudText>No channels found</MudText>
|
<MudText>No channels found</MudText>
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ namespace Manager.App.Components.Pages;
|
|||||||
public partial class Channels : ComponentBase
|
public partial class Channels : ComponentBase
|
||||||
{
|
{
|
||||||
private readonly DialogOptions _dialogOptions = new() { BackdropClick = false, CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.ExtraLarge };
|
private readonly DialogOptions _dialogOptions = new() { BackdropClick = false, CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.ExtraLarge };
|
||||||
private MudTable<ChannelEntity>? _table;
|
private MudTable<ClientAccountEntity>? _table;
|
||||||
|
|
||||||
private async Task<TableData<ChannelEntity>> ServerReload(TableState state, CancellationToken token)
|
private async Task<TableData<ClientAccountEntity>> ServerReload(TableState state, CancellationToken token)
|
||||||
{
|
{
|
||||||
var results = await LibraryService.GetChannelsAsync(state.PageSize, state.Page * state.PageSize, token);
|
var results = await LibraryService.GetAccountsAsync(null, state.Page * state.PageSize, state.PageSize, token);
|
||||||
return !results.IsSuccess ? new TableData<ChannelEntity>() : new TableData<ChannelEntity> { Items = results.Value, TotalItems = results.Total };
|
return !results.IsSuccess ? new TableData<ClientAccountEntity>() : new TableData<ClientAccountEntity> { Items = results.Value, TotalItems = results.Total };
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task OnAddAccountDialogAsync()
|
private async Task OnAddAccountDialogAsync()
|
||||||
|
|||||||
@@ -11,9 +11,8 @@ public interface ILibraryService
|
|||||||
public Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel);
|
public Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel);
|
||||||
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
|
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
|
||||||
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
|
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
public Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default);
|
public Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default);
|
||||||
public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default);
|
public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default);
|
||||||
|
public Task<ListResult<ClientAccountEntity>> GetAccountsAsync(string? search, int offset = 0, int total = 20, CancellationToken cancellationToken = default);
|
||||||
public Task<ListResult<ChannelEntity>> GetChannelsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default);
|
public Task<ListResult<ChannelEntity>> GetChannelsAsync(int offset = 0, int total = 20, CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
||||||
@@ -61,7 +61,7 @@ public class LibraryService : ILibraryService
|
|||||||
{
|
{
|
||||||
foreach (var image in images)
|
foreach (var image in images)
|
||||||
{
|
{
|
||||||
if (context.Files.Any(f => image.Url.Equals(f.OriginalUrl, StringComparison.OrdinalIgnoreCase)))
|
if (context.Files.Any(f => image.Url.Equals(f.OriginalUrl)))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -108,7 +108,7 @@ public class LibraryService : ILibraryService
|
|||||||
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
var updateEntity = false;
|
var updateEntity = false;
|
||||||
var dbClient = context.ClientAccounts.FirstOrDefault(c => c.Id == client.Id);
|
var dbClient = context.ClientAccounts.Include(ca => ca.HttpCookies).FirstOrDefault(c => c.Id == client.Id);
|
||||||
if (dbClient == null)
|
if (dbClient == null)
|
||||||
{
|
{
|
||||||
dbClient = client;
|
dbClient = client;
|
||||||
@@ -246,12 +246,54 @@ public class LibraryService : ILibraryService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ListResult<ChannelEntity>> GetChannelsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default)
|
public async Task<ListResult<ClientAccountEntity>> GetAccountsAsync(string? search, int offset = 0, int total = 20, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (total == 0)
|
||||||
|
{
|
||||||
|
total = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
var accounts = context.ClientAccounts
|
||||||
|
.Include(ca => ca.Channel)
|
||||||
|
.Include(ca => ca.HttpCookies)
|
||||||
|
.OrderByDescending(ca => ca.Id);
|
||||||
|
var totalAccounts = accounts.Count();
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(search) && totalAccounts != 0)
|
||||||
|
{
|
||||||
|
var normalizedSearch = $"%{search.ToLower()}%";
|
||||||
|
var searched = accounts
|
||||||
|
.Where(ca =>
|
||||||
|
EF.Functions.Like(
|
||||||
|
(
|
||||||
|
ca.Id.ToString() + " " +
|
||||||
|
(ca.Channel != null ? ca.Channel.Name : "") + " " +
|
||||||
|
(ca.Channel != null ? ca.Channel.Handle : "")
|
||||||
|
).ToLower(),
|
||||||
|
normalizedSearch
|
||||||
|
)
|
||||||
|
);
|
||||||
|
totalAccounts = searched.Count();
|
||||||
|
accounts = searched.OrderByDescending(ca => ca.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ListResultReturn<ClientAccountEntity>(totalAccounts == 0 ? [] : accounts.Skip(offset).Take(total).ToList(), totalAccounts);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return ResultError.Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ListResult<ChannelEntity>> GetChannelsAsync(int offset = 0, int total = 20, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
var orderedAccounts = context.Channels.Include(x => x.ClientAccount).Where(x => x.ClientAccount != null).OrderBy(x => x.Id);
|
var orderedAccounts = context.Channels.Include(x => x.ClientAccount).OrderBy(x => x.Id);
|
||||||
return new ListResultReturn<ChannelEntity>(orderedAccounts.Skip(offset).Take(total).ToList(),orderedAccounts.Count());
|
return new ListResultReturn<ChannelEntity>(orderedAccounts.Skip(offset).Take(total).ToList(),orderedAccounts.Count());
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|||||||
@@ -106,7 +106,22 @@ 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!");
|
||||||
}
|
}
|
||||||
|
|
||||||
var saveResult = await _libraryService.SaveClientAsync(new ClientAccountEntity { Id = client.Id, UserAgent = client.UserAgent }, cancellationToken);
|
List<HttpCookieEntity> httpCookies = [];
|
||||||
|
httpCookies.AddRange(client.CookieContainer.GetAllCookies()
|
||||||
|
.ToList()
|
||||||
|
.Select(cookie => new HttpCookieEntity
|
||||||
|
{
|
||||||
|
ClientId = client.Id,
|
||||||
|
Name = cookie.Name,
|
||||||
|
Value = cookie.Value,
|
||||||
|
Domain = cookie.Domain,
|
||||||
|
Path = cookie.Path,
|
||||||
|
Secure = cookie.Secure,
|
||||||
|
HttpOnly = cookie.HttpOnly,
|
||||||
|
ExpiresUtc = cookie.Expires
|
||||||
|
}));
|
||||||
|
|
||||||
|
var saveResult = await _libraryService.SaveClientAsync(new ClientAccountEntity { Id = client.Id, UserAgent = client.UserAgent, HttpCookies = httpCookies }, cancellationToken);
|
||||||
return saveResult;
|
return saveResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -55,8 +55,9 @@ public sealed class LibraryDbContext : DbContext
|
|||||||
.WithOne()
|
.WithOne()
|
||||||
.HasForeignKey(x => x.ChannelId);
|
.HasForeignKey(x => x.ChannelId);
|
||||||
channel.HasOne(x => x.ClientAccount)
|
channel.HasOne(x => x.ClientAccount)
|
||||||
.WithOne()
|
.WithOne(x => x.Channel)
|
||||||
.HasForeignKey<ClientAccountEntity>(e => e.Id);
|
.HasForeignKey<ClientAccountEntity>(e => e.Id)
|
||||||
|
.IsRequired(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity<ClientAccountEntity>(cae =>
|
modelBuilder.Entity<ClientAccountEntity>(cae =>
|
||||||
@@ -66,6 +67,10 @@ public sealed class LibraryDbContext : DbContext
|
|||||||
cae.HasMany(x => x.HttpCookies)
|
cae.HasMany(x => x.HttpCookies)
|
||||||
.WithOne()
|
.WithOne()
|
||||||
.HasForeignKey(x => x.ClientId);
|
.HasForeignKey(x => x.ClientId);
|
||||||
|
cae.HasOne(x => x.Channel)
|
||||||
|
.WithOne(ca => ca.ClientAccount)
|
||||||
|
.HasForeignKey<ChannelEntity>(ce => ce.Id)
|
||||||
|
.IsRequired(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity<HttpCookieEntity>(httpce =>
|
modelBuilder.Entity<HttpCookieEntity>(httpce =>
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ public class ClientAccountEntity : DateTimeBase
|
|||||||
public List<HttpCookieEntity> HttpCookies { get; set; } = [];
|
public List<HttpCookieEntity> HttpCookies { get; set; } = [];
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public string? UserAgent { get; set; }
|
public string? UserAgent { get; set; }
|
||||||
|
public ChannelEntity? Channel { get; set; }
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user