Add initial history API

This commit is contained in:
2019-08-08 21:03:20 -04:00
parent f4da45f4bb
commit 5a0e1b043f
5 changed files with 55 additions and 2 deletions

View File

@@ -1,6 +1,9 @@
using ChrisKaczor.HomeMonitor.Weather.Models; using System;
using ChrisKaczor.HomeMonitor.Weather.Models;
using ChrisKaczor.HomeMonitor.Weather.Service.Data; using ChrisKaczor.HomeMonitor.Weather.Service.Data;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers
@@ -21,5 +24,11 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers
{ {
return await _database.GetRecentReading(); return await _database.GetRecentReading();
} }
[HttpGet("history")]
public async Task<ActionResult<List<WeatherReading>>> GetHistory(DateTimeOffset start, DateTimeOffset end)
{
return (await _database.GetReadingHistory(start, end)).ToList();
}
} }
} }

View File

@@ -1,6 +1,8 @@
using ChrisKaczor.HomeMonitor.Weather.Models; using ChrisKaczor.HomeMonitor.Weather.Models;
using Dapper; using Dapper;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -85,5 +87,14 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
return await connection.QueryFirstOrDefaultAsync<WeatherReading>(query); return await connection.QueryFirstOrDefaultAsync<WeatherReading>(query);
} }
public async Task<IEnumerable<WeatherReading>> 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<WeatherReading>(query, new { Start = start, End = end });
}
} }
} }

View File

@@ -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

View File

@@ -10,11 +10,13 @@
<ItemGroup> <ItemGroup>
<None Remove="Data\Resources\CreateReading.sql" /> <None Remove="Data\Resources\CreateReading.sql" />
<None Remove="Data\Resources\GetReadingHistory.sql" />
<None Remove="Data\Resources\GetRecentReading.sql" /> <None Remove="Data\Resources\GetRecentReading.sql" />
<None Remove="Data\Resources\Schema.sql" /> <None Remove="Data\Resources\Schema.sql" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Data\Resources\GetReadingHistory.sql" />
<EmbeddedResource Include="Data\Resources\GetRecentReading.sql" /> <EmbeddedResource Include="Data\Resources\GetRecentReading.sql" />
<EmbeddedResource Include="Data\Resources\CreateReading.sql" /> <EmbeddedResource Include="Data\Resources\CreateReading.sql" />
<EmbeddedResource Include="Data\Resources\Schema.sql" /> <EmbeddedResource Include="Data\Resources\Schema.sql" />

View File

@@ -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.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@@ -15,6 +17,15 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
services.AddHostedService<MessageHandler>(); services.AddHostedService<MessageHandler>();
services.Configure<GzipCompressionProviderOptions>(options => {
options.Level = CompressionLevel.Optimal;
});
services.AddResponseCompression(options => {
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
} }
@@ -26,6 +37,8 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
var database = applicationBuilder.ApplicationServices.GetService<Database>(); var database = applicationBuilder.ApplicationServices.GetService<Database>();
database.EnsureDatabase(); database.EnsureDatabase();
applicationBuilder.UseResponseCompression();
applicationBuilder.UseRouting(); applicationBuilder.UseRouting();
applicationBuilder.UseEndpoints(endpoints => applicationBuilder.UseEndpoints(endpoints =>