This commit is contained in:
2023-10-30 21:58:56 +01:00
parent 5792d2fcf4
commit 08983ba30d
7 changed files with 38 additions and 29 deletions

View File

@@ -8,12 +8,13 @@ public class ApplicationContext : DbContext
: base(options) : base(options)
{ {
} }
public DbSet<Gerbil> Gerbils { get; set; }
public DbSet<Litter> Litters { get; set; }
public DbSet<Breeder> Breeders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.Entity<Gerbil>().ToTable("Gerbils");
modelBuilder.Entity<Litter>().ToTable("Litters");
modelBuilder.Entity<Breeder>().ToTable("Breeders");
modelBuilder.Entity<Gerbil>().HasOne(entity => entity.Litter); modelBuilder.Entity<Gerbil>().HasOne(entity => entity.Litter);
modelBuilder.Entity<Litter>().HasOne(entity => entity.Father); modelBuilder.Entity<Litter>().HasOne(entity => entity.Father);
modelBuilder.Entity<Litter>().HasOne(entity => entity.Mother); modelBuilder.Entity<Litter>().HasOne(entity => entity.Mother);

View File

@@ -79,8 +79,8 @@ namespace GerbilManagerWebAPI.Controllers
} }
Gerbil updatedGerbil = existingItem with{ Gerbil updatedGerbil = existingItem with{
Breeder = unitOfWork.BreederRepository.GetByID(gerbilDto.Breeder.Id), Breeder = unitOfWork.BreederRepository.GetByID(gerbilDto.Breeder),
Litter = unitOfWork.LitterRepository.GetByID(gerbilDto.Litter.Id), Litter = unitOfWork.LitterRepository.GetByID(gerbilDto.Litter),
Gender = (Gender) (int) gerbilDto.Gender, Gender = (Gender) (int) gerbilDto.Gender,
Name = gerbilDto.Name Name = gerbilDto.Name
}; };

View File

@@ -24,7 +24,7 @@ namespace GerbilManagerWebAPI.Controllers
[HttpGet] [HttpGet]
public IEnumerable<LitterDto> GetLitters() public IEnumerable<LitterDto> GetLitters()
{ {
var items = unitOfWork.LitterRepository.Get().Select( litter => litter.AsDto()); var items = unitOfWork.LitterRepository.Get(includeProperties: "Father,Mother").Select( litter => litter.AsDto());
return items; return items;
} }

View File

@@ -4,7 +4,7 @@ namespace GerbilManagerWebAPI.Dtos
{ {
public string? Name { get; init; } public string? Name { get; init; }
public GenderDto? Gender { get; init; } public GenderDto? Gender { get; init; }
public LitterDto? Litter { get; init; } public Guid? Litter { get; init; }
public BreederDto? Breeder { get; init; } public Guid? Breeder { get; init; }
} }
} }

View File

@@ -11,8 +11,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace GerbilManagerWebAPI.Migrations namespace GerbilManagerWebAPI.Migrations
{ {
[DbContext(typeof(ApplicationContext))] [DbContext(typeof(ApplicationContext))]
[Migration("20231023182219_InitialCreate")] [Migration("20231030203950_initialCreate")]
partial class InitialCreate partial class initialCreate
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -31,11 +31,12 @@ namespace GerbilManagerWebAPI.Migrations
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Breeders"); b.ToTable("Breeders", (string)null);
}); });
modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b => modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>
@@ -47,13 +48,15 @@ namespace GerbilManagerWebAPI.Migrations
b.Property<Guid?>("BreederId") b.Property<Guid?>("BreederId")
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<int>("Gender") b.Property<string>("Gender")
.HasColumnType("int"); .IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<Guid?>("LitterId") b.Property<Guid?>("LitterId")
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.HasKey("Id"); b.HasKey("Id");
@@ -62,7 +65,7 @@ namespace GerbilManagerWebAPI.Migrations
b.HasIndex("LitterId"); b.HasIndex("LitterId");
b.ToTable("Gerbils"); b.ToTable("Gerbils", (string)null);
}); });
modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b => modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b =>
@@ -81,9 +84,10 @@ namespace GerbilManagerWebAPI.Migrations
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.Property<int>("Strength") b.Property<int?>("Strength")
.HasColumnType("int"); .HasColumnType("int");
b.HasKey("Id"); b.HasKey("Id");
@@ -92,7 +96,7 @@ namespace GerbilManagerWebAPI.Migrations
b.HasIndex("MotherId"); b.HasIndex("MotherId");
b.ToTable("Litters"); b.ToTable("Litters", (string)null);
}); });
modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b => modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>

View File

@@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace GerbilManagerWebAPI.Migrations namespace GerbilManagerWebAPI.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
public partial class InitialCreate : Migration public partial class initialCreate : Migration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
@@ -16,7 +16,7 @@ namespace GerbilManagerWebAPI.Migrations
columns: table => new columns: table => new
{ {
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false), Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: true) Name = table.Column<string>(type: "nvarchar(max)", nullable: false)
}, },
constraints: table => constraints: table =>
{ {
@@ -28,8 +28,8 @@ namespace GerbilManagerWebAPI.Migrations
columns: table => new columns: table => new
{ {
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false), Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: true), Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Gender = table.Column<int>(type: "nvarchar(max)", nullable: false), Gender = table.Column<string>(type: "nvarchar(max)", nullable: false),
LitterId = table.Column<Guid>(type: "uniqueidentifier", nullable: true), LitterId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
BreederId = table.Column<Guid>(type: "uniqueidentifier", nullable: true) BreederId = table.Column<Guid>(type: "uniqueidentifier", nullable: true)
}, },
@@ -48,9 +48,9 @@ namespace GerbilManagerWebAPI.Migrations
columns: table => new columns: table => new
{ {
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false), Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: true), Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Date = table.Column<DateTime>(type: "datetime2", nullable: false), Date = table.Column<DateTime>(type: "datetime2", nullable: false),
Strength = table.Column<int>(type: "int", nullable: false), Strength = table.Column<int>(type: "int", nullable: true),
FatherId = table.Column<Guid>(type: "uniqueidentifier", nullable: true), FatherId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
MotherId = table.Column<Guid>(type: "uniqueidentifier", nullable: true) MotherId = table.Column<Guid>(type: "uniqueidentifier", nullable: true)
}, },

View File

@@ -28,11 +28,12 @@ namespace GerbilManagerWebAPI.Migrations
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Breeders"); b.ToTable("Breeders", (string)null);
}); });
modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b => modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>
@@ -44,13 +45,15 @@ namespace GerbilManagerWebAPI.Migrations
b.Property<Guid?>("BreederId") b.Property<Guid?>("BreederId")
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<int>("Gender") b.Property<string>("Gender")
.HasColumnType("int"); .IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<Guid?>("LitterId") b.Property<Guid?>("LitterId")
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.HasKey("Id"); b.HasKey("Id");
@@ -59,7 +62,7 @@ namespace GerbilManagerWebAPI.Migrations
b.HasIndex("LitterId"); b.HasIndex("LitterId");
b.ToTable("Gerbils"); b.ToTable("Gerbils", (string)null);
}); });
modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b => modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b =>
@@ -78,9 +81,10 @@ namespace GerbilManagerWebAPI.Migrations
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.Property<int>("Strength") b.Property<int?>("Strength")
.HasColumnType("int"); .HasColumnType("int");
b.HasKey("Id"); b.HasKey("Id");
@@ -89,7 +93,7 @@ namespace GerbilManagerWebAPI.Migrations
b.HasIndex("MotherId"); b.HasIndex("MotherId");
b.ToTable("Litters"); b.ToTable("Litters", (string)null);
}); });
modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b => modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>