From 363cefa00f98b220e198a8b9888c29e0c42527fb Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Fri, 29 Aug 2025 17:17:27 +0200 Subject: [PATCH] Format .NET projects with csharpier --- src/Electricity.Api/Constants.cs | 2 +- .../Controllers/ElectricityLogController.cs | 26 ++++-- src/Electricity.Api/DatabaseContext.cs | 8 +- .../Entities/ElectricityLog.cs | 14 +-- .../Extensions/DateTimeExtensions.cs | 2 +- src/Electricity.Api/Models/Day.cs | 6 +- src/Electricity.Api/Models/Minute.cs | 16 +++- src/Electricity.Api/Program.cs | 36 +++++--- .../Services/ElectricityService.cs | 16 ++-- src/Solar.Api/Constants/CacheKeys.cs | 2 +- src/Solar.Api/Constants/Format.cs | 2 +- .../Controllers/SolarLogController.cs | 64 +++++++++----- src/Solar.Api/Converters/DateOnlyConverter.cs | 9 +- src/Solar.Api/Converters/TimeOnlyConverter.cs | 9 +- src/Solar.Api/DatabaseContext.cs | 14 +-- src/Solar.Api/Models/DayResponse.cs | 3 +- src/Solar.Api/Models/DaySummaryLog.cs | 2 +- src/Solar.Api/Models/DaysResponse.cs | 2 +- src/Solar.Api/Models/EnvoyDayLog.cs | 2 +- src/Solar.Api/Models/MonthLog.cs | 2 +- .../Models/MonthSummariesResponse.cs | 2 +- src/Solar.Api/Models/ZeverDayLog.cs | 2 +- src/Solar.Api/Program.cs | 49 ++++++----- src/Solar.Api/Services/SolarService.cs | 85 ++++++++++--------- 24 files changed, 220 insertions(+), 155 deletions(-) diff --git a/src/Electricity.Api/Constants.cs b/src/Electricity.Api/Constants.cs index da12725..ba8ad00 100644 --- a/src/Electricity.Api/Constants.cs +++ b/src/Electricity.Api/Constants.cs @@ -3,4 +3,4 @@ namespace Electricity.Api; internal static class Constants { public const string isoDateFormat = "yyyy-MM-dd"; -} \ No newline at end of file +} diff --git a/src/Electricity.Api/Controllers/ElectricityLogController.cs b/src/Electricity.Api/Controllers/ElectricityLogController.cs index 405a709..2619d6f 100644 --- a/src/Electricity.Api/Controllers/ElectricityLogController.cs +++ b/src/Electricity.Api/Controllers/ElectricityLogController.cs @@ -15,7 +15,8 @@ public class ElectricityLogController : ControllerBase public ElectricityLogController( ElectricityService electricityService, - ILogger logger) + ILogger logger + ) { this.electricityService = electricityService; this.logger = logger; @@ -25,7 +26,10 @@ public class ElectricityLogController : ControllerBase [Route("/day")] public async Task> Get([FromQuery] string? date) { - if (string.IsNullOrWhiteSpace(date) || !DateOnly.TryParseExact(date, Constants.isoDateFormat, out _)) + if ( + string.IsNullOrWhiteSpace(date) + || !DateOnly.TryParseExact(date, Constants.isoDateFormat, out _) + ) { return BadDateParameter(nameof(date)); } @@ -37,24 +41,34 @@ public class ElectricityLogController : ControllerBase [Route("/days")] public async Task> 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 BadDateParameter(nameof(start)); } - if (string.IsNullOrWhiteSpace(stop) || !DateOnly.TryParseExact(stop, Constants.isoDateFormat, out _)) + 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 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})."); + BadRequest( + $"The search parameter {parameterName} is missing or is not a valid ISO date ({Constants.isoDateFormat})." + ); } diff --git a/src/Electricity.Api/DatabaseContext.cs b/src/Electricity.Api/DatabaseContext.cs index f2e7eae..baa988f 100644 --- a/src/Electricity.Api/DatabaseContext.cs +++ b/src/Electricity.Api/DatabaseContext.cs @@ -5,14 +5,10 @@ namespace Electricity.Api { public partial class DatabaseContext : DbContext { - public DatabaseContext() - { - } + public DatabaseContext() { } public DatabaseContext(DbContextOptions options) - : base(options) - { - } + : base(options) { } public virtual DbSet ElectricityLogs { get; set; } = null!; diff --git a/src/Electricity.Api/Entities/ElectricityLog.cs b/src/Electricity.Api/Entities/ElectricityLog.cs index f681641..6525a39 100644 --- a/src/Electricity.Api/Entities/ElectricityLog.cs +++ b/src/Electricity.Api/Entities/ElectricityLog.cs @@ -13,11 +13,13 @@ public long DayTarifEnabled { get; set; } public double GasConsumptionInCubicMeters { get; set; } - public DateTime GetDateTime() => DateTime - .Parse( - $"{Date}T{TimeUtc}Z", - null, - System.Globalization.DateTimeStyles.AssumeUniversal) - .ToUniversalTime(); + public DateTime GetDateTime() => + DateTime + .Parse( + $"{Date}T{TimeUtc}Z", + null, + System.Globalization.DateTimeStyles.AssumeUniversal + ) + .ToUniversalTime(); } } diff --git a/src/Electricity.Api/Extensions/DateTimeExtensions.cs b/src/Electricity.Api/Extensions/DateTimeExtensions.cs index a83c536..d51d4f5 100644 --- a/src/Electricity.Api/Extensions/DateTimeExtensions.cs +++ b/src/Electricity.Api/Extensions/DateTimeExtensions.cs @@ -4,4 +4,4 @@ public static class DateTimeExtensions { public static uint ToEpoch(this DateTime dateTime) => (uint)Math.Round((dateTime - DateTime.UnixEpoch).TotalSeconds); -} \ No newline at end of file +} diff --git a/src/Electricity.Api/Models/Day.cs b/src/Electricity.Api/Models/Day.cs index c704fea..f7b4496 100644 --- a/src/Electricity.Api/Models/Day.cs +++ b/src/Electricity.Api/Models/Day.cs @@ -6,9 +6,7 @@ public record Day(string Date, double TotalPowerUse, double TotalPowerReturn, do { public static Day Empty(DateOnly date) { - return new Day( - date.ToString(Constants.isoDateFormat), - 0, 0, 0); + return new Day(date.ToString(Constants.isoDateFormat), 0, 0, 0); } public static Day FromEntity(Entities.ElectricityLog entity) @@ -20,4 +18,4 @@ public record Day(string Date, double TotalPowerUse, double TotalPowerReturn, do TotalGasUse: entity.GasConsumptionInCubicMeters ); } -}; \ No newline at end of file +}; diff --git a/src/Electricity.Api/Models/Minute.cs b/src/Electricity.Api/Models/Minute.cs index 23865d6..eddbcae 100644 --- a/src/Electricity.Api/Models/Minute.cs +++ b/src/Electricity.Api/Models/Minute.cs @@ -2,13 +2,23 @@ using Electricity.Api.Extensions; namespace Electricity.Api.Models; -public record Minute(uint DateTime, double CurrentPowerUsage, double TotalPowerUse, double TotalPowerReturn, double TotalGasUse) +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); + 0, + 0, + 0, + 0 + ); } public static Minute FromEntity(Entities.ElectricityLog entity) @@ -21,4 +31,4 @@ public record Minute(uint DateTime, double CurrentPowerUsage, double TotalPowerU TotalGasUse: entity.GasConsumptionInCubicMeters ); } -}; \ No newline at end of file +}; diff --git a/src/Electricity.Api/Program.cs b/src/Electricity.Api/Program.cs index 1d25f2e..500a6d0 100644 --- a/src/Electricity.Api/Program.cs +++ b/src/Electricity.Api/Program.cs @@ -7,10 +7,13 @@ internal class Program { private static int Main(string[] args) { - var rootCommand = new RootCommand("ElectricityServer is a small REST API to access the electricity log database"); + var rootCommand = new RootCommand( + "ElectricityServer is a small REST API to access the electricity log database" + ); var connectionStringArgument = new Option( name: "--connection-string", - description: "Filepath to the Sqlite3 database file (*.db)"); + description: "Filepath to the Sqlite3 database file (*.db)" + ); rootCommand.AddOption(connectionStringArgument); var result = rootCommand.Parse(args); @@ -27,15 +30,19 @@ internal class Program var sqlite3DatabaseFilePath = result.GetValueForOption(connectionStringArgument); if (!File.Exists(sqlite3DatabaseFilePath)) { - Console.WriteLine($"Sqlite3 database <{sqlite3DatabaseFilePath}> does not exist or is inaccessible"); + Console.WriteLine( + $"Sqlite3 database <{sqlite3DatabaseFilePath}> does not exist or is inaccessible" + ); return 1; } var builder = WebApplication.CreateBuilder(args); - builder.Services.AddDbContext((builder) => - { - builder.UseSqlite($"Data Source={sqlite3DatabaseFilePath}"); - }); + builder.Services.AddDbContext( + (builder) => + { + builder.UseSqlite($"Data Source={sqlite3DatabaseFilePath}"); + } + ); builder.Services.AddCors(options => { @@ -44,14 +51,19 @@ internal class Program policy => { policy.WithOrigins("http://localhost:8080"); - }); + } + ); }); - - builder.Services.AddControllers() + builder + .Services.AddControllers() .AddJsonOptions(config => { - config.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase; + config.JsonSerializerOptions.PropertyNamingPolicy = System + .Text + .Json + .JsonNamingPolicy + .CamelCase; config.JsonSerializerOptions.WriteIndented = true; }); builder.Services.AddSwaggerGen(); @@ -73,4 +85,4 @@ internal class Program return 0; } -} \ No newline at end of file +} diff --git a/src/Electricity.Api/Services/ElectricityService.cs b/src/Electricity.Api/Services/ElectricityService.cs index 5e4e5bb..dffa4dc 100644 --- a/src/Electricity.Api/Services/ElectricityService.cs +++ b/src/Electricity.Api/Services/ElectricityService.cs @@ -14,10 +14,12 @@ public class ElectricityService public async Task GetDetailsFor(string date) { - return (await databaseContext.ElectricityLogs - .Where(l => l.Date == date) - .OrderBy(l => l.TimeUtc) - .ToArrayAsync()) + return ( + await databaseContext + .ElectricityLogs.Where(l => l.Date == date) + .OrderBy(l => l.TimeUtc) + .ToArrayAsync() + ) .Select(Minute.FromEntity) .ToArray(); } @@ -39,8 +41,8 @@ public class ElectricityService private async Task GetSummaryFor(DateOnly date) { var databaseDate = date.ToString(Constants.isoDateFormat); - var baseQuery = databaseContext.ElectricityLogs - .Where(l => l.Date == databaseDate) + var baseQuery = databaseContext + .ElectricityLogs.Where(l => l.Date == databaseDate) .OrderBy(l => l.TimeUtc); var first = await baseQuery.FirstOrDefaultAsync(); if (first == null) @@ -63,4 +65,4 @@ public class ElectricityService lastAsModel.TotalGasUse - firstAsModel.TotalGasUse ); } -} \ No newline at end of file +} diff --git a/src/Solar.Api/Constants/CacheKeys.cs b/src/Solar.Api/Constants/CacheKeys.cs index d5c0c3c..c845622 100644 --- a/src/Solar.Api/Constants/CacheKeys.cs +++ b/src/Solar.Api/Constants/CacheKeys.cs @@ -6,4 +6,4 @@ internal static class CacheKeys { return $"month-summary-{source}-{year}-{month}"; } -} \ No newline at end of file +} diff --git a/src/Solar.Api/Constants/Format.cs b/src/Solar.Api/Constants/Format.cs index a28fa37..a82ecc2 100644 --- a/src/Solar.Api/Constants/Format.cs +++ b/src/Solar.Api/Constants/Format.cs @@ -3,4 +3,4 @@ namespace Solar.Api.Constants; internal static class Format { public const string Date = "yyyy-MM-dd"; -} \ No newline at end of file +} diff --git a/src/Solar.Api/Controllers/SolarLogController.cs b/src/Solar.Api/Controllers/SolarLogController.cs index cf9d563..9f9fd3e 100644 --- a/src/Solar.Api/Controllers/SolarLogController.cs +++ b/src/Solar.Api/Controllers/SolarLogController.cs @@ -1,8 +1,8 @@ +using System.Net.Mime; using Microsoft.AspNetCore.Mvc; using Solar.Api.Models; using Solar.Api.Services; using Swashbuckle.AspNetCore.Annotations; -using System.Net.Mime; namespace Solar.Api.Controllers; @@ -14,9 +14,7 @@ public class SolarLogController : ControllerBase private readonly SolarService solarService; private readonly ILogger logger; - public SolarLogController( - SolarService solarService, - ILogger logger) + public SolarLogController(SolarService solarService, ILogger logger) { this.solarService = solarService; this.logger = logger; @@ -25,10 +23,8 @@ public class SolarLogController : ControllerBase [HttpGet()] [Route("/day")] public async Task> GetDayDetails( - [SwaggerParameter(Required = true)] - [SwaggerSchema(Format = "date")] - [FromQuery] - string date) + [SwaggerParameter(Required = true)] [SwaggerSchema(Format = "date")] [FromQuery] string date + ) { var parsedDate = TryParseDate(date); if (!parsedDate.HasValue || parsedDate.Value.Year < 2000 || parsedDate.Value.Year > 3000) @@ -47,28 +43,38 @@ public class SolarLogController : ControllerBase [SwaggerParameter(Required = true)] [SwaggerSchema(Format = "date")] [FromQuery] - string start, - [SwaggerParameter(Required = true)] - [SwaggerSchema(Format = "date")] - [FromQuery] - string stop) + string start, + [SwaggerParameter(Required = true)] [SwaggerSchema(Format = "date")] [FromQuery] string stop + ) { var parsedStartDate = TryParseDate(start); - if (!parsedStartDate.HasValue || parsedStartDate.Value.Year < 2000 || parsedStartDate.Value.Year > 3000) + if ( + !parsedStartDate.HasValue + || parsedStartDate.Value.Year < 2000 + || parsedStartDate.Value.Year > 3000 + ) { logger.LogInformation("Invalid start date {Date} requested", start); return BadRequest(); } var parsedStopDate = TryParseDate(stop); - if (!parsedStopDate.HasValue || parsedStopDate.Value.Year < 2000 || parsedStopDate.Value.Year > 3000) + if ( + !parsedStopDate.HasValue + || parsedStopDate.Value.Year < 2000 + || parsedStopDate.Value.Year > 3000 + ) { logger.LogInformation("Invalid stop date {Date} requested", stop); return BadRequest(); } else if (parsedStopDate < parsedStartDate) { - logger.LogInformation("Stop date {StopDate} must come before start date {StartDate} requested", stop, start); + logger.LogInformation( + "Stop date {StopDate} must come before start date {StartDate} requested", + stop, + start + ); return BadRequest(); } @@ -81,28 +87,41 @@ public class SolarLogController : ControllerBase [SwaggerParameter(Required = true)] [SwaggerSchema(Format = "yyyy-MM")] [FromQuery] - string start, + string start, [SwaggerParameter(Required = true)] [SwaggerSchema(Format = "yyyy-MM")] [FromQuery] - string stop) + string stop + ) { var parsedStartDate = TryParseDate($"{start}-01"); - if (!parsedStartDate.HasValue || parsedStartDate.Value.Year < 2000 || parsedStartDate.Value.Year > 3000) + if ( + !parsedStartDate.HasValue + || parsedStartDate.Value.Year < 2000 + || parsedStartDate.Value.Year > 3000 + ) { logger.LogInformation("Invalid start year month {YearMonth} requested", start); return BadRequest(); } var parsedStopDate = TryParseDate($"{stop}-01"); - if (!parsedStopDate.HasValue || parsedStopDate.Value.Year < 2000 || parsedStopDate.Value.Year > 3000) + if ( + !parsedStopDate.HasValue + || parsedStopDate.Value.Year < 2000 + || parsedStopDate.Value.Year > 3000 + ) { logger.LogInformation("Invalid stop year month {YearMonth} requested", stop); return BadRequest(); } else if (parsedStopDate < parsedStartDate) { - logger.LogInformation("Stop year month {StopYearMonth} must come before start year month {StartYearMonth} requested", stop, start); + logger.LogInformation( + "Stop year month {StopYearMonth} must come before start year month {StartYearMonth} requested", + stop, + start + ); return BadRequest(); } @@ -110,7 +129,8 @@ public class SolarLogController : ControllerBase parsedStartDate.Value.Year, parsedStartDate.Value.Month, parsedStopDate.Value.Year, - parsedStopDate.Value.Month); + parsedStopDate.Value.Month + ); } private static DateOnly? TryParseDate(string value) diff --git a/src/Solar.Api/Converters/DateOnlyConverter.cs b/src/Solar.Api/Converters/DateOnlyConverter.cs index 660b427..9713aa7 100644 --- a/src/Solar.Api/Converters/DateOnlyConverter.cs +++ b/src/Solar.Api/Converters/DateOnlyConverter.cs @@ -10,7 +10,8 @@ public class DateOnlyConverter : JsonConverter public override DateOnly Read( ref Utf8JsonReader reader, Type typeToConvert, - JsonSerializerOptions options) + JsonSerializerOptions options + ) { var value = reader.GetString(); return DateOnly.ParseExact(value!, serializationFormat); @@ -19,6 +20,6 @@ public class DateOnlyConverter : JsonConverter public override void Write( Utf8JsonWriter writer, DateOnly value, - JsonSerializerOptions options) - => writer.WriteStringValue(value.ToString(serializationFormat)); -} \ No newline at end of file + JsonSerializerOptions options + ) => writer.WriteStringValue(value.ToString(serializationFormat)); +} diff --git a/src/Solar.Api/Converters/TimeOnlyConverter.cs b/src/Solar.Api/Converters/TimeOnlyConverter.cs index ca41d15..3afc2e2 100644 --- a/src/Solar.Api/Converters/TimeOnlyConverter.cs +++ b/src/Solar.Api/Converters/TimeOnlyConverter.cs @@ -10,7 +10,8 @@ public class TimeOnlyConverter : JsonConverter public override TimeOnly Read( ref Utf8JsonReader reader, Type typeToConvert, - JsonSerializerOptions options) + JsonSerializerOptions options + ) { var value = reader.GetString(); return TimeOnly.ParseExact(value!, serializationFormat); @@ -19,6 +20,6 @@ public class TimeOnlyConverter : JsonConverter public override void Write( Utf8JsonWriter writer, TimeOnly value, - JsonSerializerOptions options) - => writer.WriteStringValue(value.ToString(serializationFormat)); -} \ No newline at end of file + JsonSerializerOptions options + ) => writer.WriteStringValue(value.ToString(serializationFormat)); +} diff --git a/src/Solar.Api/DatabaseContext.cs b/src/Solar.Api/DatabaseContext.cs index a5e35b8..7c9e2bf 100644 --- a/src/Solar.Api/DatabaseContext.cs +++ b/src/Solar.Api/DatabaseContext.cs @@ -5,14 +5,10 @@ namespace Solar.Api { public partial class DatabaseContext : DbContext { - public DatabaseContext() - { - } + public DatabaseContext() { } public DatabaseContext(DbContextOptions options) - : base(options) - { - } + : base(options) { } public virtual DbSet EnvoyLogs { get; set; } = null!; public virtual DbSet ZeverLogs { get; set; } = null!; @@ -46,11 +42,9 @@ namespace Solar.Api { entity.ToTable("ZeverSummary"); - entity.HasIndex(e => e.Date, "IX_ZeverSummary_Date") - .IsUnique(); + entity.HasIndex(e => e.Date, "IX_ZeverSummary_Date").IsUnique(); - entity.HasIndex(e => e.Date, "idx_ZeverSummary_Date") - .IsUnique(); + entity.HasIndex(e => e.Date, "idx_ZeverSummary_Date").IsUnique(); entity.Property(e => e.TotalWatts).HasColumnType("BIGINT"); }); diff --git a/src/Solar.Api/Models/DayResponse.cs b/src/Solar.Api/Models/DayResponse.cs index 2703053..2ec248f 100644 --- a/src/Solar.Api/Models/DayResponse.cs +++ b/src/Solar.Api/Models/DayResponse.cs @@ -1,4 +1,3 @@ - namespace Solar.Api.Models; -public record DayResponse(ZeverDayLog[] ZeverLogs, EnvoyDayLog[] EnvoyLogs); \ No newline at end of file +public record DayResponse(ZeverDayLog[] ZeverLogs, EnvoyDayLog[] EnvoyLogs); diff --git a/src/Solar.Api/Models/DaySummaryLog.cs b/src/Solar.Api/Models/DaySummaryLog.cs index a09c350..267b24c 100644 --- a/src/Solar.Api/Models/DaySummaryLog.cs +++ b/src/Solar.Api/Models/DaySummaryLog.cs @@ -1,3 +1,3 @@ namespace Solar.Api.Models; -public record DaySummaryLog(DateOnly Date, int ZeverTotalWatts, int EnvoyTotalWatts); \ No newline at end of file +public record DaySummaryLog(DateOnly Date, int ZeverTotalWatts, int EnvoyTotalWatts); diff --git a/src/Solar.Api/Models/DaysResponse.cs b/src/Solar.Api/Models/DaysResponse.cs index 0cfc3e8..186f37e 100644 --- a/src/Solar.Api/Models/DaysResponse.cs +++ b/src/Solar.Api/Models/DaysResponse.cs @@ -1,3 +1,3 @@ namespace Solar.Api.Models; -public record DaysResponse(DaySummaryLog[] DayLogs); \ No newline at end of file +public record DaysResponse(DaySummaryLog[] DayLogs); diff --git a/src/Solar.Api/Models/EnvoyDayLog.cs b/src/Solar.Api/Models/EnvoyDayLog.cs index c32fd28..889d1ca 100644 --- a/src/Solar.Api/Models/EnvoyDayLog.cs +++ b/src/Solar.Api/Models/EnvoyDayLog.cs @@ -1,3 +1,3 @@ namespace Solar.Api.Models; -public record EnvoyDayLog(TimeOnly TimeUtc, int CurrentWatts, int TotalWatts); \ No newline at end of file +public record EnvoyDayLog(TimeOnly TimeUtc, int CurrentWatts, int TotalWatts); diff --git a/src/Solar.Api/Models/MonthLog.cs b/src/Solar.Api/Models/MonthLog.cs index 1aae764..a89aafc 100644 --- a/src/Solar.Api/Models/MonthLog.cs +++ b/src/Solar.Api/Models/MonthLog.cs @@ -1,3 +1,3 @@ namespace Solar.Api.Models; -public record MonthLog(int Year, int Month, int ZeverTotalWatts, int EnvoyTotalWatts); \ No newline at end of file +public record MonthLog(int Year, int Month, int ZeverTotalWatts, int EnvoyTotalWatts); diff --git a/src/Solar.Api/Models/MonthSummariesResponse.cs b/src/Solar.Api/Models/MonthSummariesResponse.cs index de2ad46..34f6025 100644 --- a/src/Solar.Api/Models/MonthSummariesResponse.cs +++ b/src/Solar.Api/Models/MonthSummariesResponse.cs @@ -1,3 +1,3 @@ namespace Solar.Api.Models; -public record MonthSummariesResponse(MonthLog[] MonthLogs); \ No newline at end of file +public record MonthSummariesResponse(MonthLog[] MonthLogs); diff --git a/src/Solar.Api/Models/ZeverDayLog.cs b/src/Solar.Api/Models/ZeverDayLog.cs index f18ba28..e05d7e1 100644 --- a/src/Solar.Api/Models/ZeverDayLog.cs +++ b/src/Solar.Api/Models/ZeverDayLog.cs @@ -1,3 +1,3 @@ namespace Solar.Api.Models; -public record ZeverDayLog(TimeOnly TimeUtc, int CurrentWatts, int TotalWatts); \ No newline at end of file +public record ZeverDayLog(TimeOnly TimeUtc, int CurrentWatts, int TotalWatts); diff --git a/src/Solar.Api/Program.cs b/src/Solar.Api/Program.cs index 4bb84b9..a17ca3c 100644 --- a/src/Solar.Api/Program.cs +++ b/src/Solar.Api/Program.cs @@ -1,9 +1,9 @@ +using System.Text.Json; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; using Solar.Api.Converters; using Solar.Api.Services; using Swashbuckle.AspNetCore.SwaggerGen; -using System.Text.Json; namespace Solar.Api; @@ -31,31 +31,38 @@ public partial class Program policy => { policy.WithOrigins("http://localhost:8080"); - }); + } + ); }); - builder.Services.AddControllers().AddJsonOptions(config => - { - config.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; - config.JsonSerializerOptions.PropertyNameCaseInsensitive = true; - config.JsonSerializerOptions.Converters.Add(new DateOnlyConverter()); - config.JsonSerializerOptions.Converters.Add(new TimeOnlyConverter()); - }); + builder + .Services.AddControllers() + .AddJsonOptions(config => + { + config.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + config.JsonSerializerOptions.PropertyNameCaseInsensitive = true; + config.JsonSerializerOptions.Converters.Add(new DateOnlyConverter()); + config.JsonSerializerOptions.Converters.Add(new TimeOnlyConverter()); + }); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(config => { - config.MapType(() => new OpenApiSchema - { - Type = "string", - Format = "date", - Example = OpenApiAnyFactory.CreateFromJson("\"2022-12-31\"") - }); - config.MapType(() => new OpenApiSchema - { - Type = "string", - Format = "time", - Example = OpenApiAnyFactory.CreateFromJson("\"13:45:42.0000000\"") - }); + config.MapType(() => + new OpenApiSchema + { + Type = "string", + Format = "date", + Example = OpenApiAnyFactory.CreateFromJson("\"2022-12-31\""), + } + ); + config.MapType(() => + new OpenApiSchema + { + Type = "string", + Format = "time", + Example = OpenApiAnyFactory.CreateFromJson("\"13:45:42.0000000\""), + } + ); config.SupportNonNullableReferenceTypes(); config.EnableAnnotations(); diff --git a/src/Solar.Api/Services/SolarService.cs b/src/Solar.Api/Services/SolarService.cs index 74bc3c0..fa9f5e5 100644 --- a/src/Solar.Api/Services/SolarService.cs +++ b/src/Solar.Api/Services/SolarService.cs @@ -15,7 +15,8 @@ public class SolarService public SolarService( DatabaseContext databaseContext, IMemoryCache memoryCache, - ILogger logger) + ILogger logger + ) { this.databaseContext = databaseContext; this.memoryCache = memoryCache; @@ -26,13 +27,11 @@ public class SolarService public async Task GetDayDetails(DateOnly date) { var zeverRecords = await databaseContext - .ZeverLogs - .Where(zl => zl.Date == date) + .ZeverLogs.Where(zl => zl.Date == date) .OrderBy(zl => zl.TimeUtc) .ToArrayAsync(); var envoyRecords = await databaseContext - .EnvoyLogs - .Where(er => er.Date == date) + .EnvoyLogs.Where(er => er.Date == date) .OrderBy(er => er.TimeUtc) .ToArrayAsync(); @@ -40,17 +39,12 @@ public class SolarService return new DayResponse( zeverRecords - .Select(zr => new ZeverDayLog( - zr.TimeUtc, - (int)zr.CurrentWatts, - (int)zr.TotalWatts)) + .Select(zr => new ZeverDayLog(zr.TimeUtc, (int)zr.CurrentWatts, (int)zr.TotalWatts)) .ToArray(), envoyRecords - .Select(er => new EnvoyDayLog( - er.TimeUtc, - (int)er.CurrentWatts, - (int)er.TotalWatts)) - .ToArray()); + .Select(er => new EnvoyDayLog(er.TimeUtc, (int)er.CurrentWatts, (int)er.TotalWatts)) + .ToArray() + ); } private static void NormalizeEnvoyLogs(EnvoyLog[] entities) @@ -70,21 +64,18 @@ public class SolarService public async Task GetDaySummaries(DateOnly start, DateOnly stop) { var zeverRecords = await databaseContext - .ZeverSummaries - .Where(zl => zl.Date >= start && zl.Date <= stop) + .ZeverSummaries.Where(zl => zl.Date >= start && zl.Date <= stop) .ToArrayAsync(); List logs = new(); var current = start; do { - var zeverResult = zeverRecords.FirstOrDefault(zr => zr.Date == current)?.TotalWatts ?? 0; + var zeverResult = + zeverRecords.FirstOrDefault(zr => zr.Date == current)?.TotalWatts ?? 0; var envoyResult = await GetEnvoyDayTotalWatts(current); - logs.Add(new DaySummaryLog( - current, - (int)zeverResult, - envoyResult)); + logs.Add(new DaySummaryLog(current, (int)zeverResult, envoyResult)); current = current.AddDays(1); } while (current <= stop); @@ -94,12 +85,12 @@ public class SolarService private async Task GetEnvoyDayTotalWatts(DateOnly date) { - var min = await databaseContext.EnvoyLogs - .Where(el => el.Date == date) + var min = await databaseContext + .EnvoyLogs.Where(el => el.Date == date) .OrderBy(el => el.TotalWatts) .FirstOrDefaultAsync(); - var max = await databaseContext.EnvoyLogs - .Where(el => el.Date == date) + var max = await databaseContext + .EnvoyLogs.Where(el => el.Date == date) .OrderByDescending(el => el.TotalWatts) .FirstOrDefaultAsync(); @@ -111,14 +102,29 @@ public class SolarService return (int)(max.TotalWatts - min.TotalWatts); } - public async Task GetMonthSummaries(int fromYear, int fromMonth, int toYear, int toMonth) + public async Task GetMonthSummaries( + int fromYear, + int fromMonth, + int toYear, + int toMonth + ) { var results = new List(); var current = (Year: fromYear, Month: fromMonth); do { - var zeverResult = await GetOrAddToCache("zever", current.Year, current.Month, GetZeverMonthTotalWatts); - var envoyYear = await GetOrAddToCache("envoy", current.Year, current.Month, GetEnvoyMonthTotalWatts); + var zeverResult = await GetOrAddToCache( + "zever", + current.Year, + current.Month, + GetZeverMonthTotalWatts + ); + var envoyYear = await GetOrAddToCache( + "envoy", + current.Year, + current.Month, + GetEnvoyMonthTotalWatts + ); results.Add(new MonthLog(current.Year, current.Month, zeverResult, envoyYear)); if (current.Month == 12) @@ -135,19 +141,20 @@ public class SolarService private async Task GetZeverMonthTotalWatts(int year, int month) { - return (int)await databaseContext.ZeverSummaries - .Where(zs => zs.Date.Year == year && zs.Date.Month == month) - .SumAsync(zs => zs.TotalWatts); + return (int) + await databaseContext + .ZeverSummaries.Where(zs => zs.Date.Year == year && zs.Date.Month == month) + .SumAsync(zs => zs.TotalWatts); } private async Task GetEnvoyMonthTotalWatts(int year, int month) { - var min = await databaseContext.EnvoyLogs - .Where(el => el.Date.Year == year && el.Date.Month == month) + var min = await databaseContext + .EnvoyLogs.Where(el => el.Date.Year == year && el.Date.Month == month) .OrderBy(el => el.TotalWatts) .FirstOrDefaultAsync(); - var max = await databaseContext.EnvoyLogs - .Where(el => el.Date.Year == year && el.Date.Month == month) + var max = await databaseContext + .EnvoyLogs.Where(el => el.Date.Year == year && el.Date.Month == month) .OrderByDescending(el => el.TotalWatts) .FirstOrDefaultAsync(); @@ -163,7 +170,8 @@ public class SolarService string source, int year, int month, - Func> fetchMethod) + Func> fetchMethod + ) { return await memoryCache.GetOrCreateAsync( CacheKeys.GetMonthSummaryCacheKey(source, year, month), @@ -174,6 +182,7 @@ public class SolarService cacheEntry.SlidingExpiration = TimeSpan.FromHours(12); } return await fetchMethod(year, month); - }); + } + ); } -} \ No newline at end of file +}