WIP: DataInbox

This commit is contained in:
Michał Zieliski
2024-07-03 19:03:52 +02:00
parent fae5533556
commit cadb5e50a5
5 changed files with 91 additions and 1 deletions

View File

@@ -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());
}
}
}