From 26850c73effb59e06e9466db73ef31d766323685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieliski?= Date: Wed, 3 Jul 2024 22:05:04 +0200 Subject: [PATCH] WIP: DataInbox API (PUT/GET) --- WebAPI/Controllers/DataInboxController.cs | 19 +++++++++++++++---- WebAPI/Models/DataInbox.cs | 4 ++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/WebAPI/Controllers/DataInboxController.cs b/WebAPI/Controllers/DataInboxController.cs index 01ec738..d64bc75 100644 --- a/WebAPI/Controllers/DataInboxController.cs +++ b/WebAPI/Controllers/DataInboxController.cs @@ -10,16 +10,16 @@ namespace WebAPI.Controllers; [Route("api/[controller]")] public class DataInboxController : Controller { - private readonly GoogleDriveHelper _googleDriveHelper; + private readonly AppDbContext _db; private readonly IConfiguration _configuration; private readonly LogsController _logsController; public DataInboxController( - GoogleDriveHelper googleDriveHelper, + AppDbContext db, GoogleSheetsHelper googleSheetsHelper, IConfiguration configuration) { - _googleDriveHelper = googleDriveHelper; + _db = db; _configuration = configuration; _logsController = new LogsController(googleSheetsHelper, _configuration); } @@ -27,7 +27,7 @@ public class DataInboxController : Controller [HttpPut] [Route("Add/{apiKey}")] [AllowAnonymous] - public IActionResult Add([FromQuery] string apiKey, [FromBody] DataInbox dataInbox) + public IActionResult Add(string apiKey, [FromBody] DataInbox dataInbox) { if (apiKey != _configuration["apiKey"]) { @@ -43,6 +43,11 @@ public class DataInboxController : Controller try { + dataInbox.Id = Guid.NewGuid(); + dataInbox.CreatedAt = DateTime.UtcNow; + _db.DataInbox.Add(dataInbox); + _db.SaveChanges(); + _logsController.AddEntry(new LogEntry { Title = $"Insert success: {dataInbox.Source}, {dataInbox.Name}", @@ -66,4 +71,10 @@ public class DataInboxController : Controller return BadRequest(e.ToString()); } } + + [HttpGet] + public IActionResult GetAll() + { + return Ok(_db.DataInbox); + } } \ No newline at end of file diff --git a/WebAPI/Models/DataInbox.cs b/WebAPI/Models/DataInbox.cs index e9a8d7d..c9a9503 100644 --- a/WebAPI/Models/DataInbox.cs +++ b/WebAPI/Models/DataInbox.cs @@ -6,13 +6,13 @@ public class DataInbox { #region Properties [Key] - public Guid Id { get; init; } + public Guid Id { get; set; } [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; } + public DateTime CreatedAt { get; set; } #endregion } \ No newline at end of file