First table in database
This commit is contained in:
16
GerbilManager.csproj
Normal file
16
GerbilManager.csproj
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.12" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -1,12 +1,27 @@
|
|||||||
using GerbilManagerWebAPI.Models;
|
using GerbilManagerWebAPI.Models;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
public class ApplicationContext : DbContext
|
public class ApplicationContext : DbContext
|
||||||
{
|
{
|
||||||
public ApplicationContext(DbContextOptions options)
|
public ApplicationContext(DbContextOptions options)
|
||||||
:base(options)
|
: base(options)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
public DbSet<Gerbil> Gerbils { get; set; }
|
public DbSet<Gerbil> Gerbils { get; set; }
|
||||||
|
public DbSet<Litter> Litters { get; set; }
|
||||||
|
public DbSet<Breeder> Breeders { get; set; }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
modelBuilder.Entity<Gerbil>().HasOne(entity => entity.Litter);
|
||||||
|
modelBuilder.Entity<Litter>().HasOne(entity => entity.Father);
|
||||||
|
modelBuilder.Entity<Litter>().HasOne(entity => entity.Mother);
|
||||||
|
|
||||||
|
modelBuilder
|
||||||
|
.Entity<Gerbil>()
|
||||||
|
.Property(d => d.Gender)
|
||||||
|
.HasConversion(new EnumToStringConverter<Gender>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,11 +47,19 @@ namespace GerbilManagerWebAPI.Controllers
|
|||||||
Gerbil gerbil = new Gerbil{
|
Gerbil gerbil = new Gerbil{
|
||||||
Id = Guid.NewGuid(),
|
Id = Guid.NewGuid(),
|
||||||
Name = dto.Name,
|
Name = dto.Name,
|
||||||
Breeder = breederRepository.GetBreeder(dto.Breeder),
|
|
||||||
Litter = litterRepository.GetLitter(dto.Litter),
|
|
||||||
Gender = (Gender) (int) dto.Gender
|
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);
|
this.repository.CreateGerbil(gerbil);
|
||||||
|
|
||||||
return CreatedAtAction(nameof(GetGerbil), new { id = gerbil.Id}, gerbil.AsDto());
|
return CreatedAtAction(nameof(GetGerbil), new { id = gerbil.Id}, gerbil.AsDto());
|
||||||
|
|||||||
@@ -8,10 +8,11 @@ namespace GerbilManagerWebAPI.Converter
|
|||||||
public static GerbilDto AsDto(this Gerbil gerbil)
|
public static GerbilDto AsDto(this Gerbil gerbil)
|
||||||
{
|
{
|
||||||
return new GerbilDto{
|
return new GerbilDto{
|
||||||
Breeder = gerbil.Breeder.AsDto(),
|
Id = gerbil.Id,
|
||||||
Gender = (GenderDto) ((int) gerbil.Gender),
|
Gender = (GenderDto) (int) gerbil.Gender,
|
||||||
Name = gerbil.Name,
|
Name = gerbil.Name,
|
||||||
Litter = gerbil.Litter.AsDto()
|
Breeder = gerbil.Breeder?.AsDto(),
|
||||||
|
Litter = gerbil.Litter?.AsDto()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ namespace GerbilManagerWebAPI.Dtos
|
|||||||
public Guid Id { get; init; }
|
public Guid Id { get; init; }
|
||||||
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 LitterDto? Litter { get; init; }
|
||||||
public BreederDto Breeder { get; init; }
|
public BreederDto? Breeder { get; init; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,10 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.12" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.12" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
130
GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.Designer.cs
generated
Normal file
130
GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.Designer.cs
generated
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
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
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
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<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Breeders");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<Guid?>("BreederId")
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<int>("Gender")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<Guid?>("LitterId")
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("BreederId");
|
||||||
|
|
||||||
|
b.HasIndex("LitterId");
|
||||||
|
|
||||||
|
b.ToTable("Gerbils");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<DateTime>("Date")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<Guid?>("FatherId")
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<Guid?>("MotherId")
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<int>("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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
121
GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.cs
Normal file
121
GerbilManagerWebAPI/Migrations/20231023182219_InitialCreate.cs
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace GerbilManagerWebAPI.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class InitialCreate : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "Breeders",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
|
||||||
|
Name = table.Column<string>(type: "nvarchar(max)", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_Breeders", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "Gerbils",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
|
||||||
|
Name = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
Gender = table.Column<int>(type: "nvarchar(max)", nullable: false),
|
||||||
|
LitterId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
|
||||||
|
BreederId = table.Column<Guid>(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<Guid>(type: "uniqueidentifier", nullable: false),
|
||||||
|
Name = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
Date = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
Strength = table.Column<int>(type: "int", nullable: false),
|
||||||
|
FatherId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
|
||||||
|
MotherId = table.Column<Guid>(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");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
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<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Breeders");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("GerbilManagerWebAPI.Models.Gerbil", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<Guid?>("BreederId")
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<int>("Gender")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<Guid?>("LitterId")
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("BreederId");
|
||||||
|
|
||||||
|
b.HasIndex("LitterId");
|
||||||
|
|
||||||
|
b.ToTable("Gerbils");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("GerbilManagerWebAPI.Models.Litter", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<DateTime>("Date")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<Guid?>("FatherId")
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<Guid?>("MotherId")
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<int>("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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
namespace GerbilManagerWebAPI.Models
|
namespace GerbilManagerWebAPI.Models
|
||||||
{
|
{
|
||||||
public record Breeder
|
public record Breeder
|
||||||
{
|
{
|
||||||
|
[Key]
|
||||||
public Guid Id { get; init; }
|
public Guid Id { get; init; }
|
||||||
public string Name { get; init; }
|
public string? Name { get; init; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
namespace GerbilManagerWebAPI.Models
|
namespace GerbilManagerWebAPI.Models
|
||||||
{
|
{
|
||||||
public record Gerbil
|
public record Gerbil
|
||||||
{
|
{
|
||||||
|
[Key]
|
||||||
public Guid Id { get; init; }
|
public Guid Id { get; init; }
|
||||||
public string Name { get; init; }
|
public required string Name { get; init; }
|
||||||
public Gender Gender { get; init; }
|
public Gender Gender { get; init; }
|
||||||
public Litter Litter { get; init; }
|
public Litter? Litter { get; set; }
|
||||||
public Breeder Breeder { get; init; }
|
public Breeder? Breeder { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,16 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
namespace GerbilManagerWebAPI.Models
|
namespace GerbilManagerWebAPI.Models
|
||||||
{
|
{
|
||||||
public record Litter
|
public record Litter
|
||||||
{
|
{
|
||||||
|
[Key]
|
||||||
public Guid Id { get; init; }
|
public Guid Id { get; init; }
|
||||||
public string Name { get; init; }
|
public string? Name { get; init; }
|
||||||
public DateTime Date { get; init; }
|
public DateTime Date { get; init; }
|
||||||
public int Strength { get; init; }
|
public int Strength { get; init; }
|
||||||
public Gerbil Father { get; init; }
|
public Gerbil? Father { get; init; }
|
||||||
public Gerbil Mother { get; init; }
|
public Gerbil? Mother { get; init; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,14 +9,11 @@ builder.Services.AddControllers();
|
|||||||
// 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();
|
||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
builder.Services.AddSingleton<IGerbilRepository, InMemGerbilRepository>();
|
|
||||||
|
|
||||||
builder.Services.AddDbContext<ApplicationContext>(opts => opts.UseSqlServer(builder.Configuration.GetConnectionString("sqlConnection")));
|
builder.Services.AddDbContext<ApplicationContext>(opts => opts.UseSqlServer(builder.Configuration.GetConnectionString("sqlConnection")));
|
||||||
|
builder.Services.AddScoped<IGerbilRepository, EFGerbilRepository>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
|
|||||||
42
GerbilManagerWebAPI/Repositories/EFGerbilRepository.cs
Normal file
42
GerbilManagerWebAPI/Repositories/EFGerbilRepository.cs
Normal file
@@ -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<Gerbil> GetGerbils()
|
||||||
|
{
|
||||||
|
return context.Gerbils.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateGerbil(Gerbil gerbil)
|
||||||
|
{
|
||||||
|
context.Gerbils.Update(gerbil);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"sqlConnection": "server=.; database=GerbilManagerDebug; Integrated Security=true"
|
"sqlConnection": "server=.; database=GerbilManagerDebug; User Id=sa;Password=StrongerThenYYou1;Encrypt=False;TrustServerCertificate=True"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"sqlConnection": "server=.; database=GerbilManager; Integrated Security=true"
|
"sqlConnection": "server=.; database=GerbilManagerDebug; User Id=sa;Password=StrongerThenYYou1;Encrypt=False;TrustServerCertificate=True"
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user