From 85c7732ac91d5c554d7d0f0cca3cd0fcd098b512 Mon Sep 17 00:00:00 2001 From: ReiMerc Date: Tue, 5 Dec 2023 23:43:02 +0100 Subject: [PATCH] Add database and dispenser logs --- backend/.gitignore | 1 + backend/ApplicationState.cs | 2 + backend/Controllers/DispenserController.cs | 4 ++ backend/DispenserContext.cs | 17 +++++++++ ...31205223426_CreateDispenserLog.Designer.cs | 38 +++++++++++++++++++ .../20231205223426_CreateDispenserLog.cs | 35 +++++++++++++++++ .../DispenserContextModelSnapshot.cs | 35 +++++++++++++++++ backend/Models/DispenserLog.cs | 17 +++++++++ backend/Program.cs | 1 + backend/backend.csproj | 9 ++++- 10 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 backend/DispenserContext.cs create mode 100644 backend/Migrations/20231205223426_CreateDispenserLog.Designer.cs create mode 100644 backend/Migrations/20231205223426_CreateDispenserLog.cs create mode 100644 backend/Migrations/DispenserContextModelSnapshot.cs create mode 100644 backend/Models/DispenserLog.cs diff --git a/backend/.gitignore b/backend/.gitignore index 6b99a71..20c38ef 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,4 +1,5 @@ obj/ bin/ appsettings.json +db.sqlite3 diff --git a/backend/ApplicationState.cs b/backend/ApplicationState.cs index df2c7b1..7371687 100644 --- a/backend/ApplicationState.cs +++ b/backend/ApplicationState.cs @@ -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; } } } diff --git a/backend/Controllers/DispenserController.cs b/backend/Controllers/DispenserController.cs index bb9fe7f..ffa5ca8 100644 --- a/backend/Controllers/DispenserController.cs +++ b/backend/Controllers/DispenserController.cs @@ -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); } } diff --git a/backend/DispenserContext.cs b/backend/DispenserContext.cs new file mode 100644 index 0000000..d99ef29 --- /dev/null +++ b/backend/DispenserContext.cs @@ -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 DispenserLogs { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + options.UseSqlite("DataSource=db.sqlite3"); + } +} + diff --git a/backend/Migrations/20231205223426_CreateDispenserLog.Designer.cs b/backend/Migrations/20231205223426_CreateDispenserLog.Designer.cs new file mode 100644 index 0000000..5d08cd3 --- /dev/null +++ b/backend/Migrations/20231205223426_CreateDispenserLog.Designer.cs @@ -0,0 +1,38 @@ +// +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 + { + /// + 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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Timestamp") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("DispenserLogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/Migrations/20231205223426_CreateDispenserLog.cs b/backend/Migrations/20231205223426_CreateDispenserLog.cs new file mode 100644 index 0000000..7a2614b --- /dev/null +++ b/backend/Migrations/20231205223426_CreateDispenserLog.cs @@ -0,0 +1,35 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace backend.Migrations +{ + /// + public partial class CreateDispenserLog : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "DispenserLogs", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Timestamp = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DispenserLogs", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DispenserLogs"); + } + } +} diff --git a/backend/Migrations/DispenserContextModelSnapshot.cs b/backend/Migrations/DispenserContextModelSnapshot.cs new file mode 100644 index 0000000..c49025e --- /dev/null +++ b/backend/Migrations/DispenserContextModelSnapshot.cs @@ -0,0 +1,35 @@ +// +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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Timestamp") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("DispenserLogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/Models/DispenserLog.cs b/backend/Models/DispenserLog.cs new file mode 100644 index 0000000..4a146cb --- /dev/null +++ b/backend/Models/DispenserLog.cs @@ -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; } +} + diff --git a/backend/Program.cs b/backend/Program.cs index 595d7ce..a18403d 100644 --- a/backend/Program.cs +++ b/backend/Program.cs @@ -45,6 +45,7 @@ var options = new MqttClientOptionsBuilder() await mqttClient.ConnectAsync(options, CancellationToken.None); ApplicationState.MqttClient = mqttClient; +ApplicationState.DbContext = new DispenserContext(); Console.WriteLine("Connected"); diff --git a/backend/backend.csproj b/backend/backend.csproj index 2bf87cc..1e0e07b 100644 --- a/backend/backend.csproj +++ b/backend/backend.csproj @@ -1,13 +1,18 @@ - net7.0 + net8.0 enable enable - + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + +