Added logic to remove supported languages that are no longer supported, rather than just being purely additive. Added ApiClient logging.
This commit is contained in:
@@ -22,8 +22,21 @@ public abstract class ApiClient(HttpClient http, ILogger logger, JsonSerializerO
|
||||
|
||||
Stream stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return await JsonSerializer.DeserializeAsync<TResponse>(stream, json, cancellationToken).ConfigureAwait(false)
|
||||
?? throw new InvalidOperationException($"Failed to deserialize JSON to {typeof(TResponse).Name} from {url}.");
|
||||
try
|
||||
{
|
||||
return await JsonSerializer.DeserializeAsync<TResponse>(stream, json, cancellationToken).ConfigureAwait(false)
|
||||
?? throw new InvalidOperationException($"Failed to deserialize JSON to {typeof(TResponse).Name} from {url}.");
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
logger.LogError(ex,
|
||||
"Failed to deserialize JSON from {Url}. ContentLengthHeader={ContentLengthHeader}",
|
||||
url,
|
||||
response.Content.Headers.ContentLength);
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected async Task<TResponse> GetJsonpAsync<TResponse>(
|
||||
|
||||
@@ -439,6 +439,14 @@ public class VoiceWorkUpdater(AppDbContext dbContext, ITimeProvider timeProvider
|
||||
dbContext.VoiceWorkSupportedLanguages.Add(voiceWorkSupportedLanguage);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (string existingLinkCode in existingLanguageLinks.Keys)
|
||||
{
|
||||
if (!ingest.SupportedLanguages.Any(x => x.Code == existingLinkCode))
|
||||
{
|
||||
dbContext.VoiceWorkSupportedLanguages.Remove(existingLanguageLinks[existingLinkCode]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpsertSeries(VoiceWorkIngest ingest, VoiceWorkUpsertContext upsertContext)
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using JSMR.Application.Scanning.Contracts;
|
||||
using JSMR.Domain.Entities;
|
||||
using JSMR.Domain.ValueObjects;
|
||||
using JSMR.Infrastructure.Data;
|
||||
using JSMR.Tests.Fixtures;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Shouldly;
|
||||
|
||||
namespace JSMR.Tests.Ingestion.Japanese;
|
||||
|
||||
public class Update_Supported_Langauge_Tests(MariaDbContainerFixture container) : JapaneseIngestionTestsBase(container)
|
||||
{
|
||||
[Fact]
|
||||
public async Task Update_Supported_Langauge()
|
||||
{
|
||||
VoiceWorkIngest ingest = new()
|
||||
{
|
||||
MakerId = "RG1",
|
||||
MakerName = "The Maker",
|
||||
ProductId = "RJ1",
|
||||
Title = "Title",
|
||||
Description = "Description",
|
||||
SupportedLanguages = [SupportedLanguage.Japanese]
|
||||
};
|
||||
|
||||
await using AppDbContext dbContext = await GetAppDbContextAsync();
|
||||
DateTime currentDateTime = TokyoLocalToUtc(2025, 01, 05, 10, 0, 0);
|
||||
|
||||
await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 05, 10, 0, 0), ingest, [SupportedLanguage.Japanese]);
|
||||
|
||||
// Add English
|
||||
VoiceWorkIngest addSupportedLanguageIngest = ingest with
|
||||
{
|
||||
SupportedLanguages = [SupportedLanguage.Japanese, SupportedLanguage.English]
|
||||
};
|
||||
|
||||
await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 05, 10, 0, 0), addSupportedLanguageIngest, [SupportedLanguage.Japanese, SupportedLanguage.English]);
|
||||
|
||||
// Remove Japanese
|
||||
VoiceWorkIngest removeSupportedLanguageIngest = ingest with
|
||||
{
|
||||
SupportedLanguages = [SupportedLanguage.English]
|
||||
};
|
||||
|
||||
await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 05, 10, 0, 0), removeSupportedLanguageIngest, [SupportedLanguage.English]);
|
||||
}
|
||||
|
||||
private static async Task UpsertAndVerify(AppDbContext dbContext, DateTime dateTime, VoiceWorkIngest ingest, SupportedLanguage[] expectedSupportedLanguages)
|
||||
{
|
||||
await UpsertAsync(dbContext, dateTime, [ingest]);
|
||||
|
||||
VoiceWork? voiceWork = await dbContext.VoiceWorks
|
||||
.Include(x => x.SupportedLanguages)
|
||||
.SingleAsync(v => v.ProductId == ingest.ProductId, TestContext.Current.CancellationToken);
|
||||
|
||||
voiceWork.ShouldNotBeNull();
|
||||
|
||||
string[] languageCodes = [.. voiceWork.SupportedLanguages.Select(x => x.Language).OrderBy(x => x)];
|
||||
string[] expectedLanguageCode = [.. expectedSupportedLanguages.Select(x => x.Code).OrderBy(x => x)];
|
||||
|
||||
languageCodes.ShouldBe(expectedLanguageCode);
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ public class Update_Upcoming_With_No_Expected_Date_Tests(MariaDbContainerFixture
|
||||
};
|
||||
|
||||
// Should be exactly the same
|
||||
await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 05, 10, 0, 0), ingest, new DateTime(2025, 1, 21));
|
||||
await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 05, 10, 0, 0), updatedIngest, new DateTime(2025, 1, 21));
|
||||
}
|
||||
|
||||
private static async Task UpsertAndVerify(AppDbContext dbContext, DateTime dateTime, VoiceWorkIngest ingest, DateTime? expectedDate)
|
||||
|
||||
@@ -28,7 +28,9 @@
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" />
|
||||
<PackageReference Include="Serilog" Version="4.3.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
|
||||
<PackageReference Include="Spectre.Console" Version="0.54.0" />
|
||||
<PackageReference Include="System.CommandLine" Version="2.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using System.CommandLine;
|
||||
using System.Text;
|
||||
|
||||
@@ -30,7 +31,7 @@ string connectionString = builder.Configuration.GetConnectionString("AppDb")
|
||||
|
||||
//builder.Services.AddSerilog(o => o
|
||||
// .WriteTo.Console()
|
||||
// .MinimumLevel.Information());
|
||||
// .MinimumLevel.Warning());
|
||||
|
||||
builder.Services
|
||||
.AddApplication()
|
||||
|
||||
Reference in New Issue
Block a user