From cadb5e50a54e05a8c24d4c1974450c067ea25bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieliski?= Date: Wed, 3 Jul 2024 19:03:52 +0200 Subject: [PATCH] WIP: DataInbox --- WebAPI/AppDbContext.cs | 1 + WebAPI/Controllers/DataInboxController.cs | 69 +++++++++++++++++++++++ WebAPI/Controllers/LogsController.cs | 1 + WebAPI/Models/DataInbox.cs | 18 ++++++ WebAPI/Models/LogEntry.cs | 3 +- 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 WebAPI/Controllers/DataInboxController.cs create mode 100644 WebAPI/Models/DataInbox.cs diff --git a/WebAPI/AppDbContext.cs b/WebAPI/AppDbContext.cs index 9aaaa15..0ba188f 100644 --- a/WebAPI/AppDbContext.cs +++ b/WebAPI/AppDbContext.cs @@ -9,6 +9,7 @@ public class AppDbContext : DbContext public DbSet Layers { get; init; } public DbSet Records { get; init; } public DbSet ProcessSources { get; init; } + public DbSet DataInbox { get; set; } public AppDbContext(DbContextOptions options) : base(options) { diff --git a/WebAPI/Controllers/DataInboxController.cs b/WebAPI/Controllers/DataInboxController.cs new file mode 100644 index 0000000..01ec738 --- /dev/null +++ b/WebAPI/Controllers/DataInboxController.cs @@ -0,0 +1,69 @@ +using System.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Data.SqlClient; +using WebAPI.Models; + +namespace WebAPI.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class DataInboxController : Controller +{ + private readonly GoogleDriveHelper _googleDriveHelper; + private readonly IConfiguration _configuration; + private readonly LogsController _logsController; + + public DataInboxController( + GoogleDriveHelper googleDriveHelper, + GoogleSheetsHelper googleSheetsHelper, + IConfiguration configuration) + { + _googleDriveHelper = googleDriveHelper; + _configuration = configuration; + _logsController = new LogsController(googleSheetsHelper, _configuration); + } + + [HttpPut] + [Route("Add/{apiKey}")] + [AllowAnonymous] + public IActionResult Add([FromQuery] string apiKey, [FromBody] DataInbox dataInbox) + { + if (apiKey != _configuration["apiKey"]) + { + _logsController.AddEntry(new LogEntry + { + Title = $"Unauthorized request - wrong apiKey ({dataInbox.Source})", + Type = LogEntryType.Warning, + LogType = LogType.DataInbox, + CreatedAt = DateTime.UtcNow + }); + return Unauthorized(); + } + + try + { + _logsController.AddEntry(new LogEntry + { + Title = $"Insert success: {dataInbox.Source}, {dataInbox.Name}", + Type = LogEntryType.Info, + LogType = LogType.DataInbox, + CreatedAt = DateTime.UtcNow + }); + + return Ok(); + } + catch (Exception e) + { + _logsController.AddEntry(new LogEntry + { + Title = $"Insert error: {dataInbox.Source}, {dataInbox.Name}", + Type = LogEntryType.Error, + LogType = LogType.DataInbox, + Message = e.ToString(), + CreatedAt = DateTime.UtcNow + }); + return BadRequest(e.ToString()); + } + } +} \ No newline at end of file diff --git a/WebAPI/Controllers/LogsController.cs b/WebAPI/Controllers/LogsController.cs index 3acfea8..08dc45d 100644 --- a/WebAPI/Controllers/LogsController.cs +++ b/WebAPI/Controllers/LogsController.cs @@ -32,6 +32,7 @@ public class LogsController : Controller LogType.Backup => "Backup", LogType.Process => "Process", LogType.PowerBi => "PowerBIAccess", + LogType.DataInbox => "DataInbox", _ => "Other" }; var response = _googleSheetValues.Get(_configuration["appLogsFile"], $"{type}!A:A").Execute(); diff --git a/WebAPI/Models/DataInbox.cs b/WebAPI/Models/DataInbox.cs new file mode 100644 index 0000000..e9a8d7d --- /dev/null +++ b/WebAPI/Models/DataInbox.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace WebAPI.Models; + +public class DataInbox +{ + #region Properties + [Key] + public Guid Id { get; init; } + [StringLength(50)] + public required string Name { get; init; } + [StringLength(50)] + public required string Source { get; set; } + [StringLength(int.MaxValue)] + public required string Data { get; init; } + public DateTime CreatedAt { get; init; } + #endregion +} \ No newline at end of file diff --git a/WebAPI/Models/LogEntry.cs b/WebAPI/Models/LogEntry.cs index f1e2852..ad4a9ab 100644 --- a/WebAPI/Models/LogEntry.cs +++ b/WebAPI/Models/LogEntry.cs @@ -10,7 +10,8 @@ public enum LogType { Import, Backup, Process, - PowerBi + PowerBi, + DataInbox } public class LogEntry {