From 33c3a074dab97188b6efe8da4f6f30c0003f61d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=A5=D0=B0=D1=80=D0=BB?= =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B2?= Date: Tue, 29 Oct 2024 00:38:46 +0400 Subject: [PATCH] Make Farm CRUD --- Cloud/ApplicationContext.cs | 3 +- ...0241027220558_CreateUsersTable.Designer.cs | 60 +++++++++---------- .../20241027220558_CreateUsersTable.cs | 50 ++++++++-------- .../ApplicationContextModelSnapshot.cs | 42 +++++++++++++ Cloud/Models/User.cs | 2 + Cloud/Program.cs | 1 + 6 files changed, 102 insertions(+), 56 deletions(-) diff --git a/Cloud/ApplicationContext.cs b/Cloud/ApplicationContext.cs index e91cc00..6a60cf1 100644 --- a/Cloud/ApplicationContext.cs +++ b/Cloud/ApplicationContext.cs @@ -4,7 +4,8 @@ using Microsoft.EntityFrameworkCore; namespace Cloud; public class ApplicationContext : DbContext { - public DbSet Users { get; set; } + public DbSet Users { get; set; } = null!; + public DbSet Farms { get; set; } = null!; public ApplicationContext(DbContextOptions options) : base(options) diff --git a/Cloud/Migrations/20241027220558_CreateUsersTable.Designer.cs b/Cloud/Migrations/20241027220558_CreateUsersTable.Designer.cs index b021b5d..d1c7360 100644 --- a/Cloud/Migrations/20241027220558_CreateUsersTable.Designer.cs +++ b/Cloud/Migrations/20241027220558_CreateUsersTable.Designer.cs @@ -10,44 +10,44 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace Cloud.Migrations { - [DbContext(typeof(ApplicationContext))] - [Migration("20241027220558_CreateUsersTable")] - partial class CreateUsersTable - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { + [DbContext(typeof(ApplicationContext))] + [Migration("20241027220558_CreateUsersTable")] + partial class CreateUsersTable + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { #pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "6.0.14") - .HasAnnotation("Relational:MaxIdentifierLength", 63); + modelBuilder + .HasAnnotation("ProductVersion", "6.0.14") + .HasAnnotation("Relational:MaxIdentifierLength", 63); - NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - modelBuilder.Entity("Cloud.Models.User", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); + modelBuilder.Entity("Cloud.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - b.Property("Email") - .IsRequired() - .HasColumnType("text"); + b.Property("Email") + .IsRequired() + .HasColumnType("text"); - b.Property("Name") - .IsRequired() - .HasColumnType("text"); + b.Property("Name") + .IsRequired() + .HasColumnType("text"); - b.Property("Password") - .IsRequired() - .HasColumnType("text"); + b.Property("Password") + .IsRequired() + .HasColumnType("text"); - b.HasKey("Id"); + b.HasKey("Id"); - b.ToTable("Users"); - }); + b.ToTable("Users"); + }); #pragma warning restore 612, 618 - } - } + } + } } diff --git a/Cloud/Migrations/20241027220558_CreateUsersTable.cs b/Cloud/Migrations/20241027220558_CreateUsersTable.cs index 65e5500..4c9a4dd 100644 --- a/Cloud/Migrations/20241027220558_CreateUsersTable.cs +++ b/Cloud/Migrations/20241027220558_CreateUsersTable.cs @@ -5,30 +5,30 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace Cloud.Migrations { - public partial class CreateUsersTable : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Users", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - Name = table.Column(type: "text", nullable: false), - Email = table.Column(type: "text", nullable: false), - Password = table.Column(type: "text", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Users", x => x.Id); - }); - } + public partial class CreateUsersTable : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false), + Email = table.Column(type: "text", nullable: false), + Password = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + } - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Users"); - } - } + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + } + } } diff --git a/Cloud/Migrations/ApplicationContextModelSnapshot.cs b/Cloud/Migrations/ApplicationContextModelSnapshot.cs index fc15a07..c499a41 100644 --- a/Cloud/Migrations/ApplicationContextModelSnapshot.cs +++ b/Cloud/Migrations/ApplicationContextModelSnapshot.cs @@ -21,6 +21,32 @@ namespace Cloud.Migrations NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + modelBuilder.Entity("Cloud.Models.Farm", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("RaspberryMacAddr") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Farms"); + }); + modelBuilder.Entity("Cloud.Models.User", b => { b.Property("Id") @@ -45,6 +71,22 @@ namespace Cloud.Migrations b.ToTable("Users"); }); + + modelBuilder.Entity("Cloud.Models.Farm", b => + { + b.HasOne("Cloud.Models.User", "User") + .WithMany("Farms") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Cloud.Models.User", b => + { + b.Navigation("Farms"); + }); #pragma warning restore 612, 618 } } diff --git a/Cloud/Models/User.cs b/Cloud/Models/User.cs index 27c8ee4..269dec4 100644 --- a/Cloud/Models/User.cs +++ b/Cloud/Models/User.cs @@ -8,4 +8,6 @@ public class User public string Email { get; set; } public string Password { get; set; } + + public List Farms { get; set; } = new(); } \ No newline at end of file diff --git a/Cloud/Program.cs b/Cloud/Program.cs index 1c10d82..6f77533 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -38,6 +38,7 @@ builder.Services.AddFluentValidationAutoValidation(); builder.Services.AddFluentValidationClientsideAdapters(); builder.Services.AddValidatorsFromAssemblyContaining(); builder.Services.AddValidatorsFromAssemblyContaining(); +builder.Services.AddValidatorsFromAssemblyContaining(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer();