Start adding API endpoints

This commit is contained in:
2024-02-01 17:56:33 -05:00
parent 6460430a69
commit d8f6e170dd
9 changed files with 103 additions and 12 deletions

View File

@@ -0,0 +1,35 @@
using ChrisKaczor.HomeMonitor.Environment.Service.Data;
using ChrisKaczor.HomeMonitor.Environment.Service.Models;
using Microsoft.AspNetCore.Mvc;
namespace ChrisKaczor.HomeMonitor.Environment.Service.Controllers;
[Route("[controller]")]
[ApiController]
public class ReadingsController(Database database) : ControllerBase
{
[HttpGet("recent")]
public async Task<ActionResult<IEnumerable<Readings>>> GetRecent([FromQuery] string? tz)
{
var recentReadings = await database.GetRecentReadings();
if (string.IsNullOrWhiteSpace(tz))
return Ok(recentReadings);
try
{
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tz);
foreach (var recentReading in recentReadings)
{
recentReading.Time = recentReading.Time.ToOffset(timeZoneInfo.GetUtcOffset(recentReading.Time));
}
}
catch (Exception e)
{
return BadRequest(e.Message);
}
return Ok(recentReadings);
}
}