From c2a98e0386bbd532dd27f34fd4afc2adb30ba9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieliski?= Date: Tue, 18 Jun 2024 18:39:02 +0200 Subject: [PATCH] WIP: Resolve all code issues --- WebAPI/AppDbContext.cs | 14 +- WebAPI/Calculators/BaseCalc.cs | 118 +++-- WebAPI/Controllers/AdminController.cs | 63 ++- WebAPI/Controllers/LayersController.cs | 452 ++++++++---------- WebAPI/Controllers/PingController.cs | 18 +- WebAPI/GoogleDriveHelper.cs | 61 ++- WebAPI/GoogleSheetsHelper.cs | 61 ++- WebAPI/Program.cs | 9 +- .../t3.MultiSourceSummary.processor.cs | 4 +- .../t3.MultiSourceYearSummary.processor.cs | 4 +- 10 files changed, 363 insertions(+), 441 deletions(-) diff --git a/WebAPI/AppDbContext.cs b/WebAPI/AppDbContext.cs index 4c9b8de..9ad081e 100644 --- a/WebAPI/AppDbContext.cs +++ b/WebAPI/AppDbContext.cs @@ -6,10 +6,10 @@ namespace WebAPI { public class AppDbContext : DbContext { - public DbSet Users { get; set; } - public DbSet Layers { get; set; } - public DbSet Records { get; set; } - public DbSet ProcessSources { get; set; } + public DbSet Users { get; init; } + public DbSet Layers { get; init; } + public DbSet Records { get; init; } + public DbSet ProcessSources { get; init; } public AppDbContext(DbContextOptions options) : base(options) { @@ -24,14 +24,14 @@ namespace WebAPI }); } - public static readonly Microsoft.Extensions.Logging.LoggerFactory _myLoggerFactory = - new LoggerFactory(new[] { + private static readonly LoggerFactory MyLoggerFactory = + new(new[] { new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider() }); protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseLoggerFactory(_myLoggerFactory); + optionsBuilder.UseLoggerFactory(MyLoggerFactory); } } } diff --git a/WebAPI/Calculators/BaseCalc.cs b/WebAPI/Calculators/BaseCalc.cs index 15d2891..94e7927 100644 --- a/WebAPI/Calculators/BaseCalc.cs +++ b/WebAPI/Calculators/BaseCalc.cs @@ -2,62 +2,57 @@ using System.Globalization; using DiunaBIWebAPI.dataProcessors; using WebAPI.Models; using AngouriMath; -using AngouriMath.Extensions; -namespace WebAPI.Calculator +namespace WebAPI.Calculator; + +public class BaseCalc { - public class BaseCalc + public string Expression { get; } + private string ResultCode { get; set; } + private string Formula { get; } + + public BaseCalc(string expression) { - public string Expresion { get; set; } - - private string ResultCode { get; set; } - private string Formula { get; set; } + Expression = expression; + Formula = Expression.Split("=")[1]; + ResultCode = Expression.Split("=")[0]; + } - public BaseCalc(string expresion) + public bool IsFormulaCorrect() + { + // check left side of expression + if (!ResultCode.StartsWith("[") || !ResultCode.EndsWith("]")) { - Expresion = expresion; - Formula = Expresion.Split("=")[1]; - ResultCode = Expresion.Split("=")[0]; + return false; + } + if (!ResultCode.Substring(1, ResultCode.Length - 2).All(char.IsDigit)) + { + return false; } - public bool IsFormulaCorrect() - { - // check left side of expression - if (!ResultCode.StartsWith("[") || !ResultCode.EndsWith("]")) - { - return false; - } - if (!ResultCode.Substring(1, ResultCode.Length - 2).All(char.IsDigit)) - { - return false; - } + ResultCode = ResultCode.Substring(1, ResultCode.Length - 2); - ResultCode = ResultCode.Substring(1, ResultCode.Length - 2); - - // check right side of expression - if (!(!string.IsNullOrEmpty(Formula) && - Formula.All(c => char.IsDigit(c) || c == '[' || c == ']' || c == '+'))) - { - return false; - } - - return true; - } + // check right side of expression + return !string.IsNullOrEmpty(Formula) && + Formula.All(c => char.IsDigit(c) || c == '[' || c == ']' || c == '+'); + } - public Record CalculateT3(List records) + public Record CalculateT3(List records) + { + var resultCode = ResultCode; { - Record result = new Record + var result = new Record { Id = Guid.NewGuid(), - Code = this.ResultCode, + Code = resultCode, CreatedAt = DateTime.UtcNow, - ModifiedAt = DateTime.UtcNow, + ModifiedAt = DateTime.UtcNow }; - List codes = GetCodes(); - List ingredients = new List(); - foreach (string code in codes) + var codes = GetCodes(); + var ingredients = new List(); + foreach (var code in codes) { - Record? ingredient = records.FirstOrDefault(r => r.Code == code); + var ingredient = records.FirstOrDefault(r => r.Code == code); if (ingredient == null) { throw new Exception($"Record for code {code} not found."); @@ -65,44 +60,39 @@ namespace WebAPI.Calculator ingredients.Add(ingredient); } - for (int i = 1; i <= 32; i++) + for (var i = 1; i <= 32; i++) { - string formula = Formula; - foreach (Record ingredient in ingredients) - { - formula = formula.Replace($"[{ingredient.Code}]", ProcessHelper.getValue(ingredient, i)?.ToString(CultureInfo.InvariantCulture)); - } - if (formula.Contains("[")) + var formula = ingredients.Aggregate(Formula, (current, ingredient) => current.Replace($"[{ingredient.Code}]", ProcessHelper.getValue(ingredient, i)?.ToString(CultureInfo.InvariantCulture))); + if (formula.Contains('[')) { throw new Exception($"Not all placeholders were replaced. Value{i} [{formula}]"); } Entity expr = formula; - double val = (double)expr.EvalNumerical(); ProcessHelper.setValue(result, i, (double)expr.EvalNumerical()); } return result; } + } - private List GetCodes() + private List GetCodes() + { + var codes = new List(); + var endIndex = -1; + + while (true) { - List codes = new List(); - int endIndex = -1; + var startIndex = Formula.IndexOf("[", endIndex + 1, StringComparison.CurrentCulture); + endIndex = Formula.IndexOf("]", startIndex + 1, StringComparison.CurrentCulture); - while (true) + if (startIndex == -1 || endIndex == -1) { - int startIndex = Formula.IndexOf("[", endIndex + 1, StringComparison.CurrentCulture); - endIndex = Formula.IndexOf("]", startIndex + 1, StringComparison.CurrentCulture); - - if (startIndex == -1 || endIndex == -1) - { - break; - } - - string valueCode = Formula.Substring(startIndex + 1, endIndex - startIndex - 1); - codes.Add(valueCode); + break; } - return codes; + + var valueCode = Formula.Substring(startIndex + 1, endIndex - startIndex - 1); + codes.Add(valueCode); } + return codes; } } \ No newline at end of file diff --git a/WebAPI/Controllers/AdminController.cs b/WebAPI/Controllers/AdminController.cs index 4227d58..1471af8 100644 --- a/WebAPI/Controllers/AdminController.cs +++ b/WebAPI/Controllers/AdminController.cs @@ -1,13 +1,7 @@ using System.Data; -using System.Globalization; -using System.Xml.Serialization; -using Google.Apis.Sheets.v4; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; -using Microsoft.EntityFrameworkCore; -using WebAPI.dataParsers; -using WebAPI.Exports; using WebAPI.Models; using static Google.Apis.Drive.v3.FilesResource; @@ -15,26 +9,20 @@ namespace WebAPI.Controllers { [ApiController] [Route("api/[controller]")] - public class AdminController : Controller { - private readonly AppDbContext db; - private GoogleDriveHelper googleDriveHelper; - private GoogleSheetsHelper googleSheetsHelper; - private readonly IConfiguration configuration; - private readonly LogsController logsController; + private readonly GoogleDriveHelper _googleDriveHelper; + private readonly IConfiguration _configuration; + private readonly LogsController _logsController; public AdminController( - AppDbContext _db, - GoogleSheetsHelper _googleSheetsHelper, - GoogleDriveHelper _googleDriveHelper, - IConfiguration _configuration) + GoogleDriveHelper googleDriveHelper, + IConfiguration configuration, + LogsController logsController) { - db = _db; - googleSheetsHelper = _googleSheetsHelper; - googleDriveHelper = _googleDriveHelper; - configuration = _configuration; - logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration); + _googleDriveHelper = googleDriveHelper; + _configuration = configuration; + _logsController = logsController; } [HttpGet] @@ -42,18 +30,19 @@ namespace WebAPI.Controllers [AllowAnonymous] public IActionResult BackupDatabase(string apiKey) { - if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"]) + if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"]) { return Unauthorized(); } + try { - string databaseName = "diunabi-morska"; - string localDatabasePath = $"{configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak"; + var databaseName = "diunabi-morska"; + var localDatabasePath = $"{_configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak"; var formatMediaName = $"DatabaseToolkitBackup_{databaseName}"; var formatName = $"Full Backup of {databaseName}"; - var connection = new SqlConnection(configuration.GetConnectionString("SQLDatabase")); + var connection = new SqlConnection(_configuration.GetConnectionString("SQLDatabase")); var sql = @"BACKUP DATABASE @databaseName TO DISK = @localDatabasePath @@ -73,24 +62,26 @@ namespace WebAPI.Controllers command.ExecuteNonQuery(); - Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File(); - body.Name = Path.GetFileName(localDatabasePath); - body.Parents = new List { configuration["GDriveBackupDirectory"] }; - body.MimeType = "application/octet-stream"; + Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File + { + Name = Path.GetFileName(localDatabasePath), + Parents = new List { _configuration["GDriveBackupDirectory"] }, + MimeType = "application/octet-stream" + }; var fsSource = new FileStream(localDatabasePath, FileMode.Open, FileAccess.Read); - if (googleDriveHelper.Service is null) + if (_googleDriveHelper.Service is null) { throw new Exception("Google Drive API not initialized"); } - CreateMediaUpload request = googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType); + + var request = _googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType); request.Fields = "id"; - var response = request.Upload(); - - - logsController.AddEntry(new LogEntry + request.Upload(); + + _logsController.AddEntry(new LogEntry { Title = "Backup success", Type = LogEntryType.info, @@ -101,7 +92,7 @@ namespace WebAPI.Controllers } catch (Exception e) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = "Backup error", Type = LogEntryType.error, diff --git a/WebAPI/Controllers/LayersController.cs b/WebAPI/Controllers/LayersController.cs index 4f8cbfe..c657d5e 100644 --- a/WebAPI/Controllers/LayersController.cs +++ b/WebAPI/Controllers/LayersController.cs @@ -15,29 +15,29 @@ namespace WebAPI.Controllers [Route("api/[controller]")] public class LayersController : Controller { - private readonly AppDbContext db; - private SpreadsheetsResource.ValuesResource? googleSheetValues; - private GoogleDriveHelper googleDriveHelper; - private GoogleSheetsHelper googleSheetsHelper; - private readonly IConfiguration configuration; - private readonly LogsController logsController; + private readonly AppDbContext _db; + private readonly SpreadsheetsResource.ValuesResource? _googleSheetValues; + private readonly GoogleDriveHelper _googleDriveHelper; + // private GoogleSheetsHelper _googleSheetsHelper; + private readonly IConfiguration _configuration; + private readonly LogsController _logsController; public LayersController( - AppDbContext _db, - GoogleSheetsHelper _googleSheetsHelper, - GoogleDriveHelper _googleDriveHelper, - IConfiguration _configuration) + AppDbContext db, + GoogleSheetsHelper googleSheetsHelper, + GoogleDriveHelper googleDriveHelper, + IConfiguration configuration) { - db = _db; - if (_googleSheetsHelper.Service is not null) + _db = db; + if (googleSheetsHelper.Service is not null) { - googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values; + _googleSheetValues = googleSheetsHelper.Service.Spreadsheets.Values; } - googleSheetsHelper = _googleSheetsHelper; - googleDriveHelper = _googleDriveHelper; - configuration = _configuration; - logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration); + //_googleSheetsHelper = googleSheetsHelper; + _googleDriveHelper = googleDriveHelper; + _configuration = configuration; + _logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration); } [HttpGet] @@ -45,7 +45,7 @@ namespace WebAPI.Controllers { try { - IQueryable response = db.Layers.Where(x => !x.IsDeleted); + var response = _db.Layers.Where(x => !x.IsDeleted); if (name != null) { response = response.Where(x => x.Name != null && x.Name.Contains(name)); @@ -66,33 +66,15 @@ namespace WebAPI.Controllers } } - [HttpPost] - public IActionResult Save(Layer input) - { - try - { - //Request.Headers.TryGetValue("userId", out var value); - //Guid currentUserId = new Guid(value!); - - - return Ok(); - } - catch (Exception e) - { - return BadRequest(e.ToString()); - } - } - [HttpGet] [Route("{id}")] public IActionResult Get(Guid id) { try { - return Ok(db.Layers + return Ok(_db.Layers .Include(x => x.CreatedBy) - .Include(x => x.Records) - .Where(x => x.Id == id && !x.IsDeleted).First()); + .Include(x => x.Records).First(x => x.Id == id && !x.IsDeleted)); } catch (Exception e) { @@ -104,9 +86,9 @@ namespace WebAPI.Controllers [Route("getForPowerBI/{apiKey}/{number}")] public IActionResult GetByNumber(string apiKey, int number) { - if (apiKey != configuration["apiKey"]) + if (apiKey != _configuration["apiKey"]) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = $"Unauthorized request - wrong apiKey ({number})", Type = LogEntryType.warning, @@ -121,7 +103,7 @@ namespace WebAPI.Controllers if ( !Request.Headers.TryGetValue("Authorization", out var authHeader)) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = $"Unauthorized request - no authorization header ({number})", Type = LogEntryType.warning, @@ -131,10 +113,10 @@ namespace WebAPI.Controllers return Unauthorized(); } - string[] credentialsArr = authHeader.ToString().Split(" "); + var credentialsArr = authHeader.ToString().Split(" "); if (credentialsArr.Length != 2) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = $"Unauthorized request - wrong auth header format ({number})", Type = LogEntryType.warning, @@ -147,9 +129,9 @@ namespace WebAPI.Controllers var authValue = Encoding.UTF8.GetString(Convert.FromBase64String(credentialsArr[1])); var username = authValue.Split(':')[0]; var password = authValue.Split(':')[1]; - if (username != configuration["powerBI-user"] || password != configuration["powerBI-pass"]) + if (username != _configuration["powerBI-user"] || password != _configuration["powerBI-pass"]) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = $"Unauthorized request - bad credentials ({number})", Type = LogEntryType.warning, @@ -159,7 +141,7 @@ namespace WebAPI.Controllers return Unauthorized(); } - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = $"Sending data for layer {number}", Type = LogEntryType.info, @@ -167,14 +149,13 @@ namespace WebAPI.Controllers CreatedAt = DateTime.UtcNow, }); - return Ok(db.Layers + return Ok(_db.Layers .Include(x => x.CreatedBy) - .Include(x => x.Records) - .Where(x => x.Number == number && !x.IsDeleted).First()); + .Include(x => x.Records).First(x => x.Number == number && !x.IsDeleted)); } catch (Exception e) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = e.ToString(), Type = LogEntryType.error, @@ -189,16 +170,15 @@ namespace WebAPI.Controllers [Route("exportToGoogleSheet/{id}")] public IActionResult ExportToGoogleSheet(Guid id) { - if (googleSheetValues is null) + if (_googleSheetValues is null) { throw new Exception("Google Sheets API not initialized"); } - Layer layer = db.Layers - .Include(x => x.Records!.OrderByDescending(x => x.Code)) - .Where(x => x.Id == id && !x.IsDeleted).First(); + var layer = _db.Layers + .Include(x => x.Records!.OrderByDescending(y => y.Code)).First(x => x.Id == id && !x.IsDeleted); - var export = new googleSheetExport(googleDriveHelper, googleSheetValues, configuration); + var export = new googleSheetExport(_googleDriveHelper, _googleSheetValues, _configuration); export.export(layer); return Ok(true); } @@ -208,32 +188,30 @@ namespace WebAPI.Controllers [AllowAnonymous] public IActionResult AutoImport(string apiKey) { - if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"]) + if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"]) { return Unauthorized(); } - if (googleSheetValues is null) + if (_googleSheetValues is null) { throw new Exception("Google Sheets API not initialized"); } + var importWorkerLayers = _db.Layers + .Include(x => x.Records) + .Where(x => + x.Records!.Any(y => y.Code == "Type" && y.Desc1 == "ImportWorker") && + x.Records!.Any(y => y.Code == "IsEnabled" && y.Desc1 == "True") + //&& x.Records!.Any(x => x.Code == "ImportType" && x.Desc1 == "FK2") + ) + .OrderBy(x => x.CreatedAt) + .ToList(); try { - List importWorkerLayers; - importWorkerLayers = db.Layers - .Include(x => x.Records) - .Where(x => - x.Records!.Any(x => x.Code == "Type" && x.Desc1 == "ImportWorker") && - x.Records!.Any(x => x.Code == "IsEnabled" && x.Desc1 == "True") - //&& x.Records!.Any(x => x.Code == "ImportType" && x.Desc1 == "FK2") - ) - .OrderBy(x => x.CreatedAt) - .ToList(); - - if (importWorkerLayers.Count() == 0) + if (!importWorkerLayers.Any()) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = "No Layers to import.", Type = LogEntryType.info, @@ -243,24 +221,21 @@ namespace WebAPI.Controllers return Ok(); } - foreach (Layer importWorker in importWorkerLayers) + foreach (var importWorker in importWorkerLayers) { try { - string? type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1; - if (type == null) - { - type = "Standard"; - } + var type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1 ?? + "Standard"; if (type == "FK2") { - MorskaFk2Importer importer = new MorskaFk2Importer(db, googleSheetValues, this); + var importer = new MorskaFk2Importer(_db, _googleSheetValues, this); importer.import(importWorker); - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { - Title = $"{importWorker!.Name}, {importWorker.Id}", + Title = $"{importWorker.Name}, {importWorker.Id}", Type = LogEntryType.info, LogType = LogType.import, Message = "Success", @@ -269,30 +244,30 @@ namespace WebAPI.Controllers } else { - string? startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1; + var startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1; if (startDate == null) { throw new Exception("StartDate record nod found"); } - string? endDate = importWorker.Records!.Where(x => x.Code == "EndDate").First().Desc1; + var endDate = importWorker.Records!.First(x => x.Code == "EndDate").Desc1; if (endDate == null) { throw new Exception("EndDate record nod found"); } - var startDateParsed = DateTime.ParseExact(startDate!, "yyyy.MM.dd", null); - var endDateParsed = DateTime.ParseExact(endDate!, "yyyy.MM.dd", null); + 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) { - MorskaImporter importer = new MorskaImporter(db, googleSheetValues, this); + var importer = new MorskaImporter(_db, _googleSheetValues, this); importer.import(importWorker); Thread.Sleep(5000); // be aware of GSheet API quota - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { - Title = $"{importWorker!.Name}, {importWorker.Id}", + Title = $"{importWorker.Name}, {importWorker.Id}", Type = LogEntryType.info, LogType = LogType.import, Message = "Success", @@ -301,13 +276,13 @@ namespace WebAPI.Controllers } else if (IsImportedLayerUpToDate(importWorker) == false) { - MorskaImporter importer = new MorskaImporter(db, googleSheetValues, this); + MorskaImporter importer = new MorskaImporter(_db, _googleSheetValues, this); importer.import(importWorker); Thread.Sleep(5000); // be aware of GSheet API quota - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { - Title = $"{importWorker!.Name}, {importWorker.Id}", + Title = $"{importWorker.Name}, {importWorker.Id}", Type = LogEntryType.warning, LogType = LogType.import, Message = "Success (reimported)", @@ -316,9 +291,9 @@ namespace WebAPI.Controllers } else { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { - Title = $"{importWorker!.Name}, {importWorker.Id}", + Title = $"{importWorker.Name}, {importWorker.Id}", Type = LogEntryType.warning, LogType = LogType.import, Message = "importLayer records are up of date. Not processed.", @@ -329,9 +304,9 @@ namespace WebAPI.Controllers } catch (Exception e) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { - Title = $"{importWorker!.Name}, {importWorker.Id}", + Title = $"{importWorker.Name}, {importWorker.Id}", Type = LogEntryType.error, LogType = LogType.import, Message = e.ToString(), @@ -344,7 +319,7 @@ namespace WebAPI.Controllers } catch (Exception e) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = "Process error", Type = LogEntryType.error, @@ -361,17 +336,17 @@ namespace WebAPI.Controllers [AllowAnonymous] public IActionResult AutoProcess(string apiKey) { - if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"]) + if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"]) { return Unauthorized(); } - if (googleSheetValues is null) + if (_googleSheetValues is null) { throw new Exception("Google Sheets API not initialized"); } - string[] processTypes = new string[] + string[] processTypes = { "T3-SingleSource", "T3-SourceYearSummary", @@ -386,7 +361,7 @@ namespace WebAPI.Controllers { try { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = $"Processing: {type}", Type = LogEntryType.info, @@ -394,19 +369,19 @@ namespace WebAPI.Controllers CreatedAt = DateTime.UtcNow, }); - List processWorkerLayers = db.Layers + List processWorkerLayers = _db.Layers .Include(x => x.Records) .Where(x => - x.Records!.Any(x => x.Code == "Type" && x.Desc1 == "ProcessWorker") && - x.Records!.Any(x => x.Code == "IsEnabled" && x.Desc1 == "True") && - x.Records!.Any(x => x.Code == "ProcessType" && x.Desc1 == type) + x.Records!.Any(y => y.Code == "Type" && y.Desc1 == "ProcessWorker") && + x.Records!.Any(y => y.Code == "IsEnabled" && y.Desc1 == "True") && + x.Records!.Any(y => y.Code == "ProcessType" && y.Desc1 == type) ) .OrderBy(x => x.CreatedAt) .ToList(); - if (processWorkerLayers.Count() == 0) + if (!processWorkerLayers.Any()) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = "No Layers to process.", Type = LogEntryType.info, @@ -423,9 +398,9 @@ namespace WebAPI.Controllers } catch (Exception e) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { - Title = $"{processWorker!.Name}, {processWorker.Id}", + Title = $"{processWorker.Name}, {processWorker.Id}", Type = LogEntryType.error, LogType = LogType.process, Message = e.ToString(), @@ -436,7 +411,7 @@ namespace WebAPI.Controllers } catch (Exception e) { - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { Title = "Process error", Type = LogEntryType.error, @@ -450,109 +425,105 @@ namespace WebAPI.Controllers return Ok(); } - internal void ProcessLayer(Layer processWorker) + private void ProcessLayer(Layer processWorker) { - if (googleSheetValues == null) + if (_googleSheetValues == null) { throw new Exception("Google Sheets API not initialized"); } - string? name = processWorker.Name; - string? year = processWorker?.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1; + var year = processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1; if (year == null) { throw new Exception("Year record nod found"); } - string? processType = processWorker?.Records?.SingleOrDefault(x => x.Code == "ProcessType")?.Desc1; - if (processType == null) + var processType = processWorker.Records?.SingleOrDefault(x => x.Code == "ProcessType")?.Desc1; + switch (processType) { - throw new Exception("ProcessType record not found"); - } - - if (processType == "T3-SourceYearSummary") - { - T3SourceYearSummaryProcessor processor = new T3SourceYearSummaryProcessor(db, googleSheetValues, this); - processor.process(processWorker!); - - logsController.AddEntry(new LogEntry + case null: + throw new Exception("ProcessType record not found"); + case "T3-SourceYearSummary": { - Title = $"{processWorker!.Name}, {processWorker.Id}", - Type = LogEntryType.info, - LogType = LogType.process, - Message = "Success", - CreatedAt = DateTime.UtcNow - }); - return; - } + T3SourceYearSummaryProcessor processor = + new T3SourceYearSummaryProcessor(_db, _googleSheetValues, this); + processor.process(processWorker); - if (processType == "T3-MultiSourceYearSummary") - { - T3MultiSourceYearSummaryProcessor processor = - new T3MultiSourceYearSummaryProcessor(db, googleSheetValues, this, logsController); - processor.process(processWorker!); - - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry + { + Title = $"{processWorker.Name}, {processWorker.Id}", + Type = LogEntryType.info, + LogType = LogType.process, + Message = "Success", + CreatedAt = DateTime.UtcNow + }); + return; + } + case "T3-MultiSourceYearSummary": { - Title = $"{processWorker!.Name}, {processWorker.Id}", - Type = LogEntryType.info, - LogType = LogType.process, - Message = "Success", - CreatedAt = DateTime.UtcNow - }); - return; - } + T3MultiSourceYearSummaryProcessor processor = + new T3MultiSourceYearSummaryProcessor(_db, _googleSheetValues, this, _logsController); + processor.process(processWorker); - if (processType == "T3-MultiSourceCopySelectedCodesYearSummary") - { - T3MultiSourceCopySelectedCodesYearSummaryProcessor processor = - new T3MultiSourceCopySelectedCodesYearSummaryProcessor(db, googleSheetValues, this); - processor.process(processWorker!); - - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry + { + Title = $"{processWorker.Name}, {processWorker.Id}", + Type = LogEntryType.info, + LogType = LogType.process, + Message = "Success", + CreatedAt = DateTime.UtcNow + }); + return; + } + case "T3-MultiSourceCopySelectedCodesYearSummary": { - Title = $"{processWorker!.Name}, {processWorker.Id}", - Type = LogEntryType.info, - LogType = LogType.process, - Message = "Success", - CreatedAt = DateTime.UtcNow - }); - return; - } + T3MultiSourceCopySelectedCodesYearSummaryProcessor processor = + new T3MultiSourceCopySelectedCodesYearSummaryProcessor(_db, _googleSheetValues, this); + processor.process(processWorker); - if (processType == "T1-R1") - { - T1R1Processor processor = new T1R1Processor(db, googleSheetValues, this, logsController); - processor.process(processWorker!); - - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry + { + Title = $"{processWorker.Name}, {processWorker.Id}", + Type = LogEntryType.info, + LogType = LogType.process, + Message = "Success", + CreatedAt = DateTime.UtcNow + }); + return; + } + case "T1-R1": { - Title = $"{processWorker!.Name}, {processWorker.Id}", - Type = LogEntryType.info, - LogType = LogType.process, - Message = "Success", - CreatedAt = DateTime.UtcNow - }); - return; - } - - if (processType == "T4-R2") - { - T4R2Processor processor = new T4R2Processor(db, googleSheetValues, this, logsController); - processor.process(processWorker!); - - logsController.AddEntry(new LogEntry + T1R1Processor processor = new T1R1Processor(_db, _googleSheetValues, this, _logsController); + processor.process(processWorker); + + _logsController.AddEntry(new LogEntry + { + Title = $"{processWorker.Name}, {processWorker.Id}", + Type = LogEntryType.info, + LogType = LogType.process, + Message = "Success", + CreatedAt = DateTime.UtcNow + }); + return; + } + case "T4-R2": { - Title = $"{processWorker!.Name}, {processWorker.Id}", - Type = LogEntryType.info, - LogType = LogType.process, - Message = "Success", - CreatedAt = DateTime.UtcNow - }); - return; + var processor = new T4R2Processor(_db, _googleSheetValues, this, _logsController); + processor.process(processWorker); + + _logsController.AddEntry(new LogEntry + { + Title = $"{processWorker.Name}, {processWorker.Id}", + Type = LogEntryType.info, + LogType = LogType.process, + Message = "Success", + CreatedAt = DateTime.UtcNow + }); + return; + } } - string? month = processWorker?.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1; + var month = processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1; if (month == null) { throw new Exception("Month record not found"); @@ -562,35 +533,35 @@ namespace WebAPI.Controllers { case "T3-SingleSource": { - T3SingleSourceProcessor processor = new T3SingleSourceProcessor(db, googleSheetValues, this); - processor.process(processWorker!); + T3SingleSourceProcessor processor = new T3SingleSourceProcessor(_db, _googleSheetValues, this); + processor.process(processWorker); break; } case "T4-SingleSource": { - T4SingleSourceProcessor processor = new T4SingleSourceProcessor(db, googleSheetValues, this); - processor.process(processWorker!); + T4SingleSourceProcessor processor = new T4SingleSourceProcessor(_db, _googleSheetValues, this); + processor.process(processWorker); break; } case "T3-MultiSourceSummary": { T3MultiSourceSummaryProcessor processor = - new T3MultiSourceSummaryProcessor(db, googleSheetValues, this, logsController); - processor.process(processWorker!); + new T3MultiSourceSummaryProcessor(_db, _googleSheetValues, this, _logsController); + processor.process(processWorker); break; } case "T3-MultiSourceCopySelectedCodes": { T3MultiSourceCopySelectedCodesProcessor processor = - new T3MultiSourceCopySelectedCodesProcessor(db, googleSheetValues, this); - processor.process(processWorker!); + new T3MultiSourceCopySelectedCodesProcessor(_db, _googleSheetValues, this); + processor.process(processWorker); break; } } - logsController.AddEntry(new LogEntry + _logsController.AddEntry(new LogEntry { - Title = $"{processWorker!.Name}, {processWorker.Id}", + Title = $"{processWorker.Name}, {processWorker.Id}", Type = LogEntryType.info, LogType = LogType.process, Message = "Success", @@ -600,33 +571,26 @@ namespace WebAPI.Controllers internal void SaveRecords(Guid id, ICollection records, Guid currentUserId) { - try + List toDelete = _db.Records.Where(x => x.LayerId == id).ToList(); + if (toDelete.Count > 0) { - List toDelete = db.Records.Where(x => x.LayerId == id).ToList(); - if (toDelete.Count > 0) - { - db.Records.RemoveRange(toDelete); - } - - foreach (Record record in records) - { - record.CreatedById = currentUserId; - record.CreatedAt = DateTime.UtcNow; - record.ModifiedById = currentUserId; - record.ModifiedAt = DateTime.UtcNow; - record.LayerId = id; - db.Records.Add(record); - } + _db.Records.RemoveRange(toDelete); } - catch (Exception) + + foreach (Record record in records) { - throw; + record.CreatedById = currentUserId; + record.CreatedAt = DateTime.UtcNow; + record.ModifiedById = currentUserId; + record.ModifiedAt = DateTime.UtcNow; + record.LayerId = id; + _db.Records.Add(record); } } - internal void WriteToConsole(params string[] messages) + private void WriteToConsole(params string[] messages) { - foreach (string message in messages) + foreach (var message in messages) { Console.WriteLine($"DiunaLog: {message}"); } @@ -634,12 +598,12 @@ namespace WebAPI.Controllers private bool IsImportedLayerUpToDate(Layer importWorker) { - if (googleSheetValues is null) + if (_googleSheetValues is null) { throw new Exception("Google Sheets API not initialized"); } - Layer? newestLayer = db.Layers + var newestLayer = _db.Layers .Include(x => x.Records) .Where(x => x.ParentId == importWorker.Id) .OrderByDescending(x => x.CreatedAt) @@ -650,53 +614,48 @@ namespace WebAPI.Controllers return true; // importWorker is not active yet, no check needed } - string? sheetId = importWorker.Records!.Where(x => x.Code == "SheetId").FirstOrDefault()?.Desc1; + var sheetId = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetId")?.Desc1; if (sheetId == null) { throw new Exception($"SheetId not found, {importWorker.Name}"); } - string? sheetTabName = importWorker.Records!.Where(x => x.Code == "SheetTabName").FirstOrDefault()?.Desc1; + var sheetTabName = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetTabName")?.Desc1; if (sheetTabName == null) { throw new Exception($"SheetTabName not found, {importWorker.Name}"); } - string? dataRange = importWorker.Records!.Where(x => x.Code == "DataRange").FirstOrDefault()?.Desc1; + var dataRange = importWorker.Records!.FirstOrDefault(x => x.Code == "DataRange")?.Desc1; if (dataRange == null) { throw new Exception($"DataRange not found, {importWorker.Name}"); } - var dataRangeResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute(); + var dataRangeResponse = _googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute(); var data = dataRangeResponse.Values; - bool isUpToDate = true; + var isUpToDate = true; - for (int i = 0; i < data[1].Count; i++) + for (var i = 0; i < data[1].Count; i++) { - double value; - if (data[0][i].ToString() != "") + if (data[0][i].ToString() == "") continue; + var record = newestLayer.Records!.FirstOrDefault(x => x.Code == data[0][i].ToString()); + if (record == null) { - Record? record = newestLayer.Records!.Where(x => x.Code == data[0][i].ToString()).FirstOrDefault(); - if (record == null) - { - WriteToConsole("Code not found in DiunaBI", data[0][i].ToString()!); - isUpToDate = false; - continue; - } - else if ( - double.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out value) && - record.Value1 != value) - { - WriteToConsole( - $"Code: {data[0][i]}. DiunaBI: {string.Format("{0:N2}", record.Value1)}. GoogleSheet: {data[1][i]}"); - isUpToDate = false; - } + WriteToConsole("Code not found in DiunaBI", data[0][i].ToString()!); + isUpToDate = false; + continue; } - } - foreach (Record record in newestLayer.Records!) + if ((!double.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"), + out var value) || + double.Abs((double)(record.Value1-value)!) > 0.01)) continue; + WriteToConsole( + $"Code: {data[0][i]}. DiunaBI: {record.Value1:N2}. GoogleSheet: {data[1][i]}"); + isUpToDate = false; + } + foreach (var record in newestLayer.Records!) { if (data[0].Contains(record.Code)) { @@ -706,7 +665,6 @@ namespace WebAPI.Controllers WriteToConsole($"Code not found in GoogleSheet: {record.Code}"); isUpToDate = false; } - return isUpToDate; } } diff --git a/WebAPI/Controllers/PingController.cs b/WebAPI/Controllers/PingController.cs index a63204b..2d8ff70 100644 --- a/WebAPI/Controllers/PingController.cs +++ b/WebAPI/Controllers/PingController.cs @@ -1,15 +1,5 @@ -using Google.Apis.Auth; -using Google.Apis.Http; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; -using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos; -using Microsoft.IdentityModel.Tokens; -using System.Configuration; -using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; -using System.Text; -using WebAPI.Models; namespace WebAPI.Controllers { @@ -18,11 +8,11 @@ namespace WebAPI.Controllers [Authorize] public class PingController : Controller { - private readonly IConfiguration configuration; + private readonly IConfiguration _configuration; public PingController( - IConfiguration _configuration) + IConfiguration configuration) { - configuration = _configuration; + _configuration = configuration; } [HttpGet] @@ -30,7 +20,7 @@ namespace WebAPI.Controllers [AllowAnonymous] public IActionResult Ping() { - return Ok(configuration["PONG"]); + return Ok(_configuration["PONG"]); } } } \ No newline at end of file diff --git a/WebAPI/GoogleDriveHelper.cs b/WebAPI/GoogleDriveHelper.cs index 8011eaa..580338e 100644 --- a/WebAPI/GoogleDriveHelper.cs +++ b/WebAPI/GoogleDriveHelper.cs @@ -2,38 +2,35 @@ using Google.Apis.Drive.v3; using Google.Apis.Services; -namespace WebAPI +namespace WebAPI; + +public class GoogleDriveHelper { - public class GoogleDriveHelper + public DriveService? Service { get; set; } + private const string ApplicationName = "Diuna"; + private static readonly string[] Scopes = { DriveService.Scope.Drive }; + public GoogleDriveHelper() { - public DriveService? Service { get; set; } - const string APPLICATION_NAME = "Diuna"; - static readonly string[] Scopes = { DriveService.Scope.Drive }; - public GoogleDriveHelper() - { - InitializeService(); - } - private void InitializeService() - { - var credential = GetCredentialsFromFile(); - Service = new DriveService(new BaseClientService.Initializer() - { - HttpClientInitializer = credential, - ApplicationName = APPLICATION_NAME - }); - } - private GoogleCredential GetCredentialsFromFile() - { - string fileName = "client_secrets.json"; -#if DEBUG - fileName = "client_secrets.Development.json"; -#endif - GoogleCredential credential; - using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) - { - credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes); - } - return credential; - } + InitializeService(); } -} + private void InitializeService() + { + var credential = GetCredentialsFromFile(); + Service = new DriveService(new BaseClientService.Initializer + { + HttpClientInitializer = credential, + ApplicationName = ApplicationName + }); + } + private static GoogleCredential GetCredentialsFromFile() + { + // ReSharper disable once RedundantAssignment + var fileName = "client_secrets.json"; +#if DEBUG + fileName = "client_secrets.Development.json"; +#endif + using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); + var credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes); + return credential; + } +} \ No newline at end of file diff --git a/WebAPI/GoogleSheetsHelper.cs b/WebAPI/GoogleSheetsHelper.cs index 37c7789..3d424ce 100644 --- a/WebAPI/GoogleSheetsHelper.cs +++ b/WebAPI/GoogleSheetsHelper.cs @@ -2,38 +2,35 @@ using Google.Apis.Services; using Google.Apis.Sheets.v4; -namespace WebAPI +namespace WebAPI; + +public class GoogleSheetsHelper { - public class GoogleSheetsHelper + public SheetsService? Service { get; private set; } + private const string ApplicationName = "Diuna"; + private static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets }; + public GoogleSheetsHelper() { - public SheetsService? Service { get; set; } - const string APPLICATION_NAME = "Diuna"; - static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets }; - public GoogleSheetsHelper() - { - InitializeService(); - } - private void InitializeService() - { - var credential = GetCredentialsFromFile(); - Service = new SheetsService(new BaseClientService.Initializer() - { - HttpClientInitializer = credential, - ApplicationName = APPLICATION_NAME - }); - } - private GoogleCredential GetCredentialsFromFile() - { - string fileName = "client_secrets.json"; -#if DEBUG - fileName = "client_secrets.Development.json"; -#endif - GoogleCredential credential; - using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) - { - credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes); - } - return credential; - } + InitializeService(); } -} + private void InitializeService() + { + var credential = GetCredentialsFromFile(); + Service = new SheetsService(new BaseClientService.Initializer + { + HttpClientInitializer = credential, + ApplicationName = ApplicationName + }); + } + private static GoogleCredential GetCredentialsFromFile() + { + // ReSharper disable once RedundantAssignment + var fileName = "client_secrets.json"; +#if DEBUG + fileName = "client_secrets.Development.json"; +#endif + using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); + var credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes); + return credential; + } +} \ No newline at end of file diff --git a/WebAPI/Program.cs b/WebAPI/Program.cs index e573f06..37cc80d 100644 --- a/WebAPI/Program.cs +++ b/WebAPI/Program.cs @@ -1,7 +1,6 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; -using Newtonsoft.Json.Linq; using System.IdentityModel.Tokens.Jwt; using System.Text; using WebAPI; @@ -16,14 +15,14 @@ builder.Services.AddDbContext(x => { builder.Services.AddCors(options => { - options.AddPolicy("CORSPolicy", builder => + options.AddPolicy("CORSPolicy", corsPolicyBuilder => { - builder.WithOrigins("http://localhost:4200") + corsPolicyBuilder.WithOrigins("http://localhost:4200") .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); - builder.WithOrigins("https://diuna.bim-it.pl") + corsPolicyBuilder.WithOrigins("https://diuna.bim-it.pl") .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); @@ -60,7 +59,7 @@ var app = builder.Build(); app.Use(async (context, next) => { - string token = context.Request.Headers["Authorization"].ToString(); + var token = context.Request.Headers["Authorization"].ToString(); if (token.Length > 0 && !context.Request.Path.ToString().Contains("getForPowerBI")) { var handler = new JwtSecurityTokenHandler(); var data = handler.ReadJwtToken(token.Split(' ')[1]); diff --git a/WebAPI/dataProcessors/t3.MultiSourceSummary.processor.cs b/WebAPI/dataProcessors/t3.MultiSourceSummary.processor.cs index 26291f3..067c248 100644 --- a/WebAPI/dataProcessors/t3.MultiSourceSummary.processor.cs +++ b/WebAPI/dataProcessors/t3.MultiSourceSummary.processor.cs @@ -140,7 +140,7 @@ namespace WebAPI.dataProcessors Title = $"{processWorker!.Name}, {processWorker.Id}", Type = LogEntryType.warning, LogType = LogType.process, - Message = $"Formula {calc.Expresion} in Record {dynamicCode.Id} is not correct", + Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} is not correct", CreatedAt = DateTime.UtcNow }); continue; @@ -157,7 +157,7 @@ namespace WebAPI.dataProcessors Title = $"{processWorker!.Name}, {processWorker.Id}", Type = LogEntryType.warning, LogType = LogType.process, - Message = $"Formula {calc.Expresion} in Record {dynamicCode.Id} error: {e.Message}", + Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} error: {e.Message}", CreatedAt = DateTime.UtcNow }); } diff --git a/WebAPI/dataProcessors/t3.MultiSourceYearSummary.processor.cs b/WebAPI/dataProcessors/t3.MultiSourceYearSummary.processor.cs index fb3c8ae..09734d2 100644 --- a/WebAPI/dataProcessors/t3.MultiSourceYearSummary.processor.cs +++ b/WebAPI/dataProcessors/t3.MultiSourceYearSummary.processor.cs @@ -176,7 +176,7 @@ namespace WebAPI.dataProcessors Title = $"{processWorker!.Name}, {processWorker.Id}", Type = LogEntryType.warning, LogType = LogType.process, - Message = $"Formula {calc.Expresion} in Record {dynamicCode.Id} is not correct", + Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} is not correct", CreatedAt = DateTime.UtcNow }); continue; @@ -193,7 +193,7 @@ namespace WebAPI.dataProcessors Title = $"{processWorker!.Name}, {processWorker.Id}", Type = LogEntryType.warning, LogType = LogType.process, - Message = $"Formula {calc.Expresion} in Record {dynamicCode.Id} error: {e.Message}", + Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} error: {e.Message}", CreatedAt = DateTime.UtcNow }); }