Implement registry update

This commit is contained in:
2025-11-01 19:55:37 +09:00
parent 737e581f75
commit a9fd99bab8
4 changed files with 56 additions and 3 deletions

View File

@ -1,6 +1,7 @@
using Hcs.Broker; using Hcs.Broker;
using Hcs.WebApp.Data.Hcs; using Hcs.WebApp.Data.Hcs;
using Hcs.WebApp.Services; using Hcs.WebApp.Services;
using Newtonsoft.Json;
namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon
{ {
@ -16,9 +17,39 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon
if (result.Success) if (result.Success)
{ {
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>(); var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
var registry = await registryService.GetRegistryByOperationIdAsync(operation.Id);
// TODO using var context = headquartersService.GetNewContext();
using var transaction = await context.Database.BeginTransactionAsync();
try
{
var registry = await registryService.GetRegistryByOperationIdAsync(context, operation.Id, true);
registry.Elements.Clear();
await context.SaveChangesAsync();
foreach (var element in result.Result!.NsiElement)
{
var registryElement = new RegistryElement()
{
Code = element.Code,
GUID = element.GUID,
Json = JsonConvert.SerializeObject(element)
};
registry.Elements.Add(registryElement);
}
await context.SaveChangesAsync();
await headquartersService.SetOperationEndedAsync(context, operation.Id);
await transaction.CommitAsync();
}
catch
{
transaction?.Rollback();
throw;
}
return true; return true;
} }

View File

@ -12,6 +12,6 @@
public string GUID { get; set; } public string GUID { get; set; }
public virtual string Xml { get; set; } public virtual string Json { get; set; }
} }
} }

View File

@ -122,6 +122,17 @@ namespace Hcs.WebApp.Services
} }
} }
public async Task SetOperationEndedAsync(HcsDbContext context, int operationId)
{
var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
if (operation != null)
{
operation.EndedAt = DateTime.UtcNow;
await context.SaveChangesAsync();
}
}
public async Task SetOperationEndedWithFailAsync(int operationId, string failureReason) public async Task SetOperationEndedWithFailAsync(int operationId, string failureReason)
{ {
using var context = GetNewContext(); using var context = GetNewContext();

View File

@ -21,6 +21,17 @@ namespace Hcs.WebApp.Services
public async Task<Registry> GetRegistryByOperationIdAsync(int operationId) public async Task<Registry> GetRegistryByOperationIdAsync(int operationId)
{ {
using var context = GetNewContext(); using var context = GetNewContext();
return await GetRegistryByOperationIdAsync(context, operationId);
}
public async Task<Registry> GetRegistryByOperationIdAsync(HcsDbContext context, int operationId, bool includeElements = false)
{
if (includeElements)
{
return await context.Registries
.Include(x => x.Elements)
.SingleAsync(x => x.LastSyncOperationId == operationId);
}
return await context.Registries.SingleAsync(x => x.LastSyncOperationId == operationId); return await context.Registries.SingleAsync(x => x.LastSyncOperationId == operationId);
} }