diff --git a/Hcs.WebApp/BackgroundServices/DataParsers/HousesDataParser_15_7_0_1.cs b/Hcs.WebApp/BackgroundServices/DataParsers/HousesDataParser_15_7_0_1.cs index e183220..39303d7 100644 --- a/Hcs.WebApp/BackgroundServices/DataParsers/HousesDataParser_15_7_0_1.cs +++ b/Hcs.WebApp/BackgroundServices/DataParsers/HousesDataParser_15_7_0_1.cs @@ -1,13 +1,40 @@ -using Hcs.Broker; -using Hcs.WebApp.Data.Hcs; +using Hcs.WebApp.Data.Hcs; +using Hcs.WebApp.Services; +using Microsoft.EntityFrameworkCore; namespace Hcs.WebApp.BackgroundServices.DataParsers { public class HousesDataParser_15_7_0_1(IServiceScope scope, Operation operation) : DataParserBase(scope, operation) { - public override Task ParseAsync() + public override async Task ParseAsync() { - throw new NotImplementedException(); + var headquartersService = scope.ServiceProvider.GetRequiredService(); + var fileToParseService = scope.ServiceProvider.GetRequiredService(); + + using var context = headquartersService.GetNewContext(); + var executionStrategy = context.Database.CreateExecutionStrategy(); + await executionStrategy.ExecuteAsync(async () => + { + using var transaction = await context.Database.BeginTransactionAsync(); + try + { + var now = DateTime.UtcNow; + + var fileToParse = await fileToParseService.GetFileToParseByOperationIdAsync(context, operation.Id); + fileToParse.ParsedAt = now; + await context.SaveChangesAsync(); + + await headquartersService.SetOperationEndedAsync(context, operation.Id, now); + + await transaction.CommitAsync(); + } + catch + { + await transaction.RollbackAsync(); + + throw; + } + }); } } } diff --git a/Hcs.WebApp/Services/FileToParseService.cs b/Hcs.WebApp/Services/FileToParseService.cs index 5062e75..f8bb340 100644 --- a/Hcs.WebApp/Services/FileToParseService.cs +++ b/Hcs.WebApp/Services/FileToParseService.cs @@ -27,6 +27,11 @@ namespace Hcs.WebApp.Services return fileToParse; } + public async Task GetFileToParseByOperationIdAsync(HcsDbContext context, int operationId) + { + return await context.FilesToParse.SingleAsync(x => x.LastParseOperationId == operationId); + } + public async Task SetOperationToFileToParseAsync(HcsDbContext context, int fileToParseId, Operation operation) { var fileToParse = await context.FilesToParse.FirstOrDefaultAsync(x => x.Id == fileToParseId);