Split Electricity API days endpoint
This commit is contained in:
@@ -23,23 +23,38 @@ public class ElectricityLogController : ControllerBase
|
||||
|
||||
[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 BadRequest();
|
||||
return BadDateParameter(nameof(start));
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(stop))
|
||||
if (string.IsNullOrWhiteSpace(stop) || !DateOnly.TryParseExact(stop, Constants.isoDateFormat, out _))
|
||||
{
|
||||
return await electricityService.GetDetailsFor(start);
|
||||
return BadDateParameter(nameof(stop));
|
||||
}
|
||||
|
||||
if (!DateOnly.TryParseExact(stop, Constants.isoDateFormat, out _) || string.Compare(start, stop) > 0)
|
||||
if (string.Compare(start, stop) > 0)
|
||||
{
|
||||
return BadRequest();
|
||||
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}).");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user