diff --git a/GerbilManager.csproj b/GerbilManager.csproj
new file mode 100644
index 0000000..d6fce08
--- /dev/null
+++ b/GerbilManager.csproj
@@ -0,0 +1,16 @@
+
+
+
+ net7.0
+ enable
+ enable
+
+
+
+
+
+
+
+
+
+
diff --git a/GerbilManagerWebAPI/ApplicationContext.cs b/GerbilManagerWebAPI/ApplicationContext.cs
index 7fe57b5..94926a1 100644
--- a/GerbilManagerWebAPI/ApplicationContext.cs
+++ b/GerbilManagerWebAPI/ApplicationContext.cs
@@ -1,12 +1,27 @@
using GerbilManagerWebAPI.Models;
using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
public class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions options)
- :base(options)
+ : base(options)
{
}
public DbSet Gerbils { get; set; }
+ public DbSet Litters { get; set; }
+ public DbSet Breeders { get; set; }
+
+ protected override void OnModelCreating(ModelBuilder modelBuilder)
+ {
+ modelBuilder.Entity().HasOne(entity => entity.Litter);
+ modelBuilder.Entity().HasOne(entity => entity.Father);
+ modelBuilder.Entity().HasOne(entity => entity.Mother);
+
+ modelBuilder
+ .Entity()
+ .Property(d => d.Gender)
+ .HasConversion(new EnumToStringConverter());
+ }
}
diff --git a/GerbilManagerWebAPI/Controllers/GerbilsController.cs b/GerbilManagerWebAPI/Controllers/GerbilsController.cs
index 8439552..231f13c 100644
--- a/GerbilManagerWebAPI/Controllers/GerbilsController.cs
+++ b/GerbilManagerWebAPI/Controllers/GerbilsController.cs
@@ -47,11 +47,19 @@ namespace GerbilManagerWebAPI.Controllers
Gerbil gerbil = new Gerbil{
Id = Guid.NewGuid(),
Name = dto.Name,
- Breeder = breederRepository.GetBreeder(dto.Breeder),
- Litter = litterRepository.GetLitter(dto.Litter),
Gender = (Gender) (int) dto.Gender
};
+ if(dto.Litter != Guid.Empty)
+ {
+ gerbil.Litter = litterRepository.GetLitter(dto.Litter);
+ }
+
+ if(dto.Breeder != Guid.Empty)
+ {
+ gerbil.Breeder = breederRepository.GetBreeder(dto.Breeder);
+ }
+
this.repository.CreateGerbil(gerbil);
return CreatedAtAction(nameof(GetGerbil), new { id = gerbil.Id}, gerbil.AsDto());
diff --git a/GerbilManagerWebAPI/Converter/GerbilConverter.cs b/GerbilManagerWebAPI/Converter/GerbilConverter.cs
index 3b15269..ab31f77 100644
--- a/GerbilManagerWebAPI/Converter/GerbilConverter.cs
+++ b/GerbilManagerWebAPI/Converter/GerbilConverter.cs
@@ -8,10 +8,11 @@ namespace GerbilManagerWebAPI.Converter
public static GerbilDto AsDto(this Gerbil gerbil)
{
return new GerbilDto{
- Breeder = gerbil.Breeder.AsDto(),
- Gender = (GenderDto) ((int) gerbil.Gender),
+ Id = gerbil.Id,
+ Gender = (GenderDto) (int) gerbil.Gender,
Name = gerbil.Name,
- Litter = gerbil.Litter.AsDto()
+ Breeder = gerbil.Breeder?.AsDto(),
+ Litter = gerbil.Litter?.AsDto()
};
}
}
diff --git a/GerbilManagerWebAPI/Dtos/GerbilDto.cs b/GerbilManagerWebAPI/Dtos/GerbilDto.cs
index 775405f..9043844 100644
--- a/GerbilManagerWebAPI/Dtos/GerbilDto.cs
+++ b/GerbilManagerWebAPI/Dtos/GerbilDto.cs
@@ -5,7 +5,7 @@ namespace GerbilManagerWebAPI.Dtos
public Guid Id { get; init; }
public string Name { get; init; }
public GenderDto Gender { get; init; }
- public LitterDto Litter { get; init; }
- public BreederDto Breeder { get; init; }
+ public LitterDto? Litter { get; init; }
+ public BreederDto? Breeder { get; init; }
}
}
\ No newline at end of file
diff --git a/GerbilManagerWebAPI/GerbilManagerWebAPI.csproj b/GerbilManagerWebAPI/GerbilManagerWebAPI.csproj
index 16f241e..333c6ed 100644
--- a/GerbilManagerWebAPI/GerbilManagerWebAPI.csproj
+++ b/GerbilManagerWebAPI/GerbilManagerWebAPI.csproj
@@ -9,6 +9,10 @@
+
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
diff --git a/GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.Designer.cs b/GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.Designer.cs
new file mode 100644
index 0000000..e3659af
--- /dev/null
+++ b/GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.Designer.cs
@@ -0,0 +1,130 @@
+//
+using System;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+
+#nullable disable
+
+namespace GerbilManagerWebAPI.Migrations
+{
+ [DbContext(typeof(ApplicationContext))]
+ [Migration("20231023182219_InitialCreate")]
+ partial class InitialCreate
+ {
+ ///
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("ProductVersion", "7.0.12")
+ .HasAnnotation("Relational:MaxIdentifierLength", 128);
+
+ SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Breeder", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Name")
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("Breeders");
+ });
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("BreederId")
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Gender")
+ .HasColumnType("int");
+
+ b.Property("LitterId")
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Name")
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("BreederId");
+
+ b.HasIndex("LitterId");
+
+ b.ToTable("Gerbils");
+ });
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Date")
+ .HasColumnType("datetime2");
+
+ b.Property("FatherId")
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("MotherId")
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Name")
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Strength")
+ .HasColumnType("int");
+
+ b.HasKey("Id");
+
+ b.HasIndex("FatherId");
+
+ b.HasIndex("MotherId");
+
+ b.ToTable("Litters");
+ });
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>
+ {
+ b.HasOne("GerbilManagerWebAPI.Models.Breeder", "Breeder")
+ .WithMany()
+ .HasForeignKey("BreederId");
+
+ b.HasOne("GerbilManagerWebAPI.Models.Litter", "Litter")
+ .WithMany()
+ .HasForeignKey("LitterId");
+
+ b.Navigation("Breeder");
+
+ b.Navigation("Litter");
+ });
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b =>
+ {
+ b.HasOne("GerbilManagerWebAPI.Models.Gerbil", "Father")
+ .WithMany()
+ .HasForeignKey("FatherId");
+
+ b.HasOne("GerbilManagerWebAPI.Models.Gerbil", "Mother")
+ .WithMany()
+ .HasForeignKey("MotherId");
+
+ b.Navigation("Father");
+
+ b.Navigation("Mother");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.cs b/GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.cs
new file mode 100644
index 0000000..540ad6c
--- /dev/null
+++ b/GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.cs
@@ -0,0 +1,121 @@
+using System;
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace GerbilManagerWebAPI.Migrations
+{
+ ///
+ public partial class InitialCreate : Migration
+ {
+ ///
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "Breeders",
+ columns: table => new
+ {
+ Id = table.Column(type: "uniqueidentifier", nullable: false),
+ Name = table.Column(type: "nvarchar(max)", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Breeders", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Gerbils",
+ columns: table => new
+ {
+ Id = table.Column(type: "uniqueidentifier", nullable: false),
+ Name = table.Column(type: "nvarchar(max)", nullable: true),
+ Gender = table.Column(type: "nvarchar(max)", nullable: false),
+ LitterId = table.Column(type: "uniqueidentifier", nullable: true),
+ BreederId = table.Column(type: "uniqueidentifier", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Gerbils", x => x.Id);
+ table.ForeignKey(
+ name: "FK_Gerbils_Breeders_BreederId",
+ column: x => x.BreederId,
+ principalTable: "Breeders",
+ principalColumn: "Id");
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Litters",
+ columns: table => new
+ {
+ Id = table.Column(type: "uniqueidentifier", nullable: false),
+ Name = table.Column(type: "nvarchar(max)", nullable: true),
+ Date = table.Column(type: "datetime2", nullable: false),
+ Strength = table.Column(type: "int", nullable: false),
+ FatherId = table.Column(type: "uniqueidentifier", nullable: true),
+ MotherId = table.Column(type: "uniqueidentifier", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Litters", x => x.Id);
+ table.ForeignKey(
+ name: "FK_Litters_Gerbils_FatherId",
+ column: x => x.FatherId,
+ principalTable: "Gerbils",
+ principalColumn: "Id");
+ table.ForeignKey(
+ name: "FK_Litters_Gerbils_MotherId",
+ column: x => x.MotherId,
+ principalTable: "Gerbils",
+ principalColumn: "Id");
+ });
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Gerbils_BreederId",
+ table: "Gerbils",
+ column: "BreederId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Gerbils_LitterId",
+ table: "Gerbils",
+ column: "LitterId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Litters_FatherId",
+ table: "Litters",
+ column: "FatherId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Litters_MotherId",
+ table: "Litters",
+ column: "MotherId");
+
+ migrationBuilder.AddForeignKey(
+ name: "FK_Gerbils_Litters_LitterId",
+ table: "Gerbils",
+ column: "LitterId",
+ principalTable: "Litters",
+ principalColumn: "Id");
+ }
+
+ ///
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropForeignKey(
+ name: "FK_Gerbils_Breeders_BreederId",
+ table: "Gerbils");
+
+ migrationBuilder.DropForeignKey(
+ name: "FK_Gerbils_Litters_LitterId",
+ table: "Gerbils");
+
+ migrationBuilder.DropTable(
+ name: "Breeders");
+
+ migrationBuilder.DropTable(
+ name: "Litters");
+
+ migrationBuilder.DropTable(
+ name: "Gerbils");
+ }
+ }
+}
diff --git a/GerbilManagerWebAPI/Migrations/ApplicationContextModelSnapshot.cs b/GerbilManagerWebAPI/Migrations/ApplicationContextModelSnapshot.cs
new file mode 100644
index 0000000..5ff95f5
--- /dev/null
+++ b/GerbilManagerWebAPI/Migrations/ApplicationContextModelSnapshot.cs
@@ -0,0 +1,127 @@
+//
+using System;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+
+#nullable disable
+
+namespace GerbilManagerWebAPI.Migrations
+{
+ [DbContext(typeof(ApplicationContext))]
+ partial class ApplicationContextModelSnapshot : ModelSnapshot
+ {
+ protected override void BuildModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("ProductVersion", "7.0.12")
+ .HasAnnotation("Relational:MaxIdentifierLength", 128);
+
+ SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Breeder", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Name")
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("Breeders");
+ });
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("BreederId")
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Gender")
+ .HasColumnType("int");
+
+ b.Property("LitterId")
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Name")
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("BreederId");
+
+ b.HasIndex("LitterId");
+
+ b.ToTable("Gerbils");
+ });
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Date")
+ .HasColumnType("datetime2");
+
+ b.Property("FatherId")
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("MotherId")
+ .HasColumnType("uniqueidentifier");
+
+ b.Property("Name")
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Strength")
+ .HasColumnType("int");
+
+ b.HasKey("Id");
+
+ b.HasIndex("FatherId");
+
+ b.HasIndex("MotherId");
+
+ b.ToTable("Litters");
+ });
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>
+ {
+ b.HasOne("GerbilManagerWebAPI.Models.Breeder", "Breeder")
+ .WithMany()
+ .HasForeignKey("BreederId");
+
+ b.HasOne("GerbilManagerWebAPI.Models.Litter", "Litter")
+ .WithMany()
+ .HasForeignKey("LitterId");
+
+ b.Navigation("Breeder");
+
+ b.Navigation("Litter");
+ });
+
+ modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b =>
+ {
+ b.HasOne("GerbilManagerWebAPI.Models.Gerbil", "Father")
+ .WithMany()
+ .HasForeignKey("FatherId");
+
+ b.HasOne("GerbilManagerWebAPI.Models.Gerbil", "Mother")
+ .WithMany()
+ .HasForeignKey("MotherId");
+
+ b.Navigation("Father");
+
+ b.Navigation("Mother");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/GerbilManagerWebAPI/Models/Breeder.cs b/GerbilManagerWebAPI/Models/Breeder.cs
index a555c43..e9a787d 100644
--- a/GerbilManagerWebAPI/Models/Breeder.cs
+++ b/GerbilManagerWebAPI/Models/Breeder.cs
@@ -1,8 +1,11 @@
+using System.ComponentModel.DataAnnotations;
+
namespace GerbilManagerWebAPI.Models
{
public record Breeder
{
+ [Key]
public Guid Id { get; init; }
- public string Name { get; init; }
+ public string? Name { get; init; }
}
}
\ No newline at end of file
diff --git a/GerbilManagerWebAPI/Models/Gerbil.cs b/GerbilManagerWebAPI/Models/Gerbil.cs
index 7e28737..0e622ea 100644
--- a/GerbilManagerWebAPI/Models/Gerbil.cs
+++ b/GerbilManagerWebAPI/Models/Gerbil.cs
@@ -1,11 +1,14 @@
+using System.ComponentModel.DataAnnotations;
+
namespace GerbilManagerWebAPI.Models
{
public record Gerbil
{
+ [Key]
public Guid Id { get; init; }
- public string Name { get; init; }
+ public required string Name { get; init; }
public Gender Gender { get; init; }
- public Litter Litter { get; init; }
- public Breeder Breeder { get; init; }
+ public Litter? Litter { get; set; }
+ public Breeder? Breeder { get; set; }
}
}
\ No newline at end of file
diff --git a/GerbilManagerWebAPI/Models/Litter.cs b/GerbilManagerWebAPI/Models/Litter.cs
index 9f9174f..5bb0f62 100644
--- a/GerbilManagerWebAPI/Models/Litter.cs
+++ b/GerbilManagerWebAPI/Models/Litter.cs
@@ -1,13 +1,16 @@
+using System.ComponentModel.DataAnnotations;
+
namespace GerbilManagerWebAPI.Models
{
public record Litter
{
+ [Key]
public Guid Id { get; init; }
- public string Name { get; init; }
+ public string? Name { get; init; }
public DateTime Date { get; init; }
public int Strength { get; init; }
- public Gerbil Father { get; init; }
- public Gerbil Mother { get; init; }
+ public Gerbil? Father { get; init; }
+ public Gerbil? Mother { get; init; }
}
}
diff --git a/GerbilManagerWebAPI/Program.cs b/GerbilManagerWebAPI/Program.cs
index c09d210..b012fae 100644
--- a/GerbilManagerWebAPI/Program.cs
+++ b/GerbilManagerWebAPI/Program.cs
@@ -9,14 +9,11 @@ builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
-
-builder.Services.AddSingleton();
-
builder.Services.AddDbContext(opts => opts.UseSqlServer(builder.Configuration.GetConnectionString("sqlConnection")));
+builder.Services.AddScoped();
var app = builder.Build();
-
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
diff --git a/GerbilManagerWebAPI/Repositories/EFGerbilRepository.cs b/GerbilManagerWebAPI/Repositories/EFGerbilRepository.cs
new file mode 100644
index 0000000..275928d
--- /dev/null
+++ b/GerbilManagerWebAPI/Repositories/EFGerbilRepository.cs
@@ -0,0 +1,42 @@
+using GerbilManagerWebAPI.Models;
+using Microsoft.EntityFrameworkCore;
+
+namespace GerbilManagerWebAPI.Repositories
+{
+ public class EFGerbilRepository : IGerbilRepository
+ {
+ private readonly ApplicationContext context;
+ public EFGerbilRepository(ApplicationContext context)
+ {
+ this.context = context;
+ }
+
+ public void CreateGerbil(Gerbil gerbil)
+ {
+ context.Gerbils.Add(gerbil);
+ context.SaveChanges();
+ }
+
+ public void DeleteGerbil(Gerbil gerbil)
+ {
+ context.Gerbils.Remove(gerbil);
+ context.SaveChanges();
+ }
+
+ public Gerbil GetGerbil(Guid id)
+ {
+ return context.Gerbils.Where(entity => entity.Id == id).First();
+ }
+
+ public IEnumerable GetGerbils()
+ {
+ return context.Gerbils.ToList();
+ }
+
+ public void UpdateGerbil(Gerbil gerbil)
+ {
+ context.Gerbils.Update(gerbil);
+ context.SaveChanges();
+ }
+ }
+}
\ No newline at end of file
diff --git a/GerbilManagerWebAPI/appsettings.Development.json b/GerbilManagerWebAPI/appsettings.Development.json
index e0f2b5a..70e4a2b 100644
--- a/GerbilManagerWebAPI/appsettings.Development.json
+++ b/GerbilManagerWebAPI/appsettings.Development.json
@@ -6,6 +6,6 @@
}
},
"ConnectionStrings": {
- "sqlConnection": "server=.; database=GerbilManagerDebug; Integrated Security=true"
+ "sqlConnection": "server=.; database=GerbilManagerDebug; User Id=sa;Password=StrongerThenYYou1;Encrypt=False;TrustServerCertificate=True"
}
}
diff --git a/GerbilManagerWebAPI/appsettings.json b/GerbilManagerWebAPI/appsettings.json
index ce86cda..2699190 100644
--- a/GerbilManagerWebAPI/appsettings.json
+++ b/GerbilManagerWebAPI/appsettings.json
@@ -6,7 +6,7 @@
}
},
"ConnectionStrings": {
- "sqlConnection": "server=.; database=GerbilManager; Integrated Security=true"
+ "sqlConnection": "server=.; database=GerbilManagerDebug; User Id=sa;Password=StrongerThenYYou1;Encrypt=False;TrustServerCertificate=True"
},
"AllowedHosts": "*"
}