mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-14 01:25:38 -05:00
Start adding API endpoints
This commit is contained in:
35
Environment/Service/Controllers/ReadingsController.cs
Normal file
35
Environment/Service/Controllers/ReadingsController.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user