From ac7b01482ecee9f0c8c7bb46468a97f91ab18c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Tue, 3 Oct 2023 22:29:42 +0200 Subject: [PATCH] CopyProcessor: update existing layer --- Frontend/src/app/models/layer.model.ts | 1 + .../layer-detail/layer-detail.component.html | 7 ++- .../layer-detail/layer-detail.component.ts | 1 + Utils/SQL_HELPERS.sql | 46 +++++++------- WebAPI/Controllers/LayersController.cs | 14 ++--- WebAPI/dataProcessors/copy.processor.cs | 62 +++++++++++++------ 6 files changed, 81 insertions(+), 50 deletions(-) diff --git a/Frontend/src/app/models/layer.model.ts b/Frontend/src/app/models/layer.model.ts index 9cd0472..6399662 100644 --- a/Frontend/src/app/models/layer.model.ts +++ b/Frontend/src/app/models/layer.model.ts @@ -19,6 +19,7 @@ export class Layer extends Base { name?: string; records: Record[] = []; created?: string; + modified?: string; type?: LayerType; constructor(data: Partial = {}) { diff --git a/Frontend/src/app/modules/layers/layer-detail/layer-detail.component.html b/Frontend/src/app/modules/layers/layer-detail/layer-detail.component.html index 65b98fd..10e3da4 100644 --- a/Frontend/src/app/modules/layers/layer-detail/layer-detail.component.html +++ b/Frontend/src/app/modules/layers/layer-detail/layer-detail.component.html @@ -30,7 +30,12 @@ -
+
+ + Modified + + +
t.value1 || 0).reduce((acc, value) => acc + value, 0); this.createColumns(); } diff --git a/Utils/SQL_HELPERS.sql b/Utils/SQL_HELPERS.sql index f16a56a..adc9f70 100644 --- a/Utils/SQL_HELPERS.sql +++ b/Utils/SQL_HELPERS.sql @@ -1,62 +1,64 @@ --INSERT INTO [diunabi-morska].[dbo].[Layers] --([Id], [Number], [Source], [Name], [CreatedAt], [ModifiedAt], [IsDeleted], --[CreatedById], [ModifiedById], [Type]) ---VALUES ('f43e69f8-b89a-4502-963d-8c1a381a0422', 78, 'Manual', 'L77-A-IW_K5-2023/10-202310021927', +--VALUES ('2cadf61e-2653-4cda-aaef-d59e9e4c1c26', 84, 'Manual', 'L84-A-IW_PU-2023/10-202310031948', --GETDATE(), GETDATE(), 0, '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 2); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('f43e69f8-b89a-4502-963d-8c1a381a0422', 'StartDate', '2023.09.30', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('1e6b8736-5125-4c00-8da7-1bd7182ef49d', 'StartDate', '2023.09.30', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('f37eb334-8ca2-4232-8809-064a6d1cfadd', 'EndDate', '2023.11.05', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('608af36d-6a01-454f-94dc-1ac549f84d5f', 'EndDate', '2023.11.05', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('069bc419-3baa-4a2a-a115-0afc9c4c4cb1', 'Source', 'GoogleSheet', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('d839d2d7-d655-4fef-b690-e58ee96e5d8c', 'Source', 'GoogleSheet', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('47862b96-1a05-4426-9c67-4e575edeb4b0', 'SheetTabName', 'Pazdziernik_2023', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('0be4c79e-31d4-4542-99f0-a053d5cf36e1', 'SheetTabName', 'Pazdziernik_2023', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('d478ba86-5943-462c-8103-3e1b4c10c2e3', 'SheetId', '1ZzndU8HjYqz5VKCcrVHBOFW8fqpYfwquclznX9q39Yk', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('6bdf8b46-cb77-4b7c-a647-3236992ad2bd', 'SheetId', '1gnJhqdlL622GOJEuAiVzxJu1N1ZlSb4a-JvGB1rwHAo', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('f2161c73-9386-461e-b71b-88ac5178438a', 'DataRange', 'C4:AR5', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('a8cf688f-7996-4dda-8ede-4c0d57067684', 'DataRange', 'D4:AS5', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('52e1e356-0129-4c70-8cb2-195a867f02b9', 'ImportName', 'C3', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('56b2eec0-6aec-499f-b1bc-d4967a51191e', 'ImportName', 'D3', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('5380f086-b5a2-4c27-98cd-a0a0e8f57779', 'ImportMonth', 'E3', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('0b08633b-9857-404b-9b47-ae0d79e2e9d7', 'ImportMonth', 'F3', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('90cd358e-a67d-44d8-b2e1-ce15f305050b', 'ImportYear', 'D3', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('44a34bf2-f870-4d64-82db-4d7071ddffe5', 'ImportYear', 'E3', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('d8446dd7-a4ca-48b0-81a3-0abc23fe0525', 'CheckSum', 'F3', ---GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'f43e69f8-b89a-4502-963d-8c1a381a0422'); +--VALUES ('c6ac4715-a4ad-464a-9597-7e6a4fdc5208', 'CheckSum', 'G3', +--GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '2cadf61e-2653-4cda-aaef-d59e9e4c1c26'); -- --INSERT INTO [diunabi-morska].[dbo].[Records] --([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) ---VALUES ('c59d2c84-05a2-4a0d-b7a2-5bd9accb342e', 'Type', 'ImportWorker', +--VALUES ('eb79a132-dd99-4e97-b3ba-5e2544bc921c', 'Type', 'ImportWorker', --GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, '1d4ad0b1-2a36-413d-838d-881b2f7cc8dc'); +-- + --DELETE FROM [diunabi-morska].[dbo].[Records] WHERE [LayerId] = 'bb4cc1ae-a94e-424d-9aa3-08dbba1c92b1'; --DELETE FROM [diunabi-morska].[dbo].[Layers] WHERE [Id] = 'bb4cc1ae-a94e-424d-9aa3-08dbba1c92b1'; diff --git a/WebAPI/Controllers/LayersController.cs b/WebAPI/Controllers/LayersController.cs index 2ecf5a0..84bd298 100644 --- a/WebAPI/Controllers/LayersController.cs +++ b/WebAPI/Controllers/LayersController.cs @@ -252,18 +252,14 @@ namespace WebAPI.Controllers switch (processType) { case "Copy": - CopyProcessor cp = new CopyProcessor(db, googleSheetValues); - processedLayer = cp.process(sourceLayer, processWorker?.Id); + CopyProcessor cp = new CopyProcessor(db, googleSheetValues, this); + cp.process(sourceLayer, processWorker?.Id); break; case "Deaggregation": DeaggregationProcessor dp = new DeaggregationProcessor(db, googleSheetValues); processedLayer = dp.process(sourceLayer, processWorker?.Id); break; } - if (processedLayer != null) - { - AddLayer(processedLayer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D")); - } } } } @@ -394,7 +390,7 @@ namespace WebAPI.Controllers }); } - private Layer AddLayer(Layer input, Guid currentUserId) + public Layer AddLayer(Layer input, Guid currentUserId) { input.CreatedById = currentUserId; input.ModifiedById = currentUserId; @@ -406,7 +402,7 @@ namespace WebAPI.Controllers return input; } - private void SaveRecords(Guid id, ICollection records, Guid currentUserId) + public void SaveRecords(Guid id, ICollection records, Guid currentUserId) { try { @@ -432,7 +428,7 @@ namespace WebAPI.Controllers foreach (ProcessSource processSource in sources) { Console.WriteLine($"{processSource.LayerId} {processSource.SourceId}"); - // db.ProcessSources.Add(processSource); + db.ProcessSources.Add(processSource); } } catch (Exception) diff --git a/WebAPI/dataProcessors/copy.processor.cs b/WebAPI/dataProcessors/copy.processor.cs index e8fa63f..5038140 100644 --- a/WebAPI/dataProcessors/copy.processor.cs +++ b/WebAPI/dataProcessors/copy.processor.cs @@ -1,8 +1,10 @@ -using Google.Apis.Sheets.v4; +using Azure; +using Google.Apis.Sheets.v4; using Google.Apis.Sheets.v4.Data; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json.Linq; using System.Globalization; +using WebAPI.Controllers; using WebAPI.Models; namespace WebAPI.dataProcessors @@ -11,16 +13,19 @@ namespace WebAPI.dataProcessors { private AppDbContext db; private SpreadsheetsResource.ValuesResource googleSheetValues; + private LayersController controller; public CopyProcessor( AppDbContext _db, - SpreadsheetsResource.ValuesResource _googleSheetValues) + SpreadsheetsResource.ValuesResource _googleSheetValues, + LayersController _controller) { db = _db; googleSheetValues = _googleSheetValues; + controller = _controller; } - public Layer process(Layer sourceImportWorker, Guid? parentId) + public void process(Layer sourceImportWorker, Guid? parentId) { string? sheetId = sourceImportWorker.Records!.Where(x => x.Code == "SheetId").FirstOrDefault()?.Desc1; if (sheetId == null) @@ -65,29 +70,48 @@ namespace WebAPI.dataProcessors { throw new Exception($"ImportMonth cell is empty, {sourceImportWorker.Name}"); } + + Layer? processedLayer = db.Layers + .Where(x => x.ParentId == parentId) + .OrderByDescending(x => x.CreatedAt) + .FirstOrDefault(); + + if (processedLayer == null) + { + processedLayer = new Layer + { + Id = Guid.NewGuid(), + Source = "", + Type = LayerType.processed, + ParentId = parentId, + Number = db.Layers.Count() + 1, + }; + processedLayer.Name = $"L{processedLayer.Number}-P-{name}-{year}/{month}-{DateTime.Now.ToString("yyyyMMddHHmm")}"; + processedLayer.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"); + processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"); + processedLayer.CreatedAt = DateTime.UtcNow; + processedLayer.ModifiedAt = DateTime.UtcNow; + db.Layers.Add(processedLayer); + } + + processedLayer.Records = new List(); + processedLayer.Sources = new List(); + processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"); + processedLayer.ModifiedAt = DateTime.UtcNow; - Layer response = new Layer - { - Id = Guid.NewGuid(), - Source = "", - Type = LayerType.processed, - ParentId = parentId, - Number = db.Layers.Count() + 1, - Records = new List(), - Sources = new List() - }; Layer dataSource = db.Layers .Include(x => x.Records) .Where(x => x.ParentId == sourceImportWorker.Id && !x.IsDeleted) .OrderByDescending(x => x.CreatedAt) .First(); - response.Sources.Add(new ProcessSource + /* + processedLayer.Sources.Add(new ProcessSource { - LayerId = response.Id, + LayerId = processedLayer.Id, SourceId = dataSource.Id }); - response.Name = $"L{response.Number}-P-{name}-{year}/{month}-{DateTime.Now.ToString("yyyyMMddHHmm")}"; + */ foreach (Record source in dataSource.Records!) { @@ -99,9 +123,11 @@ namespace WebAPI.dataProcessors CreatedAt = DateTime.UtcNow, ModifiedAt = DateTime.UtcNow }; - response.Records.Add(record); + processedLayer.Records.Add(record); } - return response; + controller.SaveRecords(processedLayer.Id, processedLayer.Records, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D")); + db.Layers.Update(processedLayer); + db.SaveChanges(); } } }