From 139d12d7e773fb04dcd1129335a2657245d7d7d1 Mon Sep 17 00:00:00 2001 From: Kate Date: Sun, 21 May 2023 15:02:08 +0300 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=BB=D1=803?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 12 +- PrecastConcreteFileImplement/Models/Shop.cs | 8 +- PrecastConcretePlant/FormShop.cs | 4 +- PrecastConcretePlant/FormShops.cs | 2 +- .../BusinessLogic/ShopLogic.cs | 14 +- .../BindingModels/ShopBindingModel.cs | 2 +- .../ViewModels/ShopViewModel.cs | 2 +- .../Implements/ShopStorage.cs | 154 +++++++++++ .../20230521120118_addshop.Designer.cs | 248 ++++++++++++++++++ .../Migrations/20230521120118_addshop.cs | 78 ++++++ ...ecastConcretePlantDatabaseModelSnapshot.cs | 77 ++++++ .../Models/Shop.cs | 100 +++++++ .../Models/ShopReinforced.cs | 27 ++ .../PrecastConcretePlantDatabase.cs | 4 +- .../Models/IShopModel.cs | 2 +- .../Models/Shop.cs | 8 +- 16 files changed, 714 insertions(+), 28 deletions(-) create mode 100644 PrecastConcretePlantDataBaseImplemet/Implements/ShopStorage.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Migrations/20230521120118_addshop.Designer.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Migrations/20230521120118_addshop.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Models/Shop.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Models/ShopReinforced.cs diff --git a/PrecastConcreteFileImplement/Implements/ShopStorage.cs b/PrecastConcreteFileImplement/Implements/ShopStorage.cs index a689110..7889c01 100644 --- a/PrecastConcreteFileImplement/Implements/ShopStorage.cs +++ b/PrecastConcreteFileImplement/Implements/ShopStorage.cs @@ -84,23 +84,23 @@ namespace PrecastConcretePlantFileImplement.Implements } public bool SellReinforceds(IReinforcedModel model, int count) { - int availableQuantity = source.Shops.Select(x => x.Reinforceds.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum(); + int availableQuantity = source.Shops.Select(x => x.ShopReinforceds.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum(); if (availableQuantity < count) { return false; } - var shops = source.Shops.Where(x => x.Reinforceds.ContainsKey(model.Id)); + var shops = source.Shops.Where(x => x.ShopReinforceds.ContainsKey(model.Id)); foreach (var shop in shops) { - int countInCurrentShop = shop.Reinforceds[model.Id].Item2; + int countInCurrentShop = shop.ShopReinforceds[model.Id].Item2; if (countInCurrentShop <= count) { - shop.Reinforceds[model.Id] = (shop.Reinforceds[model.Id].Item1, 0); + shop.ShopReinforceds[model.Id] = (shop.ShopReinforceds[model.Id].Item1, 0); count -= countInCurrentShop; } else { - shop.Reinforceds[model.Id] = (shop.Reinforceds[model.Id].Item1, countInCurrentShop - count); + shop.ShopReinforceds[model.Id] = (shop.ShopReinforceds[model.Id].Item1, countInCurrentShop - count); count = 0; } Update(new ShopBindingModel @@ -109,7 +109,7 @@ namespace PrecastConcretePlantFileImplement.Implements ShopName = shop.ShopName, Address = shop.Address, DateOpening = shop.DateOpening, - Reinforceds = shop.Reinforceds, + ShopReinforceds = shop.ShopReinforceds, Capacity = shop.Capacity }); if (count == 0) diff --git a/PrecastConcreteFileImplement/Models/Shop.cs b/PrecastConcreteFileImplement/Models/Shop.cs index 29e439c..475249d 100644 --- a/PrecastConcreteFileImplement/Models/Shop.cs +++ b/PrecastConcreteFileImplement/Models/Shop.cs @@ -20,7 +20,7 @@ namespace PrecastConcretePlantFileImplement.Models public int Capacity { get; private set; } public Dictionary ReinforcedsCount = new(); public Dictionary? _reinforceds = null; - public Dictionary Reinforceds + public Dictionary ShopReinforceds { get { @@ -49,7 +49,7 @@ namespace PrecastConcretePlantFileImplement.Models Address = model.Address, DateOpening = model.DateOpening, Capacity = model.Capacity, - ReinforcedsCount = model.Reinforceds.ToDictionary(x => x.Key, x => x.Value.Item2) + ReinforcedsCount = model.ShopReinforceds.ToDictionary(x => x.Key, x => x.Value.Item2) }; } public static Shop? Create(XElement element) @@ -81,7 +81,7 @@ namespace PrecastConcretePlantFileImplement.Models Address = model.Address; DateOpening = model.DateOpening; Capacity = model.Capacity; - ReinforcedsCount = model.Reinforceds.ToDictionary(x => x.Key, x => x.Value.Item2); + ReinforcedsCount = model.ShopReinforceds.ToDictionary(x => x.Key, x => x.Value.Item2); _reinforceds = null; } @@ -92,7 +92,7 @@ namespace PrecastConcretePlantFileImplement.Models Address = Address, DateOpening = DateOpening, Capacity = Capacity, - Reinforceds = Reinforceds + ShopReinforceds = ShopReinforceds }; public XElement GetXElement => new("Shop", new XAttribute("Id", Id), diff --git a/PrecastConcretePlant/FormShop.cs b/PrecastConcretePlant/FormShop.cs index bf70aa8..70545da 100644 --- a/PrecastConcretePlant/FormShop.cs +++ b/PrecastConcretePlant/FormShop.cs @@ -48,7 +48,7 @@ namespace PrecastConcretePlantView textBoxAddress.Text = view.Address; dateTimePicker.Text = view.DateOpening.ToString(); numericUpDownCapacity.Value = view.Capacity; - _shopReinforceds = view.Reinforceds ?? new Dictionary(); + _shopReinforceds = view.ShopReinforceds ?? new Dictionary(); LoadData(); } } @@ -103,7 +103,7 @@ namespace PrecastConcretePlantView Address = textBoxAddress.Text, DateOpening = dateTimePicker.Value.Date, Capacity = (int)numericUpDownCapacity.Value, - Reinforceds = _shopReinforceds + ShopReinforceds = _shopReinforceds }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) diff --git a/PrecastConcretePlant/FormShops.cs b/PrecastConcretePlant/FormShops.cs index 809e50c..8a076dc 100644 --- a/PrecastConcretePlant/FormShops.cs +++ b/PrecastConcretePlant/FormShops.cs @@ -37,7 +37,7 @@ namespace PrecastConcretePlantView { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["Reinforceds"].Visible = false; + dataGridView.Columns["ShopReinforceds"].Visible = false; dataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка магазинов"); diff --git a/PrecastConcretePlantBusinessLogic/BusinessLogic/ShopLogic.cs b/PrecastConcretePlantBusinessLogic/BusinessLogic/ShopLogic.cs index 57691ce..1891c6c 100644 --- a/PrecastConcretePlantBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/PrecastConcretePlantBusinessLogic/BusinessLogic/ShopLogic.cs @@ -124,21 +124,21 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic _logger.LogWarning("AddReinforcedInShop element not found"); return false; } - if (shop.Capacity - shop.Reinforceds.Select(x => x.Value.Item2).Sum() < count) + if (shop.Capacity - shop.ShopReinforceds.Select(x => x.Value.Item2).Sum() < count) { throw new ArgumentNullException("В магазине не хватает места", nameof(count)); } else _logger.LogInformation("AddReinforcedInShop find. Id:{Id}", shop.Id); - if (shop.Reinforceds.ContainsKey(reinforced.Id)) + if (shop.ShopReinforceds.ContainsKey(reinforced.Id)) { - shop.Reinforceds[reinforced.Id] = (reinforced, count + shop.Reinforceds[reinforced.Id].Item2); + shop.ShopReinforceds[reinforced.Id] = (reinforced, count + shop.ShopReinforceds[reinforced.Id].Item2); _logger.LogInformation("AddReinforcedInShop. Added {count} {reinforced} to '{ShopName}' shop", count, reinforced.ReinforcedName, shop.ShopName); } else { - shop.Reinforceds[reinforced.Id] = (reinforced, count); + shop.ShopReinforceds[reinforced.Id] = (reinforced, count); _logger.LogInformation("AddReinforcedInShop. Added {count} new reinforced {reinforced} to '{ShopName}' shop", count, reinforced.ReinforcedName, shop.ShopName); } @@ -148,7 +148,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic Address = shop.Address, ShopName = shop.ShopName, DateOpening = shop.DateOpening, - Reinforceds = shop.Reinforceds, + ShopReinforceds = shop.ShopReinforceds, Capacity = shop.Capacity }); return true; @@ -164,7 +164,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic throw new ArgumentException("Количество ЖБИ должно быть больше 0", nameof(count)); } _logger.LogInformation("AddReinforceds. ShopName:{ShopName}. Id:{Id}", model.ReinforcedName, model.Id); - var allFreeQuantity = _shopStorage.GetFullList().Select(x => x.Capacity - x.Reinforceds.Select(x => x.Value.Item2).Sum()).Sum(); + var allFreeQuantity = _shopStorage.GetFullList().Select(x => x.Capacity - x.ShopReinforceds.Select(x => x.Value.Item2).Sum()).Sum(); if (allFreeQuantity < count) { _logger.LogWarning("AddReinforceds operation failed."); @@ -172,7 +172,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic } foreach (var shop in _shopStorage.GetFullList()) { - int freeQuantity = shop.Capacity - shop.Reinforceds.Select(x => x.Value.Item2).Sum(); + int freeQuantity = shop.Capacity - shop.ShopReinforceds.Select(x => x.Value.Item2).Sum(); if (freeQuantity < count) { if (!AddReinforcedInShop(new() { Id = shop.Id }, model, freeQuantity)) diff --git a/PrecastConcretePlantContracts/BindingModels/ShopBindingModel.cs b/PrecastConcretePlantContracts/BindingModels/ShopBindingModel.cs index 5845852..3e9e52d 100644 --- a/PrecastConcretePlantContracts/BindingModels/ShopBindingModel.cs +++ b/PrecastConcretePlantContracts/BindingModels/ShopBindingModel.cs @@ -18,7 +18,7 @@ namespace PrecastConcretePlantContracts.BindingModels public int Capacity { get; set; } - public Dictionary Reinforceds + public Dictionary ShopReinforceds { get; set; diff --git a/PrecastConcretePlantContracts/ViewModels/ShopViewModel.cs b/PrecastConcretePlantContracts/ViewModels/ShopViewModel.cs index d27e432..c63dbfb 100644 --- a/PrecastConcretePlantContracts/ViewModels/ShopViewModel.cs +++ b/PrecastConcretePlantContracts/ViewModels/ShopViewModel.cs @@ -22,7 +22,7 @@ namespace PrecastConcretePlantContracts.ViewModels [DisplayName("Вместимость магазина")] public int Capacity { get; set; } - public Dictionary Reinforceds + public Dictionary ShopReinforceds { get; set; diff --git a/PrecastConcretePlantDataBaseImplemet/Implements/ShopStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/ShopStorage.cs new file mode 100644 index 0000000..d54d059 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Implements/ShopStorage.cs @@ -0,0 +1,154 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List 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 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.UpdateTravels(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 SellReinforceds(IReinforcedModel model, int count) + { + using var context = new PrecastConcretePlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.ShopReinforceds + .Include(x => x.Shop) + .ToList() + .Where(rec => rec.ReinforcedId == model.Id); + if (shops == null) + { + return false; + } + foreach (var shop in shops) + { + if (shop.Count < count) + { + shop.Count = 0; + count -= shop.Count; + } + else + { + shop.Count = shop.Count - count; + count -= count; + } + if (count == 0) + { + + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Migrations/20230521120118_addshop.Designer.cs b/PrecastConcretePlantDataBaseImplemet/Migrations/20230521120118_addshop.Designer.cs new file mode 100644 index 0000000..8c72d31 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Migrations/20230521120118_addshop.Designer.cs @@ -0,0 +1,248 @@ +// +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("20230521120118_addshop")] + partial class addshop + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ReinforcedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Reinforceds"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("ReinforcedComponents"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ShopReinforced", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ReinforcedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopReinforceds"); + }); + + modelBuilder.Entity("Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Capacity") + .HasColumnType("int"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + 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() + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("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"); + }); + + modelBuilder.Entity("Shop", b => + { + b.Navigation("Reinforceds"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Migrations/20230521120118_addshop.cs b/PrecastConcretePlantDataBaseImplemet/Migrations/20230521120118_addshop.cs new file mode 100644 index 0000000..0d755fc --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Migrations/20230521120118_addshop.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PrecastConcretePlantDatabaseImplement.Migrations +{ + /// + public partial class addshop : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + Capacity = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopReinforceds", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopId = table.Column(type: "int", nullable: false), + ReinforcedId = table.Column(type: "int", nullable: false), + Count = table.Column(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"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopReinforceds"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs b/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs index ff469be..765c885 100644 --- a/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs +++ b/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.ToTable("ReinforcedComponents"); }); + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ShopReinforced", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ReinforcedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopReinforceds"); + }); + + modelBuilder.Entity("Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Capacity") + .HasColumnType("int"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => { b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") @@ -151,6 +204,25 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.Navigation("Reinforced"); }); + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ShopReinforced", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + .WithMany() + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("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"); @@ -162,6 +234,11 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("Shop", b => + { + b.Navigation("Reinforceds"); + }); #pragma warning restore 612, 618 } } diff --git a/PrecastConcretePlantDataBaseImplemet/Models/Shop.cs b/PrecastConcretePlantDataBaseImplemet/Models/Shop.cs new file mode 100644 index 0000000..296f2ae --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Models/Shop.cs @@ -0,0 +1,100 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDatabaseImplement; +using PrecastConcretePlantDatabaseImplement.Models; +using PrecastConcretePlantDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +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 Capacity { get; set; } + + private Dictionary? _shopReinforceds = null; + + [NotMapped] + public Dictionary ShopReinforceds + { + get + { + if (_shopReinforceds == null) + { + _shopReinforceds = Reinforceds.ToDictionary(rec => rec.ReinforcedId, rec => (rec.Reinforced as IReinforcedModel, rec.Count)); + } + return _shopReinforceds; + } + } + [ForeignKey("ShopId")] + public virtual List 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, + Capacity = model.Capacity, + 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; + Capacity = model.Capacity; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + Capacity = Capacity, + ShopReinforceds = ShopReinforceds + }; + + public void UpdateTravels(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 updateTravel in shopReinforceds) + { + updateTravel.Count = model.ShopReinforceds[updateTravel.ReinforcedId].Item2; + model.ShopReinforceds.Remove(updateTravel.ReinforcedId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var st in model.ShopReinforceds) + { + context.ShopReinforceds.Add(new ShopReinforced + { + Shop = shop, + Reinforced = context.Reinforceds.First(x => x.Id == st.Key), + Count = st.Value.Item2 + }); + context.SaveChanges(); + } + _shopReinforceds = null; + } +} \ No newline at end of file diff --git a/PrecastConcretePlantDataBaseImplemet/Models/ShopReinforced.cs b/PrecastConcretePlantDataBaseImplemet/Models/ShopReinforced.cs new file mode 100644 index 0000000..da0d9d5 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Models/ShopReinforced.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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(); + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs index 8cf3f6c..20f9d65 100644 --- a/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs +++ b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs @@ -15,7 +15,7 @@ namespace PrecastConcretePlantDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-EJNKGL2;Initial Catalog= PrecastConcretePlantDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0IOO2C0;Initial Catalog= PrecastConcretePlantDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -23,5 +23,7 @@ namespace PrecastConcretePlantDatabaseImplement public virtual DbSet Reinforceds { set; get; } public virtual DbSet ReinforcedComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopReinforceds { set; get; } } } diff --git a/PrecastConcretePlantDataModels/Models/IShopModel.cs b/PrecastConcretePlantDataModels/Models/IShopModel.cs index 9854c25..ddc05ee 100644 --- a/PrecastConcretePlantDataModels/Models/IShopModel.cs +++ b/PrecastConcretePlantDataModels/Models/IShopModel.cs @@ -12,6 +12,6 @@ namespace PrecastConcretePlantDataModels.Models string Address { get; } DateTime DateOpening { get; } public int Capacity { get; } - Dictionary Reinforceds { get; } + Dictionary ShopReinforceds { get; } } } diff --git a/PrecastConcretePlantListImplement/Models/Shop.cs b/PrecastConcretePlantListImplement/Models/Shop.cs index ccacae7..907238f 100644 --- a/PrecastConcretePlantListImplement/Models/Shop.cs +++ b/PrecastConcretePlantListImplement/Models/Shop.cs @@ -16,7 +16,7 @@ namespace PrecastConcretePlantListImplement.Models public string Address { get; private set; } = string.Empty; public DateTime DateOpening { get; private set; } public int Capacity { get; private set; } - public Dictionary Reinforceds + public Dictionary ShopReinforceds { get; private set; @@ -33,7 +33,7 @@ namespace PrecastConcretePlantListImplement.Models ShopName = model.ShopName, Address = model.Address, DateOpening = model.DateOpening, - Reinforceds = new() + ShopReinforceds = new() }; } public void Update(ShopBindingModel? model) @@ -45,14 +45,14 @@ namespace PrecastConcretePlantListImplement.Models ShopName = model.ShopName; Address = model.Address; DateOpening = model.DateOpening; - Reinforceds = model.Reinforceds; + ShopReinforceds = model.ShopReinforceds; } public ShopViewModel GetViewModel => new() { Id = Id, ShopName = ShopName, Address = Address, - Reinforceds = Reinforceds, + ShopReinforceds = ShopReinforceds, DateOpening = DateOpening, }; }