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

@ -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();