mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-14 01:25:38 -05:00
Initial setup of environment service
This commit is contained in:
51
Environment/Service/Data/Database.cs
Normal file
51
Environment/Service/Data/Database.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System.Reflection;
|
||||
using Dapper;
|
||||
using DbUp;
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
namespace ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
||||
|
||||
public class Database(IConfiguration configuration)
|
||||
{
|
||||
private string GetConnectionString()
|
||||
{
|
||||
var connectionStringBuilder = new SqlConnectionStringBuilder
|
||||
{
|
||||
DataSource = configuration["Environment:Database:Host"],
|
||||
UserID = configuration["Environment:Database:User"],
|
||||
Password = configuration["Environment:Database:Password"],
|
||||
InitialCatalog = configuration["Environment:Database:Name"],
|
||||
TrustServerCertificate = bool.Parse(configuration["Environment:Database:TrustServerCertificate"] ?? "false")
|
||||
};
|
||||
|
||||
return connectionStringBuilder.ConnectionString;
|
||||
}
|
||||
|
||||
public void EnsureDatabase()
|
||||
{
|
||||
var connectionString = GetConnectionString();
|
||||
|
||||
DbUp.EnsureDatabase.For.SqlDatabase(connectionString);
|
||||
|
||||
var upgradeEngine = DeployChanges.To.SqlDatabase(connectionString).WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(), s => s.Contains(".Schema.")).LogToConsole().Build();
|
||||
|
||||
upgradeEngine.PerformUpgrade();
|
||||
}
|
||||
|
||||
private SqlConnection CreateConnection()
|
||||
{
|
||||
var connection = new SqlConnection(GetConnectionString());
|
||||
connection.Open();
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
public async Task StoreMessageAsync(Message message)
|
||||
{
|
||||
await using var connection = CreateConnection();
|
||||
|
||||
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.CreateReading.sql");
|
||||
|
||||
await connection.QueryAsync(query, message);
|
||||
}
|
||||
}
|
||||
25
Environment/Service/Data/Queries/CreateReading.sql
Normal file
25
Environment/Service/Data/Queries/CreateReading.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
BEGIN TRANSACTION
|
||||
|
||||
INSERT Reading
|
||||
(Timestamp, Name, Model, Temperature, Pressure, Humidity, Luminance, GasResistance, ColorTemperature, AirQualityIndex)
|
||||
SELECT
|
||||
@Timestamp,
|
||||
@Name,
|
||||
@Model,
|
||||
@Temperature,
|
||||
@Pressure,
|
||||
@Humidity,
|
||||
@Luminance,
|
||||
@GasResistance,
|
||||
@ColorTemperature,
|
||||
@AirQualityIndex
|
||||
WHERE NOT EXISTS
|
||||
(
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
Reading WITH (UPDLOCK, SERIALIZABLE)
|
||||
WHERE Timestamp = @Timestamp AND Name = @Name AND Model = @Model
|
||||
)
|
||||
|
||||
COMMIT TRANSACTION
|
||||
14
Environment/Service/Data/Schema/1-Initial Schema.sql
Normal file
14
Environment/Service/Data/Schema/1-Initial Schema.sql
Normal file
@@ -0,0 +1,14 @@
|
||||
CREATE TABLE Reading
|
||||
(
|
||||
Timestamp datetimeoffset NOT NULL,
|
||||
Name nvarchar(50) NOT NULL,
|
||||
Model nvarchar(50) NOT NULL,
|
||||
Temperature decimal(5, 2) NOT NULL,
|
||||
Pressure decimal(6, 2) NOT NULL,
|
||||
Humidity decimal(5, 2) NOT NULL,
|
||||
Luminance int NOT NULL,
|
||||
GasResistance int NOT NULL,
|
||||
ColorTemperature int NOT NULL,
|
||||
AirQualityIndex decimal(4, 1) NOT NULL,
|
||||
CONSTRAINT reading_pk PRIMARY KEY (Timestamp, Name, Model)
|
||||
);
|
||||
Reference in New Issue
Block a user