Record last time an environmental device reported

This commit is contained in:
2024-05-28 13:53:02 -04:00
parent d69d69bf68
commit b1a9230f91
12 changed files with 181 additions and 5 deletions

View File

@@ -0,0 +1,45 @@
using ChrisKaczor.HomeMonitor.Environment.Service.Data;
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Device;
using Microsoft.AspNetCore.Mvc;
namespace ChrisKaczor.HomeMonitor.Environment.Service.Controllers;
[Route("[controller]")]
[ApiController]
public class DeviceController(Database database, IConfiguration configuration) : ControllerBase
{
[HttpGet()]
public async Task<ActionResult<List<Device>>> GetDevices()
{
return (await database.GetDevicesAsync()).ToList();
}
[HttpGet("{name}")]
public async Task<ActionResult<Device>> GetDevice(string name)
{
var device = await database.GetDeviceAsync(name);
if (device == null)
return NotFound();
return device;
}
[HttpPost()]
public async Task<ActionResult> AddDevice(Device device)
{
HttpContext.Request.Headers.TryGetValue("Authorization", out var authorizationHeader);
if (authorizationHeader != "Bearer " + configuration["AuthorizationToken"])
return Unauthorized();
var existingDevice = await database.GetDeviceAsync(device.Name);
if (existingDevice != null)
return BadRequest("Device already exists");
await database.SetDeviceLastUpdatedAsync(device.Name, device.LastUpdated);
return Ok();
}
}