Files
DiunaBI/WebAPI/Controllers/LayersController.cs

418 lines
18 KiB
C#
Raw Normal View History

using System.Globalization;
using System.Xml.Serialization;
2023-11-09 15:17:22 +01:00
using DiunaBIWebAPI.dataImporters;
2023-02-22 12:12:38 +01:00
using Google.Apis.Sheets.v4;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebAPI.dataParsers;
2023-10-02 19:59:43 +02:00
using WebAPI.dataProcessors;
2023-02-22 12:12:38 +01:00
using WebAPI.Exports;
using WebAPI.Models;
namespace WebAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
2023-08-21 12:30:18 +02:00
2023-02-22 12:12:38 +01:00
public class LayersController : Controller
{
private readonly AppDbContext db;
private SpreadsheetsResource.ValuesResource googleSheetValues;
private GoogleDriveHelper googleDriveHelper;
2023-08-29 14:55:31 +02:00
private GoogleSheetsHelper googleSheetsHelper;
2023-02-22 12:12:38 +01:00
private readonly IConfiguration configuration;
private readonly LogsController logsController;
2023-02-22 12:12:38 +01:00
public LayersController(
AppDbContext _db,
GoogleSheetsHelper _googleSheetsHelper,
GoogleDriveHelper _googleDriveHelper,
IConfiguration _configuration)
{
db = _db;
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
2023-08-29 14:55:31 +02:00
googleSheetsHelper = _googleSheetsHelper;
2023-02-22 12:12:38 +01:00
googleDriveHelper = _googleDriveHelper;
configuration = _configuration;
logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
2023-02-22 12:12:38 +01:00
}
[HttpGet]
2023-10-20 13:56:40 +02:00
public IActionResult GetAll(int start, int limit, string? name, LayerType? type)
2023-02-22 12:12:38 +01:00
{
try
2023-08-29 14:55:31 +02:00
{
2023-10-20 13:56:40 +02:00
IQueryable<Layer> response = db.Layers.Where(x => !x.IsDeleted);
2023-11-06 17:19:20 +01:00
if (name != null)
2023-08-29 14:55:31 +02:00
{
2023-11-06 17:19:20 +01:00
response = response.Where(x => x.Name.Contains(name));
}
2023-11-06 17:19:20 +01:00
if (type != null)
2023-08-29 14:55:31 +02:00
{
2023-11-06 17:19:20 +01:00
response = response.Where(x => x.Type == type);
2023-06-25 19:08:26 +02:00
}
2023-10-20 13:56:40 +02:00
return Ok(response
.OrderByDescending(x => x.Number)
.Skip(start).Take(limit).ToList());
2023-11-06 17:19:20 +01:00
2023-02-22 12:12:38 +01:00
}
catch (Exception e)
{
return BadRequest(e.ToString());
}
}
[HttpPost]
public IActionResult Save(Layer input)
{
try
{
2023-11-09 15:17:22 +01:00
//Request.Headers.TryGetValue("userId", out var value);
//Guid currentUserId = new Guid(value!);
return Ok();
}
catch (Exception e)
2023-02-22 12:12:38 +01:00
{
return BadRequest(e.ToString());
}
}
[HttpGet]
[Route("{id}")]
public IActionResult Get(Guid id)
{
try
{
return Ok(db.Layers
.Include(x => x.CreatedBy)
.Include(x => x.Records)
.Where(x => x.Id == id && !x.IsDeleted).First());
}
catch (Exception e)
{
return BadRequest(e.ToString());
}
}
[HttpGet]
[Route("exportToGoogleSheet/{id}")]
public IActionResult ExportToGoogleSheet(Guid id)
{
Layer layer = db.Layers
2023-08-23 17:30:25 +02:00
.Include(x => x.Records!.OrderByDescending(x => x.Code))
2023-02-22 12:12:38 +01:00
.Where(x => x.Id == id && !x.IsDeleted).First();
2023-08-23 17:30:25 +02:00
var export = new googleSheetExport(googleDriveHelper, googleSheetValues, configuration);
2023-02-22 12:12:38 +01:00
export.export(layer);
return Ok(true);
}
[HttpGet]
2023-08-29 14:55:31 +02:00
[Route("AutoImport/{apiKey}")]
2023-02-22 12:12:38 +01:00
[AllowAnonymous]
2023-08-29 14:55:31 +02:00
public IActionResult AutoImport(string apiKey)
2023-02-22 12:12:38 +01:00
{
2023-08-30 23:18:54 +02:00
if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"])
2023-02-22 12:12:38 +01:00
{
return Unauthorized();
}
2023-11-09 15:17:22 +01:00
try
{
List<Layer> 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")
)
.OrderBy(x => x.CreatedAt)
.ToList();
if (importWorkerLayers.Count() == 0)
{
logsController.AddEntry(new LogEntry
{
Title = "No Layers to import.",
Type = LogEntryType.info,
LogType = LogType.import,
CreatedAt = DateTime.UtcNow,
});
return Ok();
}
foreach (Layer importWorker in importWorkerLayers)
{
try
{
string? startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1;
if (startDate == null)
{
throw new Exception("Year record nod found");
}
string? endDate = importWorker.Records!.Where(x => x.Code == "EndDate").First().Desc1;
if (endDate == null)
{
throw new Exception("Year record nod found");
}
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);
importer.import(importWorker);
logsController.AddEntry(new LogEntry
{
Title = $"{importWorker!.Name}, {importWorker.Id}",
Type = LogEntryType.info,
LogType = LogType.import,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
}
else
{
logsController.AddEntry(new LogEntry
{
Title = $"{importWorker!.Name}, {importWorker.Id}",
Type = LogEntryType.warning,
LogType = LogType.import,
Message = "importLayer is out of date. Should be disabled. Not processed.",
CreatedAt = DateTime.UtcNow
});
}
} catch(Exception e)
{
logsController.AddEntry(new LogEntry
{
Title = $"{importWorker!.Name}, {importWorker.Id}",
Type = LogEntryType.error,
LogType = LogType.import,
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
}
}
return Ok();
} catch(Exception e)
{
logsController.AddEntry(new LogEntry
2023-08-29 14:55:31 +02:00
{
2023-11-09 15:17:22 +01:00
Title = "Process error",
2023-08-29 14:55:31 +02:00
Type = LogEntryType.error,
2023-09-01 18:23:53 +02:00
LogType = LogType.import,
2023-08-29 14:55:31 +02:00
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
2023-11-09 15:17:22 +01:00
return BadRequest(e.ToString());
2023-08-29 14:55:31 +02:00
}
2023-10-02 19:59:43 +02:00
}
2023-08-20 13:05:10 +02:00
[HttpGet]
2023-11-08 18:09:01 +01:00
[Route("AutoProcess/{apiKey}/{type}")]
2023-08-20 13:05:10 +02:00
[AllowAnonymous]
2023-11-08 18:09:01 +01:00
public IActionResult AutoProcess(string apiKey, string type)
2023-08-20 13:05:10 +02:00
{
2023-08-30 23:18:54 +02:00
if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"])
2023-08-20 13:05:10 +02:00
{
return Unauthorized();
}
2023-08-29 14:55:31 +02:00
2023-09-17 13:00:24 +02:00
try
{
List<Layer> processWorkerLayers = db.Layers
2023-09-17 13:00:24 +02:00
.Include(x => x.Records)
2023-10-30 15:22:57 +01:00
.Where(x =>
x.Records!.Any(x => x.Code == "Type" && x.Desc1 == "ProcessWorker") &&
2023-11-08 18:09:01 +01:00
x.Records!.Any(x => x.Code == "IsEnabled" && x.Desc1 == "True") &&
x.Records!.Any(x => x.Code == "ProcessType" && x.Desc1 == type)
2023-10-30 16:39:13 +01:00
)
.OrderBy(x => x.CreatedAt)
2023-09-17 13:00:24 +02:00
.ToList();
2023-08-20 13:05:10 +02:00
if (processWorkerLayers.Count() == 0)
{
logsController.AddEntry(new LogEntry
{
Title = "No Layers to process.",
Type = LogEntryType.info,
LogType = LogType.process,
CreatedAt = DateTime.UtcNow,
});
return Ok();
}
foreach (Layer processWorker in processWorkerLayers)
{
try
{
string name = processWorker.Name;
string? year = processWorker?.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1;
if (year == null)
{
throw new Exception("Year record nod found");
}
if (int.Parse(year!) < DateTime.UtcNow.Year)
{
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.warning,
LogType = LogType.process,
Message = "processLayer is out of date. Should be disabled. Not processed.",
CreatedAt = DateTime.UtcNow
});
continue;
}
string? processType = processWorker?.Records?.SingleOrDefault(x => x.Code == "ProcessType")?.Desc1;
if (processType == null)
{
throw new Exception("ProcessType record not found");
}
if (processType == "T3-SourceYearSummary")
{
if (int.Parse(year!) < DateTime.UtcNow.Year)
{
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.warning,
LogType = LogType.process,
Message = "processLayer is out of date. Should be disabled. Not processed",
CreatedAt = DateTime.UtcNow
});
continue;
}
2023-11-06 23:11:30 +01:00
T3SourceYearSummaryProcessor processor = new T3SourceYearSummaryProcessor(db, googleSheetValues, this);
processor.process(processWorker!);
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.info,
LogType = LogType.process,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
continue;
}
if (processType == "T3-MultiSourceYearSummary")
{
if (int.Parse(year!) < DateTime.UtcNow.Year)
{
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.warning,
LogType = LogType.process,
Message = "processLayer is out of date. Should be disabled. Not processed",
CreatedAt = DateTime.UtcNow
});
continue;
}
T3MultiSourceYearSummaryProcessor processor = new T3MultiSourceYearSummaryProcessor(db, googleSheetValues, this);
processor.process(processWorker!);
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.info,
LogType = LogType.process,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
continue;
}
string? month = processWorker?.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1;
if (month == null)
{
throw new Exception("Month record not found");
}
if (int.Parse(month!) < DateTime.UtcNow.Month)
{
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.warning,
LogType = LogType.process,
Message = "processLayer is out of date. Should be disabled.",
CreatedAt = DateTime.UtcNow
});
}
switch (processType!)
{
case "T3-SingleSource":
{
T3SingleSourceProcessor processor = new T3SingleSourceProcessor(db, googleSheetValues, this);
processor.process(processWorker!);
break;
}
case "T3-MultiSourceSummary":
{
T3MultiSourceSummaryProcessor processor = new T3MultiSourceSummaryProcessor(db, googleSheetValues, this);
processor.process(processWorker!);
break;
}
}
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.info,
LogType = LogType.process,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
} catch (Exception e)
{
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker!.Name}, {processWorker.Id}",
Type = LogEntryType.error,
LogType = LogType.process,
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
}
}
2023-09-18 19:41:39 +02:00
return Ok();
2023-09-17 13:00:24 +02:00
}
catch (Exception e)
{
logsController.AddEntry(new LogEntry
{
Title = "Process error",
Type = LogEntryType.error,
2023-09-18 19:41:39 +02:00
LogType = LogType.process,
2023-09-17 13:00:24 +02:00
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
return BadRequest(e.ToString());
}
2023-10-02 19:59:43 +02:00
}
2023-10-20 09:28:42 +02:00
internal void SaveRecords(Guid id, ICollection<Record> records, Guid currentUserId)
2023-02-22 12:12:38 +01:00
{
try
{
2023-11-06 17:19:20 +01:00
List<Record> toDelete = db.Records.Where(x => x.LayerId == id).ToList();
if (toDelete.Count > 0)
{
db.Records.RemoveRange(toDelete);
}
2023-02-22 12:12:38 +01:00
foreach (Record record in records)
{
record.CreatedById = currentUserId;
record.CreatedAt = DateTime.UtcNow;
record.ModifiedById = currentUserId;
record.ModifiedAt = DateTime.UtcNow;
record.LayerId = id;
2023-02-22 12:12:38 +01:00
db.Records.Add(record);
}
}
catch (Exception)
{
throw;
}
2023-10-02 19:59:43 +02:00
}
2023-02-22 12:12:38 +01:00
}
2022-12-11 23:40:16 +01:00
}