Split Electricity API days endpoint
This commit is contained in:
@@ -23,23 +23,38 @@ public class ElectricityLogController : ControllerBase
|
|||||||
|
|
||||||
[HttpGet()]
|
[HttpGet()]
|
||||||
[Route("/day")]
|
[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)
|
public async Task<ActionResult<Day[]>> Get([FromQuery] string? start, [FromQuery] string? stop)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(start) || !DateOnly.TryParseExact(start, Constants.isoDateFormat, out _))
|
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);
|
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}).");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,5 +12,12 @@
|
|||||||
public double TotalPowerReturnNight { get; set; }
|
public double TotalPowerReturnNight { get; set; }
|
||||||
public long DayTarifEnabled { get; set; }
|
public long DayTarifEnabled { get; set; }
|
||||||
public double GasConsumptionInCubicMeters { get; set; }
|
public double GasConsumptionInCubicMeters { get; set; }
|
||||||
|
|
||||||
|
public DateTime GetDateTime() => DateTime
|
||||||
|
.Parse(
|
||||||
|
$"{Date}T{TimeUtc}Z",
|
||||||
|
null,
|
||||||
|
System.Globalization.DateTimeStyles.AssumeUniversal)
|
||||||
|
.ToUniversalTime();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/Electricity.Api/Extensions/DateTimeExtensions.cs
Normal file
7
src/Electricity.Api/Extensions/DateTimeExtensions.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Electricity.Api.Extensions;
|
||||||
|
|
||||||
|
public static class DateTimeExtensions
|
||||||
|
{
|
||||||
|
public static uint ToEpoch(this DateTime dateTime) =>
|
||||||
|
(uint)Math.Round((dateTime - DateTime.UnixEpoch).TotalSeconds);
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using Electricity.Api.Extensions;
|
||||||
|
|
||||||
namespace Electricity.Api.Models;
|
namespace Electricity.Api.Models;
|
||||||
|
|
||||||
public record Day(uint DateTime, double TotalPowerUse, double TotalPowerReturn, double TotalGasUse)
|
public record Day(uint DateTime, double TotalPowerUse, double TotalPowerReturn, double TotalGasUse)
|
||||||
@@ -5,32 +7,17 @@ public record Day(uint DateTime, double TotalPowerUse, double TotalPowerReturn,
|
|||||||
public static Day Empty(DateOnly date)
|
public static Day Empty(DateOnly date)
|
||||||
{
|
{
|
||||||
return new Day(
|
return new Day(
|
||||||
ConvertToEpoch(date.ToDateTime(new TimeOnly(0, 0), DateTimeKind.Utc)),
|
date.ToDateTime(new TimeOnly(0, 0), DateTimeKind.Utc).ToEpoch(),
|
||||||
0, 0, 0);
|
0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Day FromEntity(Entities.ElectricityLog entity)
|
public static Day FromEntity(Entities.ElectricityLog entity)
|
||||||
{
|
{
|
||||||
return new Day(
|
return new Day(
|
||||||
DateTime: ConvertToEpoch(ParseEntityDateTime(entity.Date, entity.TimeUtc)),
|
DateTime: entity.GetDateTime().ToEpoch(),
|
||||||
TotalPowerUse: entity.TotalPowerConsumptionDay + entity.TotalPowerConsumptionNight,
|
TotalPowerUse: entity.TotalPowerConsumptionDay + entity.TotalPowerConsumptionNight,
|
||||||
TotalPowerReturn: entity.TotalPowerReturnDay + entity.TotalPowerReturnNight,
|
TotalPowerReturn: entity.TotalPowerReturnDay + entity.TotalPowerReturnNight,
|
||||||
TotalGasUse: entity.GasConsumptionInCubicMeters
|
TotalGasUse: entity.GasConsumptionInCubicMeters
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DateTime ParseEntityDateTime(string date, string timeUtc)
|
|
||||||
{
|
|
||||||
return System.DateTime
|
|
||||||
.Parse(
|
|
||||||
$"{date}T{timeUtc}Z",
|
|
||||||
null,
|
|
||||||
System.Globalization.DateTimeStyles.AssumeUniversal)
|
|
||||||
.ToUniversalTime();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static uint ConvertToEpoch(DateTime dateTime)
|
|
||||||
{
|
|
||||||
return (uint)Math.Round((dateTime - System.DateTime.UnixEpoch).TotalSeconds);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
24
src/Electricity.Api/Models/Minute.cs
Normal file
24
src/Electricity.Api/Models/Minute.cs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
using Electricity.Api.Extensions;
|
||||||
|
|
||||||
|
namespace Electricity.Api.Models;
|
||||||
|
|
||||||
|
public record Minute(uint DateTime, double CurrentPowerUsage, double TotalPowerUse, double TotalPowerReturn, double TotalGasUse)
|
||||||
|
{
|
||||||
|
public static Minute Empty(DateOnly date)
|
||||||
|
{
|
||||||
|
return new Minute(
|
||||||
|
date.ToDateTime(new TimeOnly(0, 0), DateTimeKind.Utc).ToEpoch(),
|
||||||
|
0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Minute FromEntity(Entities.ElectricityLog entity)
|
||||||
|
{
|
||||||
|
return new Minute(
|
||||||
|
DateTime: entity.GetDateTime().ToEpoch(),
|
||||||
|
CurrentPowerUsage: entity.CurrentPowerUsage,
|
||||||
|
TotalPowerUse: entity.TotalPowerConsumptionDay + entity.TotalPowerConsumptionNight,
|
||||||
|
TotalPowerReturn: entity.TotalPowerReturnDay + entity.TotalPowerReturnNight,
|
||||||
|
TotalGasUse: entity.GasConsumptionInCubicMeters
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -12,13 +12,13 @@ public class ElectricityService
|
|||||||
this.databaseContext = databaseContext;
|
this.databaseContext = databaseContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Day[]> GetDetailsFor(string date)
|
public async Task<Minute[]> GetDetailsFor(string date)
|
||||||
{
|
{
|
||||||
return (await databaseContext.ElectricityLogs
|
return (await databaseContext.ElectricityLogs
|
||||||
.Where(l => l.Date == date)
|
.Where(l => l.Date == date)
|
||||||
.OrderBy(l => l.TimeUtc)
|
.OrderBy(l => l.TimeUtc)
|
||||||
.ToArrayAsync())
|
.ToArrayAsync())
|
||||||
.Select(Day.FromEntity)
|
.Select(Minute.FromEntity)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user