WIP: refactor

This commit is contained in:
Michał Zieliski
2024-06-18 19:53:52 +02:00
parent a0a228f86d
commit 7e8406dc9b
10 changed files with 245 additions and 255 deletions

View File

@@ -23,30 +23,30 @@ namespace WebAPI.dataProcessors
controller = _controller;
}
public void process(Layer processWorker)
public void Process(Layer processWorker)
{
int year = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
int month = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
List<Record>? sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
if (sources!.Count() == 0)
var year = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
var month = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
var sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
if (!sources!.Any())
{
throw new Exception("Source record not found");
}
string? codes = processWorker.Records?.SingleOrDefault(x => x.Code == "Codes")?.Desc1;
var codes = processWorker.Records?.SingleOrDefault(x => x.Code == "Codes")?.Desc1;
if (codes == null)
{
throw new Exception("Codes record not found");
}
// create array of integers from string codes where: '501-503;505-505;510-512' -> [501,502,503,505,510,511,512]
List<int> codesList = ProcessHelper.ParseCodes(codes);
Layer? processedLayer = db.Layers
var codesList = ProcessHelper.ParseCodes(codes);
var processedLayer = db.Layers
.Where(x => x.ParentId == processWorker!.Id
&& !x.IsDeleted)
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault();
bool isNew = false;
var isNew = false;
if (processedLayer == null)
{
isNew = true;
@@ -56,7 +56,7 @@ namespace WebAPI.dataProcessors
Source = "",
Type = LayerType.processed,
ParentId = processWorker!.Id,
Number = db.Layers.Count() + 1,
Number = db.Layers.Count() + 1
};
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month}-AB-T3";
processedLayer.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
@@ -68,33 +68,23 @@ namespace WebAPI.dataProcessors
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
processedLayer.ModifiedAt = DateTime.UtcNow;
List<Layer> dataSources = new List<Layer>();
foreach (Record source in sources!)
{
Layer? dataSource = db.Layers.Where(x =>
x.Type == LayerType.processed &&
!x.IsDeleted &&
x.Name != null && x.Name.Contains($"{year}/{month}-{source.Desc1}-T3")
)
var dataSources = sources!.Select(source => db.Layers.Where(x => x.Type == LayerType.processed && !x.IsDeleted && x.Name != null && x.Name.Contains($"{year}/{month}-{source.Desc1}-T3"))
.Include(x => x.Records)
.FirstOrDefault();
if (dataSource != null)
{
dataSources.Add(dataSource);
}
}
.FirstOrDefault())
.OfType<Layer>()
.ToList();
if (dataSources.Count == 0)
{
throw new Exception($"DataSources are empty");
}
List<Record> newRecords = dataSources
var newRecords = dataSources
.SelectMany(x => x.Records!)
.Where(x => codesList.Contains(int.Parse(x.Code!)))
.Select(x =>
{
Record newRecord = new Record
var newRecord = new Record
{
Id = Guid.NewGuid(),
Code = x.Code,
@@ -103,7 +93,7 @@ namespace WebAPI.dataProcessors
};
for (var i = 1; i < 33; i++)
{
ProcessHelper.SetValue(newRecord, i, ProcessHelper.getValue(x, i));
ProcessHelper.SetValue(newRecord, i, ProcessHelper.GetValue(x, i));
}
return newRecord;
})