PIbd-21_IevlewaMD_Lab3_hard #12

Closed
chtzsch wants to merge 3 commits from lab3Hard into lab2Hard
18 changed files with 735 additions and 42 deletions
Showing only changes of commit 29a989cc6c - Show all commits

View File

@ -2,14 +2,14 @@
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using PrecastConcretePlantDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Implements
namespace PrecastConcretePlantDataBaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{

View File

@ -3,14 +3,14 @@ using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using PrecastConcretePlantDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Implements
namespace PrecastConcretePlantDataBaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{

View File

@ -3,7 +3,7 @@ using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using PrecastConcretePlantDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -11,7 +11,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Implements
namespace PrecastConcretePlantDataBaseImplement.Implements
{
public class ReinforcedStorage : IReinforcedStorage
{

View File

@ -0,0 +1,147 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataBaseImplement.Models;
using PrecastConcretePlantDataModels.Models;
using Microsoft.EntityFrameworkCore;
namespace PrecastConcretePlantDataBaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new PrecastConcretePlantDataBase();
return context.Shops
.Include(x => x.Reinforceds)
.ThenInclude(x => x.Reinforced)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new PrecastConcretePlantDataBase();
return context.Shops
.Include(x => x.Reinforceds)
.ThenInclude(x => x.Reinforced)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
using var context = new PrecastConcretePlantDataBase();
return context.Shops
.Include(x => x.Reinforceds)
.ThenInclude(x => x.Reinforced)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
shop.UpdateReinforceds(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
var element = context.Shops
.Include(x => x.Reinforceds)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
//продажа изделий
public bool MakeSale(IReinforcedModel model, int count)
{
using var context = new PrecastConcretePlantDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
foreach (var shop in context.Shops.Include(x => x.Reinforceds).ThenInclude(x => x.Reinforced)
.Where(x => x.Reinforceds.Any(x => x.ReinforcedId == model.Id))
.ToList())
{
var reinforced = shop.ShopReinforceds[model.Id];
int min = Math.Min(reinforced.Item2, count);
if (min == reinforced.Item2)
{
shop.ShopReinforceds.Remove(model.Id);
}
else
{
shop.ShopReinforceds[model.Id] = (reinforced.Item1, reinforced.Item2 - min);
}
shop.UpdateReinforceds(context, new() { Id = shop.Id, ShopReinforceds = shop.ShopReinforceds });
count -= min;
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -5,11 +5,11 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PrecastConcretePlantDatabaseImplement;
using PrecastConcretePlantDataBaseImplement;
#nullable disable
namespace PrecastConcretePlantDatabaseImplement.Migrations
namespace PrecastConcretePlantDataBaseImplement.Migrations
{
[DbContext(typeof(PrecastConcretePlantDataBase))]
[Migration("20240404165842_InitialCreate")]
@ -25,7 +25,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -45,7 +45,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.ToTable("Components");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -78,7 +78,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -98,7 +98,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.ToTable("Reinforceds");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -124,9 +124,9 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.ToTable("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Order", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Orders")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
@ -135,15 +135,15 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component")
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Component", "Component")
.WithMany("ReinforcedComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Components")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
@ -154,12 +154,12 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Component", b =>
{
b.Navigation("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b =>
{
b.Navigation("Components");

View File

@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PrecastConcretePlantDatabaseImplement.Migrations
namespace PrecastConcretePlantDataBaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration

View File

@ -0,0 +1,250 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PrecastConcretePlantDataBaseImplement;
#nullable disable
namespace PrecastConcretePlantDataBaseImplement.Migrations
{
[DbContext(typeof(PrecastConcretePlantDataBase))]
[Migration("20240516193152_initial")]
partial class initial
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("ReinforcedId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ReinforcedId");
b.ToTable("Orders");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("ReinforcedName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Reinforceds");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ReinforcedId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("ReinforcedId");
b.ToTable("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<int>("ReinforcedsMax")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ShopReinforced", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ReinforcedId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ReinforcedId");
b.HasIndex("ShopId");
b.ToTable("ShopReinforceds");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Order", b =>
{
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Orders")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b =>
{
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Component", "Component")
.WithMany("ReinforcedComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Components")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ShopReinforced", b =>
{
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced")
.WithMany("ShopReinforceds")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Shop", "Shop")
.WithMany("Reinforceds")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Reinforced");
b.Navigation("Shop");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Component", b =>
{
b.Navigation("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopReinforceds");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Shop", b =>
{
b.Navigation("Reinforceds");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,78 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PrecastConcretePlantDataBaseImplement.Migrations
{
/// <inheritdoc />
public partial class initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
DateOpening = table.Column<DateTime>(type: "datetime2", nullable: false),
ReinforcedsMax = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ShopReinforceds",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopId = table.Column<int>(type: "int", nullable: false),
ReinforcedId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShopReinforceds", x => x.Id);
table.ForeignKey(
name: "FK_ShopReinforceds_Reinforceds_ReinforcedId",
column: x => x.ReinforcedId,
principalTable: "Reinforceds",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopReinforceds_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ShopReinforceds_ReinforcedId",
table: "ShopReinforceds",
column: "ReinforcedId");
migrationBuilder.CreateIndex(
name: "IX_ShopReinforceds_ShopId",
table: "ShopReinforceds",
column: "ShopId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ShopReinforceds");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -4,11 +4,11 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PrecastConcretePlantDatabaseImplement;
using PrecastConcretePlantDataBaseImplement;
#nullable disable
namespace PrecastConcretePlantDatabaseImplement.Migrations
namespace PrecastConcretePlantDataBaseImplement.Migrations
{
[DbContext(typeof(PrecastConcretePlantDataBase))]
partial class PrecastConcretePlantDataBaseModelSnapshot : ModelSnapshot
@ -22,7 +22,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -42,7 +42,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.ToTable("Components");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -75,7 +75,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -95,7 +95,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.ToTable("Reinforceds");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -121,9 +121,62 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.ToTable("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Shop", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<int>("ReinforcedsMax")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ShopReinforced", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ReinforcedId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ReinforcedId");
b.HasIndex("ShopId");
b.ToTable("ShopReinforceds");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Order", b =>
{
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Orders")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
@ -132,15 +185,15 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component")
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Component", "Component")
.WithMany("ReinforcedComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Components")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
@ -151,16 +204,42 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ShopReinforced", b =>
{
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced")
.WithMany("ShopReinforceds")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Shop", "Shop")
.WithMany("Reinforceds")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Reinforced");
b.Navigation("Shop");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Component", b =>
{
b.Navigation("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopReinforceds");
});
modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Shop", b =>
{
b.Navigation("Reinforceds");
});
#pragma warning restore 612, 618
}

View File

@ -9,7 +9,7 @@ using PrecastConcretePlantDataModels.Models;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
namespace PrecastConcretePlantDatabaseImplement.Models
namespace PrecastConcretePlantDataBaseImplement.Models
{
public class Component : IComponentModel
{

View File

@ -9,7 +9,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
namespace PrecastConcretePlantDataBaseImplement.Models
{
public class Order : IOrderModel
{

View File

@ -10,7 +10,7 @@ using System.Threading.Tasks;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
namespace PrecastConcretePlantDatabaseImplement.Models
namespace PrecastConcretePlantDataBaseImplement.Models
{
public class Reinforced : IReinforcedModel
{
@ -45,6 +45,9 @@ namespace PrecastConcretePlantDatabaseImplement.Models
[ForeignKey("ReinforcedId")]
public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("ReinforcedId")]
public virtual List<ShopReinforced> ShopReinforceds { get; set; } = new();
public static Reinforced Create(PrecastConcretePlantDataBase context, ReinforcedBindingModel model)
{
return new Reinforced()

View File

@ -6,7 +6,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
namespace PrecastConcretePlantDataBaseImplement.Models
{
public class ReinforcedComponent
{

View File

@ -0,0 +1,110 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace PrecastConcretePlantDataBaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime DateOpening { get; set; }
[Required]
public int ReinforcedsMax { get; set; }
private Dictionary<int, (IReinforcedModel, int)>? _shopReinforceds = null;
[NotMapped]
public Dictionary<int, (IReinforcedModel, int)> ShopReinforceds
{
get
{
if (_shopReinforceds == null)
{
_shopReinforceds = Reinforceds
.ToDictionary(x => x.ReinforcedId, x => (x.Reinforced as IReinforcedModel, x.Count));
}
return _shopReinforceds;
}
}
//3ий вопрос
[ForeignKey("ShopId")]//список записей класса-связи
public virtual List<ShopReinforced> Reinforceds { get; set; } = new();
public static Shop Create(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
ReinforcedsMax = model.ReinforcedsMax,
Reinforceds = model.ShopReinforceds.Select(x => new ShopReinforced
{
Reinforced = context.Reinforceds.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
ReinforcedsMax = model.ReinforcedsMax;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
ReinforcedsMax = ReinforcedsMax,
ShopReinforceds = ShopReinforceds
};
public void UpdateReinforceds(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
var shopReinforceds = context.ShopReinforceds.Where(rec => rec.ShopId == model.Id).ToList();
if (shopReinforceds != null && shopReinforceds.Count > 0)
{
context.ShopReinforceds.RemoveRange(shopReinforceds.Where(rec => !model.ShopReinforceds.ContainsKey(rec.ReinforcedId)));
context.SaveChanges();
foreach (var updateReinforced in shopReinforceds)
{
if (model.ShopReinforceds.ContainsKey(updateReinforced.ReinforcedId))
{
updateReinforced.Count = model.ShopReinforceds[updateReinforced.ReinforcedId].Item2;
model.ShopReinforceds.Remove(updateReinforced.ReinforcedId);
}
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var ic in model.ShopReinforceds)
{
context.ShopReinforceds.Add(new ShopReinforced
{
Shop = shop,
Reinforced = context.Reinforceds.First(x => x.Id == ic.Key),
Count = ic.Value.Item2
});
context.SaveChanges();
}
_shopReinforceds = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace PrecastConcretePlantDataBaseImplement.Models
{
public class ShopReinforced
{
public int Id { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int ReinforcedId { get; set; }
[Required]
public int Count { get; set; }
public virtual Reinforced Reinforced { get; set; } = new();
public virtual Shop Shop { get; set; } = new();
}
}

View File

@ -1,5 +1,5 @@
using Microsoft.EntityFrameworkCore;
using PrecastConcretePlantDatabaseImplement.Models;
using PrecastConcretePlantDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -7,7 +7,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement
namespace PrecastConcretePlantDataBaseImplement
{
public class PrecastConcretePlantDataBase : DbContext
{
@ -15,7 +15,7 @@ namespace PrecastConcretePlantDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=PRETTYNAME;Initial Catalog=PrecastConcretePlantDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
optionsBuilder.UseSqlServer(@"Data Source=PRETTYNAME;Initial Catalog=PrecastConcretePlantDataBaseHard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
@ -23,6 +23,7 @@ namespace PrecastConcretePlantDatabaseImplement
public virtual DbSet<Reinforced> Reinforceds { set; get; }
public virtual DbSet<ReinforcedComponent> ReinforcedComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopReinforced> ShopReinforceds { set; get; }
}
}

View File

@ -27,7 +27,7 @@
<ItemGroup>
<ProjectReference Include="..\PrecastConcretePlantBusinessLogic\PrecastConcretePlantBusinessLogic.csproj" />
<ProjectReference Include="..\PrecastConcretePlantContracts\PrecastConcretePlantContracts.csproj" />
<ProjectReference Include="..\PrecastConcretePlantDatabaseImplement\PrecastConcretePlantDatabaseImplement.csproj" />
<ProjectReference Include="..\PrecastConcretePlantDataBaseImplement\PrecastConcretePlantDataBaseImplement.csproj" />
<ProjectReference Include="..\PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj" />
<ProjectReference Include="..\PrecastConcretePlantListImplement\PrecastConcretePlantListImplement.csproj" />
</ItemGroup>

View File

@ -3,6 +3,9 @@ using PrecastConcretePlantBusinessLogic.BusinessLogics;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantFileImplement.Implements;
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
namespace PrecastConcretePlantView
{