Make Farm CRUD

This commit is contained in:
Артем Харламов 2024-10-29 00:38:46 +04:00
parent 0bf5823652
commit 33c3a074da
6 changed files with 102 additions and 56 deletions

View File

@ -4,7 +4,8 @@ using Microsoft.EntityFrameworkCore;
namespace Cloud; namespace Cloud;
public class ApplicationContext : DbContext public class ApplicationContext : DbContext
{ {
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; set; } = null!;
public DbSet<Farm> Farms { get; set; } = null!;
public ApplicationContext(DbContextOptions<ApplicationContext> options) public ApplicationContext(DbContextOptions<ApplicationContext> options)
: base(options) : base(options)

View File

@ -10,44 +10,44 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Cloud.Migrations namespace Cloud.Migrations
{ {
[DbContext(typeof(ApplicationContext))] [DbContext(typeof(ApplicationContext))]
[Migration("20241027220558_CreateUsersTable")] [Migration("20241027220558_CreateUsersTable")]
partial class CreateUsersTable partial class CreateUsersTable
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "6.0.14") .HasAnnotation("ProductVersion", "6.0.14")
.HasAnnotation("Relational:MaxIdentifierLength", 63); .HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Cloud.Models.User", b => modelBuilder.Entity("Cloud.Models.User", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("integer"); .HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id")); NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Email") b.Property<string>("Email")
.IsRequired() .IsRequired()
.HasColumnType("text"); .HasColumnType("text");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.HasColumnType("text"); .HasColumnType("text");
b.Property<string>("Password") b.Property<string>("Password")
.IsRequired() .IsRequired()
.HasColumnType("text"); .HasColumnType("text");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Users"); b.ToTable("Users");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }
} }

View File

@ -5,30 +5,30 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Cloud.Migrations namespace Cloud.Migrations
{ {
public partial class CreateUsersTable : Migration public partial class CreateUsersTable : Migration
{ {
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "Users", name: "Users",
columns: table => new columns: table => new
{ {
Id = table.Column<int>(type: "integer", nullable: false) Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "text", nullable: false), Name = table.Column<string>(type: "text", nullable: false),
Email = table.Column<string>(type: "text", nullable: false), Email = table.Column<string>(type: "text", nullable: false),
Password = table.Column<string>(type: "text", nullable: false) Password = table.Column<string>(type: "text", nullable: false)
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_Users", x => x.Id); table.PrimaryKey("PK_Users", x => x.Id);
}); });
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Users"); name: "Users");
} }
} }
} }

View File

@ -21,6 +21,32 @@ namespace Cloud.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Cloud.Models.Farm", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<string>("RaspberryMacAddr")
.IsRequired()
.HasColumnType("text");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Farms");
});
modelBuilder.Entity("Cloud.Models.User", b => modelBuilder.Entity("Cloud.Models.User", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -45,6 +71,22 @@ namespace Cloud.Migrations
b.ToTable("Users"); 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 #pragma warning restore 612, 618
} }
} }

View File

@ -8,4 +8,6 @@ public class User
public string Email { get; set; } public string Email { get; set; }
public string Password { get; set; } public string Password { get; set; }
public List<Farm> Farms { get; set; } = new();
} }

View File

@ -38,6 +38,7 @@ builder.Services.AddFluentValidationAutoValidation();
builder.Services.AddFluentValidationClientsideAdapters(); builder.Services.AddFluentValidationClientsideAdapters();
builder.Services.AddValidatorsFromAssemblyContaining<LoginValidator>(); builder.Services.AddValidatorsFromAssemblyContaining<LoginValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<RegisterValidator>(); builder.Services.AddValidatorsFromAssemblyContaining<RegisterValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<FarmValidator>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();