[CHANGE] Reworked client creation

This commit is contained in:
max
2025-09-08 21:28:33 +02:00
parent b2c9fc2c52
commit 680b6d2cc9
19 changed files with 563 additions and 1347 deletions

View File

@@ -7,6 +7,8 @@ namespace Manager.App.Services;
public interface ILibraryService
{
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
public Task<Result> SaveChannelAsync(ChannelEntity channel, CancellationToken cancellationToken = default);
public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default);
public Task<ListResult<ChannelEntity>> GetChannelAccountsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default);

View File

@@ -29,6 +29,53 @@ public class LibraryService : ILibraryService
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, SubDirChannels));
}
public async Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(id))
{
return ResultError.Fail("Channel id cannot be null or empty!");
}
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var channel = await context.Channels.Include(c => c.ClientAccount).FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
if (channel == null)
{
return ResultError.Fail("Channel not found!");
}
return channel;
}
catch (Exception e)
{
return HandleException(e);
}
}
public async Task<Result> SaveChannelAsync(ChannelEntity channel, CancellationToken cancellationToken = default)
{
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
if (context.Channels.Any(c => c.Id == channel.Id))
{
context.Channels.Update(channel);
}
else
{
context.Channels.Add(channel);
}
var changed = await context.SaveChangesAsync(cancellationToken);
return changed <= 0 ? Result.Success() : ResultError.Fail("Failed to save channel!");
}
catch (Exception e)
{
return ResultError.Error(e);
}
}
public async Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default)
{
try
@@ -57,7 +104,7 @@ public class LibraryService : ILibraryService
{
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);
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)
{

View File

@@ -1,4 +1,7 @@
using System.Net;
using DotBased.Monads;
using Manager.App.Models.Library;
using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
namespace Manager.App.Services.System;
@@ -19,17 +22,118 @@ public class ClientManager : BackgroundService
// Clear up
}
public async Task<Result> SaveClientAsync(YouTubeClient client)
public async Task<Result<ClientPrep>> PrepareClient()
{
return ResultError.Fail("Not implemented");
return ResultError.Fail("Not implemented!");
}
/*public async Task<Result> SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(client.Id))
{
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
}
var channelResult = await libraryService.GetChannelByIdAsync(client.Id, cancellationToken);
ChannelEntity? channel;
if (channelResult.IsSuccess)
{
channel = channelResult.Value;
UpdateChannelEntity(channel, client);
}
else
{
channel = CreateNewChannelFromClient(client);
}
var saveResult = await libraryService.SaveChannelAsync(channel, cancellationToken);
return saveResult;
}*/
/*private void UpdateChannelEntity(ChannelEntity channel, YouTubeClient client)
{
channel.Name = client.External.Channel?.ChannelName;
channel.Handle = client.External.Channel?.Handle;
channel.Description = client.External.Channel?.Description;
var clientAcc = channel.ClientAccount;
if (clientAcc != null)
{
clientAcc.UserAgent = clientAcc.UserAgent;
var currentCookies = client.CookieContainer.GetAllCookies();
foreach (var cookieEntity in clientAcc.HttpCookies.ToList())
{
var cookie = currentCookies[cookieEntity.Name];
if (cookie == null)
{
clientAcc.HttpCookies.Remove(cookieEntity);
continue;
}
if (!cookie.Domain.Equals(cookieEntity.Domain, StringComparison.InvariantCultureIgnoreCase))
{
continue;
}
cookieEntity.Value = cookie.Value;
cookieEntity.Path = cookie.Path;
cookieEntity.Secure = cookie.Secure;
cookieEntity.HttpOnly = cookie.HttpOnly;
cookieEntity.ExpiresUtc = cookie.Expires == DateTime.MinValue ? null : cookie.Expires;
}
}
}
private ChannelEntity CreateNewChannelFromClient(YouTubeClient client)
{
var cookies = new List<HttpCookieEntity>();
foreach (var cookieObj in client.CookieContainer.GetAllCookies())
{
if (cookieObj is not Cookie cookie)
{
continue;
}
var cookieEntity = 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 == DateTime.MinValue ? null : cookie.Expires
};
cookies.Add(cookieEntity);
}
var clientAcc = new ClientAccountEntity
{
Id = client.Id,
UserAgent = client.UserAgent,
HttpCookies = cookies
};
var channel = new ChannelEntity
{
Id = client.Id,
Name = client.External.Channel?.ChannelName,
Handle = client.External.Channel?.Handle,
Description = client.External.Channel?.Description,
ClientAccount = clientAcc
};
return channel;
}*/
public async Task<Result<YouTubeClient>> LoadClientByIdAsync(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
return ResultError.Fail("Client ID is empty!");
}
return ResultError.Fail("Not implemented");
}