[CHANGE] Reworked client creation
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user