WIP: Resolve all code issues

This commit is contained in:
Michał Zieliski
2024-06-18 18:39:02 +02:00
parent f93bb9cd42
commit c2a98e0386
10 changed files with 363 additions and 441 deletions

View File

@@ -6,10 +6,10 @@ namespace WebAPI
{ {
public class AppDbContext : DbContext public class AppDbContext : DbContext
{ {
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; init; }
public DbSet<Layer> Layers { get; set; } public DbSet<Layer> Layers { get; init; }
public DbSet<Record> Records { get; set; } public DbSet<Record> Records { get; init; }
public DbSet<ProcessSource> ProcessSources { get; set; } public DbSet<ProcessSource> ProcessSources { get; init; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{ {
@@ -24,14 +24,14 @@ namespace WebAPI
}); });
} }
public static readonly Microsoft.Extensions.Logging.LoggerFactory _myLoggerFactory = private static readonly LoggerFactory MyLoggerFactory =
new LoggerFactory(new[] { new(new[] {
new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider() new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider()
}); });
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
optionsBuilder.UseLoggerFactory(_myLoggerFactory); optionsBuilder.UseLoggerFactory(MyLoggerFactory);
} }
} }
} }

View File

@@ -2,22 +2,20 @@ using System.Globalization;
using DiunaBIWebAPI.dataProcessors; using DiunaBIWebAPI.dataProcessors;
using WebAPI.Models; using WebAPI.Models;
using AngouriMath; using AngouriMath;
using AngouriMath.Extensions;
namespace WebAPI.Calculator namespace WebAPI.Calculator;
public class BaseCalc
{ {
public class BaseCalc public string Expression { get; }
{
public string Expresion { get; set; }
private string ResultCode { get; set; } private string ResultCode { get; set; }
private string Formula { get; set; } private string Formula { get; }
public BaseCalc(string expresion) public BaseCalc(string expression)
{ {
Expresion = expresion; Expression = expression;
Formula = Expresion.Split("=")[1]; Formula = Expression.Split("=")[1];
ResultCode = Expresion.Split("=")[0]; ResultCode = Expression.Split("=")[0];
} }
public bool IsFormulaCorrect() public bool IsFormulaCorrect()
@@ -35,29 +33,26 @@ namespace WebAPI.Calculator
ResultCode = ResultCode.Substring(1, ResultCode.Length - 2); ResultCode = ResultCode.Substring(1, ResultCode.Length - 2);
// check right side of expression // check right side of expression
if (!(!string.IsNullOrEmpty(Formula) && return !string.IsNullOrEmpty(Formula) &&
Formula.All(c => char.IsDigit(c) || c == '[' || c == ']' || c == '+'))) Formula.All(c => char.IsDigit(c) || c == '[' || c == ']' || c == '+');
{
return false;
}
return true;
} }
public Record CalculateT3(List<Record> records) public Record CalculateT3(List<Record> records)
{ {
Record result = new Record var resultCode = ResultCode;
{
var result = new Record
{ {
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
Code = this.ResultCode, Code = resultCode,
CreatedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow, ModifiedAt = DateTime.UtcNow
}; };
List<string> codes = GetCodes(); var codes = GetCodes();
List<Record> ingredients = new List<Record>(); var ingredients = new List<Record>();
foreach (string code in codes) foreach (var code in codes)
{ {
Record? ingredient = records.FirstOrDefault(r => r.Code == code); var ingredient = records.FirstOrDefault(r => r.Code == code);
if (ingredient == null) if (ingredient == null)
{ {
throw new Exception($"Record for code {code} not found."); throw new Exception($"Record for code {code} not found.");
@@ -65,33 +60,29 @@ namespace WebAPI.Calculator
ingredients.Add(ingredient); ingredients.Add(ingredient);
} }
for (int i = 1; i <= 32; i++) for (var i = 1; i <= 32; i++)
{ {
string formula = Formula; var formula = ingredients.Aggregate(Formula, (current, ingredient) => current.Replace($"[{ingredient.Code}]", ProcessHelper.getValue(ingredient, i)?.ToString(CultureInfo.InvariantCulture)));
foreach (Record ingredient in ingredients) if (formula.Contains('['))
{
formula = formula.Replace($"[{ingredient.Code}]", ProcessHelper.getValue(ingredient, i)?.ToString(CultureInfo.InvariantCulture));
}
if (formula.Contains("["))
{ {
throw new Exception($"Not all placeholders were replaced. Value{i} [{formula}]"); throw new Exception($"Not all placeholders were replaced. Value{i} [{formula}]");
} }
Entity expr = formula; Entity expr = formula;
double val = (double)expr.EvalNumerical();
ProcessHelper.setValue(result, i, (double)expr.EvalNumerical()); ProcessHelper.setValue(result, i, (double)expr.EvalNumerical());
} }
return result; return result;
} }
}
private List<string> GetCodes() private List<string> GetCodes()
{ {
List<string> codes = new List<string>(); var codes = new List<string>();
int endIndex = -1; var endIndex = -1;
while (true) while (true)
{ {
int startIndex = Formula.IndexOf("[", endIndex + 1, StringComparison.CurrentCulture); var startIndex = Formula.IndexOf("[", endIndex + 1, StringComparison.CurrentCulture);
endIndex = Formula.IndexOf("]", startIndex + 1, StringComparison.CurrentCulture); endIndex = Formula.IndexOf("]", startIndex + 1, StringComparison.CurrentCulture);
if (startIndex == -1 || endIndex == -1) if (startIndex == -1 || endIndex == -1)
@@ -99,10 +90,9 @@ namespace WebAPI.Calculator
break; break;
} }
string valueCode = Formula.Substring(startIndex + 1, endIndex - startIndex - 1); var valueCode = Formula.Substring(startIndex + 1, endIndex - startIndex - 1);
codes.Add(valueCode); codes.Add(valueCode);
} }
return codes; return codes;
} }
}
} }

View File

@@ -1,13 +1,7 @@
using System.Data; using System.Data;
using System.Globalization;
using System.Xml.Serialization;
using Google.Apis.Sheets.v4;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using WebAPI.dataParsers;
using WebAPI.Exports;
using WebAPI.Models; using WebAPI.Models;
using static Google.Apis.Drive.v3.FilesResource; using static Google.Apis.Drive.v3.FilesResource;
@@ -15,26 +9,20 @@ namespace WebAPI.Controllers
{ {
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class AdminController : Controller public class AdminController : Controller
{ {
private readonly AppDbContext db; private readonly GoogleDriveHelper _googleDriveHelper;
private GoogleDriveHelper googleDriveHelper; private readonly IConfiguration _configuration;
private GoogleSheetsHelper googleSheetsHelper; private readonly LogsController _logsController;
private readonly IConfiguration configuration;
private readonly LogsController logsController;
public AdminController( public AdminController(
AppDbContext _db, GoogleDriveHelper googleDriveHelper,
GoogleSheetsHelper _googleSheetsHelper, IConfiguration configuration,
GoogleDriveHelper _googleDriveHelper, LogsController logsController)
IConfiguration _configuration)
{ {
db = _db; _googleDriveHelper = googleDriveHelper;
googleSheetsHelper = _googleSheetsHelper; _configuration = configuration;
googleDriveHelper = _googleDriveHelper; _logsController = logsController;
configuration = _configuration;
logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
} }
[HttpGet] [HttpGet]
@@ -42,18 +30,19 @@ namespace WebAPI.Controllers
[AllowAnonymous] [AllowAnonymous]
public IActionResult BackupDatabase(string apiKey) 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(); return Unauthorized();
} }
try try
{ {
string databaseName = "diunabi-morska"; var databaseName = "diunabi-morska";
string localDatabasePath = $"{configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak"; var localDatabasePath = $"{_configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak";
var formatMediaName = $"DatabaseToolkitBackup_{databaseName}"; var formatMediaName = $"DatabaseToolkitBackup_{databaseName}";
var formatName = $"Full Backup of {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 var sql = @"BACKUP DATABASE @databaseName
TO DISK = @localDatabasePath TO DISK = @localDatabasePath
@@ -73,24 +62,26 @@ namespace WebAPI.Controllers
command.ExecuteNonQuery(); command.ExecuteNonQuery();
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File(); Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File
body.Name = Path.GetFileName(localDatabasePath); {
body.Parents = new List<string?> { configuration["GDriveBackupDirectory"] }; Name = Path.GetFileName(localDatabasePath),
body.MimeType = "application/octet-stream"; Parents = new List<string?> { _configuration["GDriveBackupDirectory"] },
MimeType = "application/octet-stream"
};
var fsSource = new FileStream(localDatabasePath, FileMode.Open, FileAccess.Read); 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"); 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"; request.Fields = "id";
var response = request.Upload(); request.Upload();
_logsController.AddEntry(new LogEntry
logsController.AddEntry(new LogEntry
{ {
Title = "Backup success", Title = "Backup success",
Type = LogEntryType.info, Type = LogEntryType.info,
@@ -101,7 +92,7 @@ namespace WebAPI.Controllers
} }
catch (Exception e) catch (Exception e)
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = "Backup error", Title = "Backup error",
Type = LogEntryType.error, Type = LogEntryType.error,

View File

@@ -15,29 +15,29 @@ namespace WebAPI.Controllers
[Route("api/[controller]")] [Route("api/[controller]")]
public class LayersController : Controller public class LayersController : Controller
{ {
private readonly AppDbContext db; private readonly AppDbContext _db;
private SpreadsheetsResource.ValuesResource? googleSheetValues; private readonly SpreadsheetsResource.ValuesResource? _googleSheetValues;
private GoogleDriveHelper googleDriveHelper; private readonly GoogleDriveHelper _googleDriveHelper;
private GoogleSheetsHelper googleSheetsHelper; // private GoogleSheetsHelper _googleSheetsHelper;
private readonly IConfiguration configuration; private readonly IConfiguration _configuration;
private readonly LogsController logsController; private readonly LogsController _logsController;
public LayersController( public LayersController(
AppDbContext _db, AppDbContext db,
GoogleSheetsHelper _googleSheetsHelper, GoogleSheetsHelper googleSheetsHelper,
GoogleDriveHelper _googleDriveHelper, GoogleDriveHelper googleDriveHelper,
IConfiguration _configuration) IConfiguration configuration)
{ {
db = _db; _db = db;
if (_googleSheetsHelper.Service is not null) if (googleSheetsHelper.Service is not null)
{ {
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values; _googleSheetValues = googleSheetsHelper.Service.Spreadsheets.Values;
} }
googleSheetsHelper = _googleSheetsHelper; //_googleSheetsHelper = googleSheetsHelper;
googleDriveHelper = _googleDriveHelper; _googleDriveHelper = googleDriveHelper;
configuration = _configuration; _configuration = configuration;
logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration); _logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
} }
[HttpGet] [HttpGet]
@@ -45,7 +45,7 @@ namespace WebAPI.Controllers
{ {
try try
{ {
IQueryable<Layer> response = db.Layers.Where(x => !x.IsDeleted); var response = _db.Layers.Where(x => !x.IsDeleted);
if (name != null) if (name != null)
{ {
response = response.Where(x => x.Name != null && x.Name.Contains(name)); 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] [HttpGet]
[Route("{id}")] [Route("{id}")]
public IActionResult Get(Guid id) public IActionResult Get(Guid id)
{ {
try try
{ {
return Ok(db.Layers return Ok(_db.Layers
.Include(x => x.CreatedBy) .Include(x => x.CreatedBy)
.Include(x => x.Records) .Include(x => x.Records).First(x => x.Id == id && !x.IsDeleted));
.Where(x => x.Id == id && !x.IsDeleted).First());
} }
catch (Exception e) catch (Exception e)
{ {
@@ -104,9 +86,9 @@ namespace WebAPI.Controllers
[Route("getForPowerBI/{apiKey}/{number}")] [Route("getForPowerBI/{apiKey}/{number}")]
public IActionResult GetByNumber(string apiKey, int 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})", Title = $"Unauthorized request - wrong apiKey ({number})",
Type = LogEntryType.warning, Type = LogEntryType.warning,
@@ -121,7 +103,7 @@ namespace WebAPI.Controllers
if ( if (
!Request.Headers.TryGetValue("Authorization", out var authHeader)) !Request.Headers.TryGetValue("Authorization", out var authHeader))
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"Unauthorized request - no authorization header ({number})", Title = $"Unauthorized request - no authorization header ({number})",
Type = LogEntryType.warning, Type = LogEntryType.warning,
@@ -131,10 +113,10 @@ namespace WebAPI.Controllers
return Unauthorized(); return Unauthorized();
} }
string[] credentialsArr = authHeader.ToString().Split(" "); var credentialsArr = authHeader.ToString().Split(" ");
if (credentialsArr.Length != 2) if (credentialsArr.Length != 2)
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"Unauthorized request - wrong auth header format ({number})", Title = $"Unauthorized request - wrong auth header format ({number})",
Type = LogEntryType.warning, Type = LogEntryType.warning,
@@ -147,9 +129,9 @@ namespace WebAPI.Controllers
var authValue = Encoding.UTF8.GetString(Convert.FromBase64String(credentialsArr[1])); var authValue = Encoding.UTF8.GetString(Convert.FromBase64String(credentialsArr[1]));
var username = authValue.Split(':')[0]; var username = authValue.Split(':')[0];
var password = authValue.Split(':')[1]; 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})", Title = $"Unauthorized request - bad credentials ({number})",
Type = LogEntryType.warning, Type = LogEntryType.warning,
@@ -159,7 +141,7 @@ namespace WebAPI.Controllers
return Unauthorized(); return Unauthorized();
} }
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"Sending data for layer {number}", Title = $"Sending data for layer {number}",
Type = LogEntryType.info, Type = LogEntryType.info,
@@ -167,14 +149,13 @@ namespace WebAPI.Controllers
CreatedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow,
}); });
return Ok(db.Layers return Ok(_db.Layers
.Include(x => x.CreatedBy) .Include(x => x.CreatedBy)
.Include(x => x.Records) .Include(x => x.Records).First(x => x.Number == number && !x.IsDeleted));
.Where(x => x.Number == number && !x.IsDeleted).First());
} }
catch (Exception e) catch (Exception e)
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = e.ToString(), Title = e.ToString(),
Type = LogEntryType.error, Type = LogEntryType.error,
@@ -189,16 +170,15 @@ namespace WebAPI.Controllers
[Route("exportToGoogleSheet/{id}")] [Route("exportToGoogleSheet/{id}")]
public IActionResult ExportToGoogleSheet(Guid id) public IActionResult ExportToGoogleSheet(Guid id)
{ {
if (googleSheetValues is null) if (_googleSheetValues is null)
{ {
throw new Exception("Google Sheets API not initialized"); throw new Exception("Google Sheets API not initialized");
} }
Layer layer = db.Layers var layer = _db.Layers
.Include(x => x.Records!.OrderByDescending(x => x.Code)) .Include(x => x.Records!.OrderByDescending(y => y.Code)).First(x => x.Id == id && !x.IsDeleted);
.Where(x => x.Id == id && !x.IsDeleted).First();
var export = new googleSheetExport(googleDriveHelper, googleSheetValues, configuration); var export = new googleSheetExport(_googleDriveHelper, _googleSheetValues, _configuration);
export.export(layer); export.export(layer);
return Ok(true); return Ok(true);
} }
@@ -208,32 +188,30 @@ namespace WebAPI.Controllers
[AllowAnonymous] [AllowAnonymous]
public IActionResult AutoImport(string apiKey) 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(); return Unauthorized();
} }
if (googleSheetValues is null) if (_googleSheetValues is null)
{ {
throw new Exception("Google Sheets API not initialized"); throw new Exception("Google Sheets API not initialized");
} }
try var importWorkerLayers = _db.Layers
{
List<Layer> importWorkerLayers;
importWorkerLayers = db.Layers
.Include(x => x.Records) .Include(x => x.Records)
.Where(x => .Where(x =>
x.Records!.Any(x => x.Code == "Type" && x.Desc1 == "ImportWorker") && x.Records!.Any(y => y.Code == "Type" && y.Desc1 == "ImportWorker") &&
x.Records!.Any(x => x.Code == "IsEnabled" && x.Desc1 == "True") x.Records!.Any(y => y.Code == "IsEnabled" && y.Desc1 == "True")
//&& x.Records!.Any(x => x.Code == "ImportType" && x.Desc1 == "FK2") //&& x.Records!.Any(x => x.Code == "ImportType" && x.Desc1 == "FK2")
) )
.OrderBy(x => x.CreatedAt) .OrderBy(x => x.CreatedAt)
.ToList(); .ToList();
try
if (importWorkerLayers.Count() == 0)
{ {
logsController.AddEntry(new LogEntry if (!importWorkerLayers.Any())
{
_logsController.AddEntry(new LogEntry
{ {
Title = "No Layers to import.", Title = "No Layers to import.",
Type = LogEntryType.info, Type = LogEntryType.info,
@@ -243,24 +221,21 @@ namespace WebAPI.Controllers
return Ok(); return Ok();
} }
foreach (Layer importWorker in importWorkerLayers) foreach (var importWorker in importWorkerLayers)
{ {
try try
{ {
string? type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1; var type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1 ??
if (type == null) "Standard";
{
type = "Standard";
}
if (type == "FK2") if (type == "FK2")
{ {
MorskaFk2Importer importer = new MorskaFk2Importer(db, googleSheetValues, this); var importer = new MorskaFk2Importer(_db, _googleSheetValues, this);
importer.import(importWorker); importer.import(importWorker);
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{importWorker!.Name}, {importWorker.Id}", Title = $"{importWorker.Name}, {importWorker.Id}",
Type = LogEntryType.info, Type = LogEntryType.info,
LogType = LogType.import, LogType = LogType.import,
Message = "Success", Message = "Success",
@@ -269,30 +244,30 @@ namespace WebAPI.Controllers
} }
else else
{ {
string? startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1; var startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1;
if (startDate == null) if (startDate == null)
{ {
throw new Exception("StartDate record nod found"); 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) if (endDate == null)
{ {
throw new Exception("EndDate record nod found"); throw new Exception("EndDate record nod found");
} }
var startDateParsed = DateTime.ParseExact(startDate!, "yyyy.MM.dd", null); var startDateParsed = DateTime.ParseExact(startDate, "yyyy.MM.dd", null);
var endDateParsed = DateTime.ParseExact(endDate!, "yyyy.MM.dd", null); var endDateParsed = DateTime.ParseExact(endDate, "yyyy.MM.dd", null);
if (startDateParsed.Date <= DateTime.UtcNow.Date && if (startDateParsed.Date <= DateTime.UtcNow.Date &&
endDateParsed.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); importer.import(importWorker);
Thread.Sleep(5000); // be aware of GSheet API quota 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, Type = LogEntryType.info,
LogType = LogType.import, LogType = LogType.import,
Message = "Success", Message = "Success",
@@ -301,13 +276,13 @@ namespace WebAPI.Controllers
} }
else if (IsImportedLayerUpToDate(importWorker) == false) else if (IsImportedLayerUpToDate(importWorker) == false)
{ {
MorskaImporter importer = new MorskaImporter(db, googleSheetValues, this); MorskaImporter importer = new MorskaImporter(_db, _googleSheetValues, this);
importer.import(importWorker); importer.import(importWorker);
Thread.Sleep(5000); // be aware of GSheet API quota 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, Type = LogEntryType.warning,
LogType = LogType.import, LogType = LogType.import,
Message = "Success (reimported)", Message = "Success (reimported)",
@@ -316,9 +291,9 @@ namespace WebAPI.Controllers
} }
else else
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{importWorker!.Name}, {importWorker.Id}", Title = $"{importWorker.Name}, {importWorker.Id}",
Type = LogEntryType.warning, Type = LogEntryType.warning,
LogType = LogType.import, LogType = LogType.import,
Message = "importLayer records are up of date. Not processed.", Message = "importLayer records are up of date. Not processed.",
@@ -329,9 +304,9 @@ namespace WebAPI.Controllers
} }
catch (Exception e) catch (Exception e)
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{importWorker!.Name}, {importWorker.Id}", Title = $"{importWorker.Name}, {importWorker.Id}",
Type = LogEntryType.error, Type = LogEntryType.error,
LogType = LogType.import, LogType = LogType.import,
Message = e.ToString(), Message = e.ToString(),
@@ -344,7 +319,7 @@ namespace WebAPI.Controllers
} }
catch (Exception e) catch (Exception e)
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = "Process error", Title = "Process error",
Type = LogEntryType.error, Type = LogEntryType.error,
@@ -361,17 +336,17 @@ namespace WebAPI.Controllers
[AllowAnonymous] [AllowAnonymous]
public IActionResult AutoProcess(string apiKey) 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(); return Unauthorized();
} }
if (googleSheetValues is null) if (_googleSheetValues is null)
{ {
throw new Exception("Google Sheets API not initialized"); throw new Exception("Google Sheets API not initialized");
} }
string[] processTypes = new string[] string[] processTypes =
{ {
"T3-SingleSource", "T3-SingleSource",
"T3-SourceYearSummary", "T3-SourceYearSummary",
@@ -386,7 +361,7 @@ namespace WebAPI.Controllers
{ {
try try
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"Processing: {type}", Title = $"Processing: {type}",
Type = LogEntryType.info, Type = LogEntryType.info,
@@ -394,19 +369,19 @@ namespace WebAPI.Controllers
CreatedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow,
}); });
List<Layer> processWorkerLayers = db.Layers List<Layer> processWorkerLayers = _db.Layers
.Include(x => x.Records) .Include(x => x.Records)
.Where(x => .Where(x =>
x.Records!.Any(x => x.Code == "Type" && x.Desc1 == "ProcessWorker") && x.Records!.Any(y => y.Code == "Type" && y.Desc1 == "ProcessWorker") &&
x.Records!.Any(x => x.Code == "IsEnabled" && x.Desc1 == "True") && x.Records!.Any(y => y.Code == "IsEnabled" && y.Desc1 == "True") &&
x.Records!.Any(x => x.Code == "ProcessType" && x.Desc1 == type) x.Records!.Any(y => y.Code == "ProcessType" && y.Desc1 == type)
) )
.OrderBy(x => x.CreatedAt) .OrderBy(x => x.CreatedAt)
.ToList(); .ToList();
if (processWorkerLayers.Count() == 0) if (!processWorkerLayers.Any())
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = "No Layers to process.", Title = "No Layers to process.",
Type = LogEntryType.info, Type = LogEntryType.info,
@@ -423,9 +398,9 @@ namespace WebAPI.Controllers
} }
catch (Exception e) catch (Exception e)
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.error, Type = LogEntryType.error,
LogType = LogType.process, LogType = LogType.process,
Message = e.ToString(), Message = e.ToString(),
@@ -436,7 +411,7 @@ namespace WebAPI.Controllers
} }
catch (Exception e) catch (Exception e)
{ {
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = "Process error", Title = "Process error",
Type = LogEntryType.error, Type = LogEntryType.error,
@@ -450,34 +425,33 @@ namespace WebAPI.Controllers
return Ok(); 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"); throw new Exception("Google Sheets API not initialized");
} }
string? name = processWorker.Name; var year = processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1;
string? year = processWorker?.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1;
if (year == null) if (year == null)
{ {
throw new Exception("Year record nod found"); throw new Exception("Year record nod found");
} }
string? processType = processWorker?.Records?.SingleOrDefault(x => x.Code == "ProcessType")?.Desc1; var processType = processWorker.Records?.SingleOrDefault(x => x.Code == "ProcessType")?.Desc1;
if (processType == null) switch (processType)
{ {
case null:
throw new Exception("ProcessType record not found"); throw new Exception("ProcessType record not found");
} case "T3-SourceYearSummary":
if (processType == "T3-SourceYearSummary")
{ {
T3SourceYearSummaryProcessor processor = new T3SourceYearSummaryProcessor(db, googleSheetValues, this); T3SourceYearSummaryProcessor processor =
processor.process(processWorker!); new T3SourceYearSummaryProcessor(_db, _googleSheetValues, this);
processor.process(processWorker);
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info, Type = LogEntryType.info,
LogType = LogType.process, LogType = LogType.process,
Message = "Success", Message = "Success",
@@ -485,16 +459,15 @@ namespace WebAPI.Controllers
}); });
return; return;
} }
case "T3-MultiSourceYearSummary":
if (processType == "T3-MultiSourceYearSummary")
{ {
T3MultiSourceYearSummaryProcessor processor = T3MultiSourceYearSummaryProcessor processor =
new T3MultiSourceYearSummaryProcessor(db, googleSheetValues, this, logsController); new T3MultiSourceYearSummaryProcessor(_db, _googleSheetValues, this, _logsController);
processor.process(processWorker!); processor.process(processWorker);
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info, Type = LogEntryType.info,
LogType = LogType.process, LogType = LogType.process,
Message = "Success", Message = "Success",
@@ -502,16 +475,15 @@ namespace WebAPI.Controllers
}); });
return; return;
} }
case "T3-MultiSourceCopySelectedCodesYearSummary":
if (processType == "T3-MultiSourceCopySelectedCodesYearSummary")
{ {
T3MultiSourceCopySelectedCodesYearSummaryProcessor processor = T3MultiSourceCopySelectedCodesYearSummaryProcessor processor =
new T3MultiSourceCopySelectedCodesYearSummaryProcessor(db, googleSheetValues, this); new T3MultiSourceCopySelectedCodesYearSummaryProcessor(_db, _googleSheetValues, this);
processor.process(processWorker!); processor.process(processWorker);
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info, Type = LogEntryType.info,
LogType = LogType.process, LogType = LogType.process,
Message = "Success", Message = "Success",
@@ -519,15 +491,14 @@ namespace WebAPI.Controllers
}); });
return; return;
} }
case "T1-R1":
if (processType == "T1-R1")
{ {
T1R1Processor processor = new T1R1Processor(db, googleSheetValues, this, logsController); T1R1Processor processor = new T1R1Processor(_db, _googleSheetValues, this, _logsController);
processor.process(processWorker!); processor.process(processWorker);
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info, Type = LogEntryType.info,
LogType = LogType.process, LogType = LogType.process,
Message = "Success", Message = "Success",
@@ -535,15 +506,14 @@ namespace WebAPI.Controllers
}); });
return; return;
} }
case "T4-R2":
if (processType == "T4-R2")
{ {
T4R2Processor processor = new T4R2Processor(db, googleSheetValues, this, logsController); var processor = new T4R2Processor(_db, _googleSheetValues, this, _logsController);
processor.process(processWorker!); processor.process(processWorker);
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info, Type = LogEntryType.info,
LogType = LogType.process, LogType = LogType.process,
Message = "Success", Message = "Success",
@@ -551,8 +521,9 @@ namespace WebAPI.Controllers
}); });
return; return;
} }
}
string? month = processWorker?.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1; var month = processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1;
if (month == null) if (month == null)
{ {
throw new Exception("Month record not found"); throw new Exception("Month record not found");
@@ -562,35 +533,35 @@ namespace WebAPI.Controllers
{ {
case "T3-SingleSource": case "T3-SingleSource":
{ {
T3SingleSourceProcessor processor = new T3SingleSourceProcessor(db, googleSheetValues, this); T3SingleSourceProcessor processor = new T3SingleSourceProcessor(_db, _googleSheetValues, this);
processor.process(processWorker!); processor.process(processWorker);
break; break;
} }
case "T4-SingleSource": case "T4-SingleSource":
{ {
T4SingleSourceProcessor processor = new T4SingleSourceProcessor(db, googleSheetValues, this); T4SingleSourceProcessor processor = new T4SingleSourceProcessor(_db, _googleSheetValues, this);
processor.process(processWorker!); processor.process(processWorker);
break; break;
} }
case "T3-MultiSourceSummary": case "T3-MultiSourceSummary":
{ {
T3MultiSourceSummaryProcessor processor = T3MultiSourceSummaryProcessor processor =
new T3MultiSourceSummaryProcessor(db, googleSheetValues, this, logsController); new T3MultiSourceSummaryProcessor(_db, _googleSheetValues, this, _logsController);
processor.process(processWorker!); processor.process(processWorker);
break; break;
} }
case "T3-MultiSourceCopySelectedCodes": case "T3-MultiSourceCopySelectedCodes":
{ {
T3MultiSourceCopySelectedCodesProcessor processor = T3MultiSourceCopySelectedCodesProcessor processor =
new T3MultiSourceCopySelectedCodesProcessor(db, googleSheetValues, this); new T3MultiSourceCopySelectedCodesProcessor(_db, _googleSheetValues, this);
processor.process(processWorker!); processor.process(processWorker);
break; break;
} }
} }
logsController.AddEntry(new LogEntry _logsController.AddEntry(new LogEntry
{ {
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info, Type = LogEntryType.info,
LogType = LogType.process, LogType = LogType.process,
Message = "Success", Message = "Success",
@@ -600,12 +571,10 @@ namespace WebAPI.Controllers
internal void SaveRecords(Guid id, ICollection<Record> records, Guid currentUserId) internal void SaveRecords(Guid id, ICollection<Record> records, Guid currentUserId)
{ {
try List<Record> toDelete = _db.Records.Where(x => x.LayerId == id).ToList();
{
List<Record> toDelete = db.Records.Where(x => x.LayerId == id).ToList();
if (toDelete.Count > 0) if (toDelete.Count > 0)
{ {
db.Records.RemoveRange(toDelete); _db.Records.RemoveRange(toDelete);
} }
foreach (Record record in records) foreach (Record record in records)
@@ -615,18 +584,13 @@ namespace WebAPI.Controllers
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);
}
}
catch (Exception)
{
throw;
} }
} }
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}"); Console.WriteLine($"DiunaLog: {message}");
} }
@@ -634,12 +598,12 @@ namespace WebAPI.Controllers
private bool IsImportedLayerUpToDate(Layer importWorker) private bool IsImportedLayerUpToDate(Layer importWorker)
{ {
if (googleSheetValues is null) if (_googleSheetValues is null)
{ {
throw new Exception("Google Sheets API not initialized"); throw new Exception("Google Sheets API not initialized");
} }
Layer? newestLayer = db.Layers var newestLayer = _db.Layers
.Include(x => x.Records) .Include(x => x.Records)
.Where(x => x.ParentId == importWorker.Id) .Where(x => x.ParentId == importWorker.Id)
.OrderByDescending(x => x.CreatedAt) .OrderByDescending(x => x.CreatedAt)
@@ -650,53 +614,48 @@ namespace WebAPI.Controllers
return true; // importWorker is not active yet, no check needed 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) if (sheetId == null)
{ {
throw new Exception($"SheetId not found, {importWorker.Name}"); 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) if (sheetTabName == null)
{ {
throw new Exception($"SheetTabName not found, {importWorker.Name}"); 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) if (dataRange == null)
{ {
throw new Exception($"DataRange not found, {importWorker.Name}"); 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; 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() == "") continue;
if (data[0][i].ToString() != "") var record = newestLayer.Records!.FirstOrDefault(x => x.Code == data[0][i].ToString());
{
Record? record = newestLayer.Records!.Where(x => x.Code == data[0][i].ToString()).FirstOrDefault();
if (record == null) if (record == null)
{ {
WriteToConsole("Code not found in DiunaBI", data[0][i].ToString()!); WriteToConsole("Code not found in DiunaBI", data[0][i].ToString()!);
isUpToDate = false; isUpToDate = false;
continue; continue;
} }
else if (
double.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out value) && if ((!double.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"),
record.Value1 != value) out var value) ||
{ double.Abs((double)(record.Value1-value)!) > 0.01)) continue;
WriteToConsole( WriteToConsole(
$"Code: {data[0][i]}. DiunaBI: {string.Format("{0:N2}", record.Value1)}. GoogleSheet: {data[1][i]}"); $"Code: {data[0][i]}. DiunaBI: {record.Value1:N2}. GoogleSheet: {data[1][i]}");
isUpToDate = false; isUpToDate = false;
} }
} foreach (var record in newestLayer.Records!)
}
foreach (Record record in newestLayer.Records!)
{ {
if (data[0].Contains(record.Code)) if (data[0].Contains(record.Code))
{ {
@@ -706,7 +665,6 @@ namespace WebAPI.Controllers
WriteToConsole($"Code not found in GoogleSheet: {record.Code}"); WriteToConsole($"Code not found in GoogleSheet: {record.Code}");
isUpToDate = false; isUpToDate = false;
} }
return isUpToDate; return isUpToDate;
} }
} }

View File

@@ -1,15 +1,5 @@
using Google.Apis.Auth;
using Google.Apis.Http;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; 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 namespace WebAPI.Controllers
{ {
@@ -18,11 +8,11 @@ namespace WebAPI.Controllers
[Authorize] [Authorize]
public class PingController : Controller public class PingController : Controller
{ {
private readonly IConfiguration configuration; private readonly IConfiguration _configuration;
public PingController( public PingController(
IConfiguration _configuration) IConfiguration configuration)
{ {
configuration = _configuration; _configuration = configuration;
} }
[HttpGet] [HttpGet]
@@ -30,7 +20,7 @@ namespace WebAPI.Controllers
[AllowAnonymous] [AllowAnonymous]
public IActionResult Ping() public IActionResult Ping()
{ {
return Ok(configuration["PONG"]); return Ok(_configuration["PONG"]);
} }
} }
} }

View File

@@ -2,13 +2,13 @@
using Google.Apis.Drive.v3; using Google.Apis.Drive.v3;
using Google.Apis.Services; using Google.Apis.Services;
namespace WebAPI namespace WebAPI;
public class GoogleDriveHelper
{ {
public class GoogleDriveHelper
{
public DriveService? Service { get; set; } public DriveService? Service { get; set; }
const string APPLICATION_NAME = "Diuna"; private const string ApplicationName = "Diuna";
static readonly string[] Scopes = { DriveService.Scope.Drive }; private static readonly string[] Scopes = { DriveService.Scope.Drive };
public GoogleDriveHelper() public GoogleDriveHelper()
{ {
InitializeService(); InitializeService();
@@ -16,24 +16,21 @@ namespace WebAPI
private void InitializeService() private void InitializeService()
{ {
var credential = GetCredentialsFromFile(); var credential = GetCredentialsFromFile();
Service = new DriveService(new BaseClientService.Initializer() Service = new DriveService(new BaseClientService.Initializer
{ {
HttpClientInitializer = credential, HttpClientInitializer = credential,
ApplicationName = APPLICATION_NAME ApplicationName = ApplicationName
}); });
} }
private GoogleCredential GetCredentialsFromFile() private static GoogleCredential GetCredentialsFromFile()
{ {
string fileName = "client_secrets.json"; // ReSharper disable once RedundantAssignment
var fileName = "client_secrets.json";
#if DEBUG #if DEBUG
fileName = "client_secrets.Development.json"; fileName = "client_secrets.Development.json";
#endif #endif
GoogleCredential credential; using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) var credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
{
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
}
return credential; return credential;
} }
}
} }

View File

@@ -2,13 +2,13 @@
using Google.Apis.Services; using Google.Apis.Services;
using Google.Apis.Sheets.v4; 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";
public SheetsService? Service { get; set; } private static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets };
const string APPLICATION_NAME = "Diuna";
static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets };
public GoogleSheetsHelper() public GoogleSheetsHelper()
{ {
InitializeService(); InitializeService();
@@ -16,24 +16,21 @@ namespace WebAPI
private void InitializeService() private void InitializeService()
{ {
var credential = GetCredentialsFromFile(); var credential = GetCredentialsFromFile();
Service = new SheetsService(new BaseClientService.Initializer() Service = new SheetsService(new BaseClientService.Initializer
{ {
HttpClientInitializer = credential, HttpClientInitializer = credential,
ApplicationName = APPLICATION_NAME ApplicationName = ApplicationName
}); });
} }
private GoogleCredential GetCredentialsFromFile() private static GoogleCredential GetCredentialsFromFile()
{ {
string fileName = "client_secrets.json"; // ReSharper disable once RedundantAssignment
var fileName = "client_secrets.json";
#if DEBUG #if DEBUG
fileName = "client_secrets.Development.json"; fileName = "client_secrets.Development.json";
#endif #endif
GoogleCredential credential; using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) var credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
{
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
}
return credential; return credential;
} }
}
} }

View File

@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json.Linq;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using System.Text; using System.Text;
using WebAPI; using WebAPI;
@@ -16,14 +15,14 @@ builder.Services.AddDbContext<AppDbContext>(x => {
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
options.AddPolicy("CORSPolicy", builder => options.AddPolicy("CORSPolicy", corsPolicyBuilder =>
{ {
builder.WithOrigins("http://localhost:4200") corsPolicyBuilder.WithOrigins("http://localhost:4200")
.AllowAnyMethod() .AllowAnyMethod()
.AllowAnyHeader() .AllowAnyHeader()
.AllowCredentials(); .AllowCredentials();
builder.WithOrigins("https://diuna.bim-it.pl") corsPolicyBuilder.WithOrigins("https://diuna.bim-it.pl")
.AllowAnyMethod() .AllowAnyMethod()
.AllowAnyHeader() .AllowAnyHeader()
.AllowCredentials(); .AllowCredentials();
@@ -60,7 +59,7 @@ var app = builder.Build();
app.Use(async (context, next) => 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")) { if (token.Length > 0 && !context.Request.Path.ToString().Contains("getForPowerBI")) {
var handler = new JwtSecurityTokenHandler(); var handler = new JwtSecurityTokenHandler();
var data = handler.ReadJwtToken(token.Split(' ')[1]); var data = handler.ReadJwtToken(token.Split(' ')[1]);

View File

@@ -140,7 +140,7 @@ namespace WebAPI.dataProcessors
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.warning, Type = LogEntryType.warning,
LogType = LogType.process, 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 CreatedAt = DateTime.UtcNow
}); });
continue; continue;
@@ -157,7 +157,7 @@ namespace WebAPI.dataProcessors
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.warning, Type = LogEntryType.warning,
LogType = LogType.process, 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 CreatedAt = DateTime.UtcNow
}); });
} }

View File

@@ -176,7 +176,7 @@ namespace WebAPI.dataProcessors
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.warning, Type = LogEntryType.warning,
LogType = LogType.process, 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 CreatedAt = DateTime.UtcNow
}); });
continue; continue;
@@ -193,7 +193,7 @@ namespace WebAPI.dataProcessors
Title = $"{processWorker!.Name}, {processWorker.Id}", Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.warning, Type = LogEntryType.warning,
LogType = LogType.process, 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 CreatedAt = DateTime.UtcNow
}); });
} }