Add UserController, Add User model

This commit is contained in:
Alexandertp 2023-12-06 14:11:11 +01:00
parent bacffd90b3
commit c55a5c1b36
9 changed files with 153 additions and 6 deletions

2
backend/.gitignore vendored
View File

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

View File

@ -24,4 +24,3 @@ public class DispenserController : ControllerBase
ApplicationState.MqttClient!.PublishAsync(message, CancellationToken.None);
}
}

View File

@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using backend.Application;
using backend.Models;
namespace backend.Controllers;
[ApiController]
public class UserController : ControllerBase
{
[HttpPost("Register")]
public void Register()
{
ApplicationState.DbContext!.Add(new User { Username = "test", Password = "test", TouchCode = "1234" });
ApplicationState.DbContext!.SaveChanges();
}
}

View File

@ -8,6 +8,7 @@ using backend.Models;
public class DispenserContext : DbContext
{
public DbSet<DispenserLog> DispenserLogs { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{

View File

@ -0,0 +1,61 @@
// <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("20231206130439_CreateUser")]
partial class CreateUser
{
/// <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");
});
modelBuilder.Entity("backend.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("TouchCode")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace backend.Migrations
{
/// <inheritdoc />
public partial class CreateUser : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Username = table.Column<string>(type: "TEXT", nullable: false),
Password = table.Column<string>(type: "TEXT", nullable: false),
TouchCode = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
}

View File

@ -29,6 +29,29 @@ namespace backend.Migrations
b.ToTable("DispenserLogs");
});
modelBuilder.Entity("backend.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("TouchCode")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}

View File

@ -1,6 +1,3 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -14,4 +11,3 @@ public class DispenserLog
public DateTime Timestamp { get; set; }
}

15
backend/Models/User.cs Normal file
View File

@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace backend.Models;
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string TouchCode { get; set; }
}