[CHANGE] Channel & Account list to view models
This commit is contained in:
@@ -150,7 +150,7 @@ public class LibraryService : ILibraryService
|
||||
return ResultError.Fail($"File with id {id} not found.");
|
||||
}
|
||||
|
||||
var fs = new FileStream(Path.Combine(_libraryDirectory.FullName, file.RelativePath), FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var fs = new FileStream(Path.Combine(_libraryDirectory.FullName, LibraryConstants.Directories.SubDirChannels, file.RelativePath), FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
return new LibraryFile { DataStream = fs, SizeBytes = file.SizeBytes, FileName = file.OriginalFileName ?? file.Id.ToString(), MimeType = file.MimeType ?? MediaTypeNames.Application.Octet };
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -268,7 +268,7 @@ public class LibraryService : ILibraryService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<ClientAccountEntity>> GetAccountsAsync(string? search, int offset = 0, int total = 20, CancellationToken cancellationToken = default)
|
||||
public async Task<ListResult<AccountListView>> GetAccountsAsync(string? search, int offset = 0, int total = 20, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (total == 0)
|
||||
{
|
||||
@@ -278,16 +278,16 @@ public class LibraryService : ILibraryService
|
||||
try
|
||||
{
|
||||
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var accounts = context.ClientAccounts
|
||||
var accountsQuery = context.ClientAccounts
|
||||
.Include(ca => ca.Channel)
|
||||
.Include(ca => ca.HttpCookies)
|
||||
.OrderByDescending(ca => ca.Id);
|
||||
var totalAccounts = accounts.Count();
|
||||
.OrderByDescending(ca => ca.Id).AsQueryable();
|
||||
var totalAccounts = accountsQuery.Count();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(search) && totalAccounts != 0)
|
||||
{
|
||||
var normalizedSearch = $"%{search.ToLower()}%";
|
||||
var searched = accounts
|
||||
accountsQuery = accountsQuery
|
||||
.Where(ca =>
|
||||
EF.Functions.Like(
|
||||
(
|
||||
@@ -298,11 +298,20 @@ public class LibraryService : ILibraryService
|
||||
normalizedSearch
|
||||
)
|
||||
);
|
||||
totalAccounts = searched.Count();
|
||||
accounts = searched.OrderByDescending(ca => ca.Id);
|
||||
totalAccounts = accountsQuery.Count();
|
||||
}
|
||||
|
||||
return new ListResultReturn<ClientAccountEntity>(totalAccounts == 0 ? [] : accounts.Skip(offset).Take(total).ToList(), totalAccounts);
|
||||
var accountViews = accountsQuery.Skip(offset).Take(total).Select(account => new AccountListView
|
||||
{
|
||||
Id = account.Id,
|
||||
Name = account.Channel != null ? account.Channel.Name : "",
|
||||
Handle = account.Channel != null ? account.Channel.Handle : "",
|
||||
HasCookies = account.HttpCookies.Count != 0,
|
||||
AvatarFileId = account.Files == null ? null
|
||||
: account.Files.Where(f => f.FileType == LibraryConstants.FileTypes.ChannelAvatar).OrderBy(x => x.Id).Select(f => f.Id).FirstOrDefault()
|
||||
});
|
||||
|
||||
return new ListResultReturn<AccountListView>(totalAccounts == 0 ? [] : accountViews.ToList(), totalAccounts);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -310,21 +319,26 @@ public class LibraryService : ILibraryService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<ChannelEntity>> GetChannelsAsync(string? search, int offset = 0, int total = 20, CancellationToken cancellationToken = default)
|
||||
public async Task<ListResult<ChannelListView>> GetChannelsAsync(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 orderedAccounts = context.Channels.OrderBy(x => x.Id);
|
||||
|
||||
var totalChannels = orderedAccounts.Count();
|
||||
if (!string.IsNullOrWhiteSpace(search) && orderedAccounts.Any())
|
||||
var channelQuery = context.Channels.OrderByDescending(c => c.Id).AsQueryable();
|
||||
|
||||
var totalChannels = channelQuery.Count();
|
||||
if (!string.IsNullOrWhiteSpace(search) && totalChannels != 0)
|
||||
{
|
||||
var normalizedSearch = $"%{search.ToLower()}%";
|
||||
var searched = orderedAccounts
|
||||
channelQuery = channelQuery
|
||||
.Where(ca =>
|
||||
EF.Functions.Like(
|
||||
(
|
||||
EF.Functions.Like((
|
||||
ca.Id.ToString() + " " +
|
||||
ca.Name + " " +
|
||||
ca.Handle
|
||||
@@ -332,11 +346,19 @@ public class LibraryService : ILibraryService
|
||||
normalizedSearch
|
||||
)
|
||||
);
|
||||
totalChannels = searched.Count();
|
||||
orderedAccounts = searched.OrderByDescending(ca => ca.Id);
|
||||
totalChannels = channelQuery.Count();
|
||||
}
|
||||
|
||||
var channelViews = channelQuery.Skip(offset).Take(total).Select(channel => new ChannelListView
|
||||
{
|
||||
Id = channel.Id,
|
||||
Name = channel.Name,
|
||||
Handle = channel.Handle,
|
||||
AvatarFileId = channel.Files == null ? null
|
||||
: channel.Files.Where(f => f.FileType == LibraryConstants.FileTypes.ChannelAvatar).OrderBy(x => x.Id).Select(f => f.Id).FirstOrDefault()
|
||||
});
|
||||
|
||||
return new ListResultReturn<ChannelEntity>(totalChannels == 0 ? [] : orderedAccounts.Skip(offset).Take(total).ToList(), totalChannels);
|
||||
return new ListResultReturn<ChannelListView>(totalChannels == 0 ? [] : channelViews.ToList(), totalChannels);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -366,6 +388,6 @@ public class LibraryService : ILibraryService
|
||||
}
|
||||
|
||||
_logger.LogError(exception, "Service error");
|
||||
return ResultError.Fail("Failed to get library information");
|
||||
return ResultError.Error(exception);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user