61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using System.Net.Mime;
|
|
using Electricity.Api.Models;
|
|
using Electricity.Api.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Electricity.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Consumes(MediaTypeNames.Application.Json)]
|
|
[Produces(MediaTypeNames.Application.Json)]
|
|
public class ElectricityLogController : ControllerBase
|
|
{
|
|
private readonly ElectricityService electricityService;
|
|
private readonly ILogger<ElectricityLogController> logger;
|
|
|
|
public ElectricityLogController(
|
|
ElectricityService electricityService,
|
|
ILogger<ElectricityLogController> logger)
|
|
{
|
|
this.electricityService = electricityService;
|
|
this.logger = logger;
|
|
}
|
|
|
|
[HttpGet()]
|
|
[Route("/day")]
|
|
public async Task<ActionResult<Minute[]>> Get([FromQuery] string? date)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(date) || !DateOnly.TryParseExact(date, Constants.isoDateFormat, out _))
|
|
{
|
|
return BadDateParameter(nameof(date));
|
|
}
|
|
|
|
return await electricityService.GetDetailsFor(date);
|
|
}
|
|
|
|
[HttpGet()]
|
|
[Route("/days")]
|
|
public async Task<ActionResult<Day[]>> Get([FromQuery] string? start, [FromQuery] string? stop)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(start) || !DateOnly.TryParseExact(start, Constants.isoDateFormat, out _))
|
|
{
|
|
return BadDateParameter(nameof(start));
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(stop) || !DateOnly.TryParseExact(stop, Constants.isoDateFormat, out _))
|
|
{
|
|
return BadDateParameter(nameof(stop));
|
|
}
|
|
|
|
if (string.Compare(start, stop) > 0)
|
|
{
|
|
return BadRequest($"The date argument of {nameof(stop)} must be greater or equal to the argument value of {nameof(start)}");
|
|
}
|
|
|
|
return await electricityService.GetSummariesFor(start, stop);
|
|
}
|
|
|
|
private BadRequestObjectResult BadDateParameter(string parameterName) =>
|
|
BadRequest($"The search parameter {parameterName} is missing or is not a valid ISO date ({Constants.isoDateFormat}).");
|
|
}
|