Add database and dispenser logs
This commit is contained in:
parent
e024e0dfde
commit
85c7732ac9
1
backend/.gitignore
vendored
1
backend/.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
obj/
|
obj/
|
||||||
bin/
|
bin/
|
||||||
appsettings.json
|
appsettings.json
|
||||||
|
db.sqlite3
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace backend.Application
|
namespace backend.Application
|
||||||
{
|
{
|
||||||
public static class ApplicationState
|
public static class ApplicationState
|
||||||
{
|
{
|
||||||
public static IMqttClient? MqttClient { get; set; }
|
public static IMqttClient? MqttClient { get; set; }
|
||||||
|
public static DbContext? DbContext { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using MQTTnet;
|
using MQTTnet;
|
||||||
using backend.Application;
|
using backend.Application;
|
||||||
|
using backend.Models;
|
||||||
|
|
||||||
namespace backend.Controllers;
|
namespace backend.Controllers;
|
||||||
|
|
||||||
@ -17,6 +18,9 @@ public class DispenserController : ControllerBase
|
|||||||
.WithPayload("dispense")
|
.WithPayload("dispense")
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
|
ApplicationState.DbContext!.Add(new DispenserLog { Timestamp = DateTime.Now });
|
||||||
|
ApplicationState.DbContext!.SaveChanges();
|
||||||
|
|
||||||
ApplicationState.MqttClient!.PublishAsync(message, CancellationToken.None);
|
ApplicationState.MqttClient!.PublishAsync(message, CancellationToken.None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
17
backend/DispenserContext.cs
Normal file
17
backend/DispenserContext.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
38
backend/Migrations/20231205223426_CreateDispenserLog.Designer.cs
generated
Normal file
38
backend/Migrations/20231205223426_CreateDispenserLog.Designer.cs
generated
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
backend/Migrations/20231205223426_CreateDispenserLog.cs
Normal file
35
backend/Migrations/20231205223426_CreateDispenserLog.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
backend/Migrations/DispenserContextModelSnapshot.cs
Normal file
35
backend/Migrations/DispenserContextModelSnapshot.cs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
backend/Models/DispenserLog.cs
Normal file
17
backend/Models/DispenserLog.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
|
@ -45,6 +45,7 @@ var options = new MqttClientOptionsBuilder()
|
|||||||
await mqttClient.ConnectAsync(options, CancellationToken.None);
|
await mqttClient.ConnectAsync(options, CancellationToken.None);
|
||||||
|
|
||||||
ApplicationState.MqttClient = mqttClient;
|
ApplicationState.MqttClient = mqttClient;
|
||||||
|
ApplicationState.DbContext = new DispenserContext();
|
||||||
|
|
||||||
Console.WriteLine("Connected");
|
Console.WriteLine("Connected");
|
||||||
|
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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="MQTTnet" Version="4.3.1.873" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user