From 5a0e1b043f9df9ec57d8e4e4849068eca6ea64f7 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Thu, 8 Aug 2019 21:03:20 -0400 Subject: [PATCH] Add initial history API --- .../Service/Controllers/ReadingsController.cs | 11 ++++++++++- Weather/Service/Data/Database.cs | 11 +++++++++++ .../Data/Resources/GetReadingHistory.sql | 18 ++++++++++++++++++ Weather/Service/Service.csproj | 2 ++ Weather/Service/Startup.cs | 15 ++++++++++++++- 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 Weather/Service/Data/Resources/GetReadingHistory.sql diff --git a/Weather/Service/Controllers/ReadingsController.cs b/Weather/Service/Controllers/ReadingsController.cs index d1d240c..26b7659 100644 --- a/Weather/Service/Controllers/ReadingsController.cs +++ b/Weather/Service/Controllers/ReadingsController.cs @@ -1,6 +1,9 @@ -using ChrisKaczor.HomeMonitor.Weather.Models; +using System; +using ChrisKaczor.HomeMonitor.Weather.Models; using ChrisKaczor.HomeMonitor.Weather.Service.Data; using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers @@ -21,5 +24,11 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers { return await _database.GetRecentReading(); } + + [HttpGet("history")] + public async Task>> GetHistory(DateTimeOffset start, DateTimeOffset end) + { + return (await _database.GetReadingHistory(start, end)).ToList(); + } } } \ No newline at end of file diff --git a/Weather/Service/Data/Database.cs b/Weather/Service/Data/Database.cs index 0c26ac7..f393dc5 100644 --- a/Weather/Service/Data/Database.cs +++ b/Weather/Service/Data/Database.cs @@ -1,6 +1,8 @@ using ChrisKaczor.HomeMonitor.Weather.Models; using Dapper; using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; using System.Data.SqlClient; using System.Threading.Tasks; @@ -85,5 +87,14 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data return await connection.QueryFirstOrDefaultAsync(query); } + + public async Task> GetReadingHistory(DateTimeOffset start, DateTimeOffset end) + { + await using var connection = CreateConnection(); + + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingHistory.sql"); + + return await connection.QueryAsync(query, new { Start = start, End = end }); + } } } \ No newline at end of file diff --git a/Weather/Service/Data/Resources/GetReadingHistory.sql b/Weather/Service/Data/Resources/GetReadingHistory.sql new file mode 100644 index 0000000..fd0abe4 --- /dev/null +++ b/Weather/Service/Data/Resources/GetReadingHistory.sql @@ -0,0 +1,18 @@ +SELECT Timestamp, + WindDirection, + WindSpeed, + Humidity, + HumidityTemperature, + Rain, + Pressure, + PressureTemperature, + BatteryLevel, + LightLevel, + Latitude, + Longitude, + Altitude, + SatelliteCount, + GpsTimestamp +FROM Reading +WHERE Timestamp BETWEEN @Start AND @End +ORDER BY Timestamp ASC \ No newline at end of file diff --git a/Weather/Service/Service.csproj b/Weather/Service/Service.csproj index 17c807f..200219d 100644 --- a/Weather/Service/Service.csproj +++ b/Weather/Service/Service.csproj @@ -10,11 +10,13 @@ + + diff --git a/Weather/Service/Startup.cs b/Weather/Service/Startup.cs index 2382219..fc91bba 100644 --- a/Weather/Service/Startup.cs +++ b/Weather/Service/Startup.cs @@ -1,7 +1,9 @@ -using ChrisKaczor.HomeMonitor.Weather.Service.Data; +using System.IO.Compression; +using ChrisKaczor.HomeMonitor.Weather.Service.Data; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.ResponseCompression; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -15,6 +17,15 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service services.AddHostedService(); + services.Configure(options => { + options.Level = CompressionLevel.Optimal; + }); + + services.AddResponseCompression(options => { + options.Providers.Add(); + options.EnableForHttps = true; + }); + services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); } @@ -26,6 +37,8 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service var database = applicationBuilder.ApplicationServices.GetService(); database.EnsureDatabase(); + applicationBuilder.UseResponseCompression(); + applicationBuilder.UseRouting(); applicationBuilder.UseEndpoints(endpoints =>