AutoImport based on ImportWorkers Layers
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
using System.Xml.Serialization;
|
||||||
using Google.Apis.Sheets.v4;
|
using Google.Apis.Sheets.v4;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@@ -18,6 +20,7 @@ namespace WebAPI.Controllers
|
|||||||
private GoogleDriveHelper googleDriveHelper;
|
private GoogleDriveHelper googleDriveHelper;
|
||||||
private GoogleSheetsHelper googleSheetsHelper;
|
private GoogleSheetsHelper googleSheetsHelper;
|
||||||
private readonly IConfiguration configuration;
|
private readonly IConfiguration configuration;
|
||||||
|
private readonly LogsController logsController;
|
||||||
public LayersController(
|
public LayersController(
|
||||||
AppDbContext _db,
|
AppDbContext _db,
|
||||||
GoogleSheetsHelper _googleSheetsHelper,
|
GoogleSheetsHelper _googleSheetsHelper,
|
||||||
@@ -29,6 +32,7 @@ namespace WebAPI.Controllers
|
|||||||
googleSheetsHelper = _googleSheetsHelper;
|
googleSheetsHelper = _googleSheetsHelper;
|
||||||
googleDriveHelper = _googleDriveHelper;
|
googleDriveHelper = _googleDriveHelper;
|
||||||
configuration = _configuration;
|
configuration = _configuration;
|
||||||
|
logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
@@ -42,7 +46,8 @@ namespace WebAPI.Controllers
|
|||||||
.Where(x => codes.Select(Int32.Parse).ToList().Contains(x.Number))
|
.Where(x => codes.Select(Int32.Parse).ToList().Contains(x.Number))
|
||||||
.OrderByDescending(x => x.Number)
|
.OrderByDescending(x => x.Number)
|
||||||
.Skip(start).Take(limit).ToList());
|
.Skip(start).Take(limit).ToList());
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
return Ok(db.Layers.Where(x => !x.IsDeleted)
|
return Ok(db.Layers.Where(x => !x.IsDeleted)
|
||||||
.OrderByDescending(x => x.Number)
|
.OrderByDescending(x => x.Number)
|
||||||
@@ -62,7 +67,8 @@ namespace WebAPI.Controllers
|
|||||||
Request.Headers.TryGetValue("userId", out var value);
|
Request.Headers.TryGetValue("userId", out var value);
|
||||||
Guid currentUserId = new Guid(value!);
|
Guid currentUserId = new Guid(value!);
|
||||||
return Ok(AddLayer(input, currentUserId).Id);
|
return Ok(AddLayer(input, currentUserId).Id);
|
||||||
} catch (Exception e)
|
}
|
||||||
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return BadRequest(e.ToString());
|
return BadRequest(e.ToString());
|
||||||
}
|
}
|
||||||
@@ -131,26 +137,40 @@ namespace WebAPI.Controllers
|
|||||||
return Unauthorized();
|
return Unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Layer> layersToImport;
|
List<Layer> importWorkerLayers;
|
||||||
try {
|
List<Layer> layersToImport; ;
|
||||||
layersToImport = db.Layers
|
try
|
||||||
.Where(x => x.Records!.Any(x => x.Code == "StartDate" && DateTime.Parse(x.Desc1!) > DateTime.UtcNow))
|
{
|
||||||
.Where(x => x.Records!.Any(x => x.Code == "EndDate" && DateTime.Parse(x.Desc1!) < DateTime.UtcNow))
|
importWorkerLayers = db.Layers
|
||||||
.ToList();
|
.Include(x => x.Records)
|
||||||
} catch (Exception e) {
|
.Where(x => x.Records!.Any(x => x.Code == "Type" && x.Desc1 == "ImportWorker"))
|
||||||
LogEntry entry = new LogEntry
|
.ToList();
|
||||||
|
|
||||||
|
layersToImport = new List<Layer>();
|
||||||
|
foreach (Layer layer in importWorkerLayers)
|
||||||
|
{
|
||||||
|
string startDate = layer.Records!.Where(x => x.Code == "StartDate").First().Desc1!;
|
||||||
|
string endDate = layer.Records!.Where(x => x.Code == "EndDate").First().Desc1!;
|
||||||
|
var startDateParsed = DateTime.ParseExact(startDate, "yyyy.MM.dd", null);
|
||||||
|
var endDateParsed = DateTime.ParseExact(endDate, "yyyy.MM.dd", null);
|
||||||
|
if (startDateParsed.Date <= DateTime.UtcNow.Date && endDateParsed.Date >= DateTime.UtcNow.Date)
|
||||||
|
{
|
||||||
|
processImportWorker(layer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
logsController.AddEntry(new LogEntry
|
||||||
{
|
{
|
||||||
Title = "Import error",
|
Title = "Import error",
|
||||||
Type = LogEntryType.error,
|
Type = LogEntryType.error,
|
||||||
Message = e.ToString(),
|
Message = e.ToString(),
|
||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow
|
||||||
};
|
});
|
||||||
LogsController logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
|
|
||||||
logsController.AddEntry(entry);
|
|
||||||
return BadRequest(e.ToString());
|
return BadRequest(e.ToString());
|
||||||
}
|
}
|
||||||
|
return Ok();
|
||||||
return Ok(layersToImport);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -176,7 +196,7 @@ namespace WebAPI.Controllers
|
|||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("checkDates")]
|
[Route("checkDates")]
|
||||||
public IActionResult checkDates()
|
public IActionResult checkDates()
|
||||||
{
|
{
|
||||||
var warsawTZ = TimeZoneInfo.FindSystemTimeZoneById("Singapore Standard Time");
|
var warsawTZ = TimeZoneInfo.FindSystemTimeZoneById("Singapore Standard Time");
|
||||||
DateTime date = DateTime.UtcNow;
|
DateTime date = DateTime.UtcNow;
|
||||||
@@ -184,9 +204,102 @@ namespace WebAPI.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
private void processImportWorker(Layer importWorker)
|
||||||
|
{
|
||||||
|
// get and check settings
|
||||||
|
string? sheetId = importWorker.Records!.Where(x => x.Code == "SheetId").FirstOrDefault()?.Desc1;
|
||||||
|
if (sheetId == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"SheetId not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
string? sheetTabName = importWorker.Records!.Where(x => x.Code == "SheetTabName").FirstOrDefault()?.Desc1;
|
||||||
|
if (sheetId == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"SheetTabName not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
string? importYearCell = importWorker.Records!.Where(x => x.Code == "ImportYear").FirstOrDefault()?.Desc1;
|
||||||
|
if (importYearCell == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"ImportYear not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
string? importMonthCell = importWorker.Records!.Where(x => x.Code == "ImportMonth").FirstOrDefault()?.Desc1;
|
||||||
|
if (importMonthCell == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"ImportMonth not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
string? importNameCell = importWorker.Records!.Where(x => x.Code == "ImportName").FirstOrDefault()?.Desc1;
|
||||||
|
if (importNameCell == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"ImportName not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
string? checkSumCell = importWorker.Records!.Where(x => x.Code == "SheetId").FirstOrDefault()?.Desc1;
|
||||||
|
if (checkSumCell == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"SheetId not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
string? dataRange = importWorker.Records!.Where(x => x.Code == "DataRange").FirstOrDefault()?.Desc1;
|
||||||
|
if (dataRange == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"DataRange not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
// open excel and read data
|
||||||
|
var nameResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{importNameCell}:{importNameCell}").Execute();
|
||||||
|
string? name = nameResponse.Values[0][0].ToString();
|
||||||
|
if (name == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"ImportName cell is empty, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
var yearResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{importYearCell}:{importYearCell}").Execute();
|
||||||
|
string? year = yearResponse.Values[0][0].ToString();
|
||||||
|
if (year == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"ImportYear cell is empty, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
var monthResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{importMonthCell}:{importMonthCell}").Execute();
|
||||||
|
string? month = monthResponse.Values[0][0].ToString();
|
||||||
|
if (month == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"ImportMonth cell is empty, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
Layer layer = new Layer
|
||||||
|
{
|
||||||
|
Source = "GoogleSheet",
|
||||||
|
Number = db.Layers.Count() + 1
|
||||||
|
};
|
||||||
|
layer.Name = $"L{layer.Number}-I-{name}-{year}/{month}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||||
|
layer.Type = LayerType.import;
|
||||||
|
layer.Records = new List<Record>();
|
||||||
|
var dataRangeResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute();
|
||||||
|
var data = dataRangeResponse.Values;
|
||||||
|
for (int i = 0; i < data[1].Count; i++)
|
||||||
|
{
|
||||||
|
float value;
|
||||||
|
if (
|
||||||
|
data[0][i].ToString()?.Length > 0 &&
|
||||||
|
float.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out value))
|
||||||
|
{
|
||||||
|
Record record = new Record
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
Code = data[0][i].ToString(),
|
||||||
|
Value1 = value,
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
ModifiedAt = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
layer.Records.Add(record);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
AddLayer(layer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||||
|
logsController.AddEntry(new LogEntry
|
||||||
|
{
|
||||||
|
Title = $"Import Success, {importWorker.Name}",
|
||||||
|
Type = LogEntryType.info,
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
private Layer AddLayer(Layer input, Guid currentUserId)
|
private Layer AddLayer(Layer input, Guid currentUserId)
|
||||||
{
|
{
|
||||||
input.Number = db.Layers.Count() + 1;
|
|
||||||
input.CreatedById = currentUserId;
|
input.CreatedById = currentUserId;
|
||||||
input.ModifiedById = currentUserId;
|
input.ModifiedById = currentUserId;
|
||||||
input.CreatedAt = DateTime.UtcNow;
|
input.CreatedAt = DateTime.UtcNow;
|
||||||
@@ -209,7 +322,7 @@ namespace WebAPI.Controllers
|
|||||||
record.CreatedAt = DateTime.UtcNow;
|
record.CreatedAt = DateTime.UtcNow;
|
||||||
record.ModifiedById = currentUserId;
|
record.ModifiedById = currentUserId;
|
||||||
record.ModifiedAt = DateTime.UtcNow;
|
record.ModifiedAt = DateTime.UtcNow;
|
||||||
record.LayerId= id;
|
record.LayerId = id;
|
||||||
|
|
||||||
db.Records.Add(record);
|
db.Records.Add(record);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user