Add database and dispenser logs

This commit is contained in:
ReiMerc 2023-12-05 23:43:02 +01:00
parent e024e0dfde
commit 85c7732ac9
10 changed files with 157 additions and 2 deletions

1
backend/.gitignore vendored
View File

@ -1,4 +1,5 @@
obj/
bin/
appsettings.json
db.sqlite3

View File

@ -1,9 +1,11 @@
using MQTTnet.Client;
using Microsoft.EntityFrameworkCore;
namespace backend.Application
{
public static class ApplicationState
{
public static IMqttClient? MqttClient { get; set; }
public static DbContext? DbContext { get; set; }
}
}

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using backend.Application;
using backend.Models;
namespace backend.Controllers;
@ -17,6 +18,9 @@ public class DispenserController : ControllerBase
.WithPayload("dispense")
.Build();
ApplicationState.DbContext!.Add(new DispenserLog { Timestamp = DateTime.Now });
ApplicationState.DbContext!.SaveChanges();
ApplicationState.MqttClient!.PublishAsync(message, CancellationToken.None);
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using backend.Models;
public class DispenserContext : DbContext
{
public DbSet<DispenserLog> DispenserLogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlite("DataSource=db.sqlite3");
}
}

View File

@ -0,0 +1,38 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace backend.Migrations
{
[DbContext(typeof(DispenserContext))]
[Migration("20231205223426_CreateDispenserLog")]
partial class CreateDispenserLog
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.0");
modelBuilder.Entity("backend.Models.DispenserLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("Timestamp")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("DispenserLogs");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,35 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace backend.Migrations
{
/// <inheritdoc />
public partial class CreateDispenserLog : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "DispenserLogs",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Timestamp = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_DispenserLogs", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "DispenserLogs");
}
}
}

View File

@ -0,0 +1,35 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace backend.Migrations
{
[DbContext(typeof(DispenserContext))]
partial class DispenserContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.0");
modelBuilder.Entity("backend.Models.DispenserLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("Timestamp")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("DispenserLogs");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace backend.Models;
public class DispenserLog
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime Timestamp { get; set; }
}

View File

@ -45,6 +45,7 @@ var options = new MqttClientOptionsBuilder()
await mqttClient.ConnectAsync(options, CancellationToken.None);
ApplicationState.MqttClient = mqttClient;
ApplicationState.DbContext = new DispenserContext();
Console.WriteLine("Connected");

View File

@ -1,13 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.13" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="MQTTnet" Version="4.3.1.873" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>