Files
jsmr/JSMR.Application/Scanning/ScanVoiceWorksHandler.cs
Brian Bicknell 1d40013837
All checks were successful
ci / build-test (push) Successful in 2m27s
ci / publish-image (push) Has been skipped
Fixed voice work updater bug. Added integration tests for voice work search updates (Japanese).
2026-03-05 23:29:29 -05:00

56 lines
2.2 KiB
C#

using JSMR.Application.Common.Caching;
using JSMR.Application.Integrations.DLSite.Models;
using JSMR.Application.Integrations.Ports;
using JSMR.Application.Scanning.Contracts;
using JSMR.Application.Scanning.Ports;
namespace JSMR.Application.Scanning;
public sealed class ScanVoiceWorksHandler(
IVoiceWorkScannerRepository scannerRepository,
IVoiceWorkUpdaterRepository updaterRepository,
IDLSiteClient dlsiteClient,
ISpamCircleCache spamCircleCache,
IVoiceWorkSearchUpdater searchUpdater)
{
public async Task<ScanVoiceWorksResponse> HandleAsync(ScanVoiceWorksRequest request, CancellationToken cancellationToken)
{
IVoiceWorksScanner? scanner = scannerRepository.GetScanner(request.Locale);
IVoiceWorkUpdater? updater = updaterRepository.GetUpdater(request.Locale);
if (scanner is null || updater is null)
return new();
VoiceWorkScanOptions options = new(
PageNumber: request.PageNumber,
PageSize: request.PageSize,
ExcludedMakerIds: await spamCircleCache.GetAsync(cancellationToken),
ExcludePartiallyAIGeneratedWorks: true,
ExcludeAIGeneratedWorks: true
);
IReadOnlyList<DLSiteWork> works = await scanner.ScanPageAsync(options, cancellationToken);
if (works.Count == 0)
return new();
string[] productIds = [.. works.Where(x => !string.IsNullOrWhiteSpace(x.ProductId)).Select(x => x.ProductId!)];
VoiceWorkDetailCollection voiceWorkDetails = await dlsiteClient.GetVoiceWorkDetailsAsync(productIds, cancellationToken);
VoiceWorkIngest[] ingests = [.. works.Select(work =>
{
voiceWorkDetails.TryGetValue(work.ProductId!, out VoiceWorkDetails? value);
return VoiceWorkIngest.From(work, value);
})];
VoiceWorkUpsertResult[] upsertResults = await updater.UpsertAsync(ingests, cancellationToken);
int[] voiceWorkIds = [.. upsertResults.Where(x => x.VoiceWorkId.HasValue).Select(x => x.VoiceWorkId!.Value)];
await searchUpdater.UpdateAsync(voiceWorkIds, cancellationToken);
return new()
{
Results = upsertResults
};
}
}