Logging refactor
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services;
|
||||
@@ -8,20 +10,33 @@ namespace DiunaBI.Plugins.Morska.Processors;
|
||||
public class T3SourceYearSummaryProcessor : MorskaBaseProcessor
|
||||
{
|
||||
public override string ProcessorType => "T3.SourceYearSummary";
|
||||
|
||||
private readonly AppDbContext _db;
|
||||
private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
|
||||
private readonly ILogger<T3SourceYearSummaryProcessor> _logger;
|
||||
|
||||
public T3SourceYearSummaryProcessor(
|
||||
AppDbContext db)
|
||||
AppDbContext db,
|
||||
SpreadsheetsResource.ValuesResource googleSheetValues,
|
||||
ILogger<T3SourceYearSummaryProcessor> logger)
|
||||
{
|
||||
_db = db;
|
||||
_googleSheetValues = googleSheetValues;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override void Process(Layer processWorker)
|
||||
{
|
||||
_logger.LogInformation("T3SourceYearSummary: Starting processing for {ProcessWorkerName} ({ProcessWorkerId})",
|
||||
processWorker.Name, processWorker.Id);
|
||||
|
||||
var year = processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1;
|
||||
var source = processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
|
||||
if (source == null)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
var processedLayer = _db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
@@ -101,8 +116,32 @@ public class T3SourceYearSummaryProcessor : MorskaBaseProcessor
|
||||
{
|
||||
_db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
|
||||
SaveRecords(processedLayer.Id, newRecords);
|
||||
_db.SaveChanges();
|
||||
|
||||
_logger.LogInformation("T3SourceYearSummary: Successfully completed processing for {ProcessWorkerName} ({ProcessWorkerId})",
|
||||
processWorker.Name, processWorker.Id);
|
||||
}
|
||||
|
||||
private void SaveRecords(Guid layerId, ICollection<Record> records)
|
||||
{
|
||||
var toDelete = _db.Records.Where(x => x.LayerId == layerId).ToList();
|
||||
if (toDelete.Count > 0)
|
||||
{
|
||||
_db.Records.RemoveRange(toDelete);
|
||||
}
|
||||
|
||||
foreach (var record in records)
|
||||
{
|
||||
record.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
record.CreatedAt = DateTime.UtcNow;
|
||||
record.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
record.ModifiedAt = DateTime.UtcNow;
|
||||
record.LayerId = layerId;
|
||||
_db.Records.Add(record);
|
||||
}
|
||||
|
||||
_logger.LogDebug("T3SourceYearSummary: Saved {RecordCount} records for layer {LayerId}", records.Count, layerId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user