From 883c050eb96a28d710a357858ecf9642e7a7717f Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Fri, 24 Mar 2023 00:15:37 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BB=D0=BE=D0=B1=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=B5=20=D0=BF=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5.=20=D0=90=20=D1=82=D0=BE?= =?UTF-8?q?=20=D1=87=D1=82=D0=BE-=D1=82=D0=BE=20=D1=8F=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B4=D1=83=D1=81=D1=82=D0=B0=D0=BB))((((?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlacksmithWorkshopDatabase.cs | 4 +++ .../Implements/ShopStorage.cs | 22 ++++++------ .../ManufactureShopDatabase.cs | 34 ------------------- .../Models/Shop.cs | 16 ++++----- 4 files changed, 23 insertions(+), 53 deletions(-) delete mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/ManufactureShopDatabase.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs index d4e6e68..bb81317 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs @@ -28,5 +28,9 @@ namespace BlacksmithWorkshopDatabaseImplement public virtual DbSet ManufactureWorkPieces { set; get; } public virtual DbSet Orders { set; get; } + + public virtual DbSet Shops { set; get; } + + public virtual DbSet ShopManufactures { get; set; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs index 5724d6d..77e0003 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs @@ -17,7 +17,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements { public List GetFullList() { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); return context.Shops .Include(x => x.Manufactures) .ThenInclude(x => x.Manufacture) @@ -33,7 +33,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements return new(); } - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); return context.Shops .Include(x => x.Manufactures) @@ -51,7 +51,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements return null; } - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); return context.Shops .Include(x => x.Manufactures) @@ -63,7 +63,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public ShopViewModel? Insert(ShopBindingModel model) { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); var newShop = Shop.Create(context, model); if (newShop == null) @@ -79,7 +79,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public ShopViewModel? Update(ShopBindingModel model) { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); using var transaction = context.Database.BeginTransaction(); try @@ -93,7 +93,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements shop.Update(model); context.SaveChanges(); - shop.UpdateIceCreams(context, model); + shop.UpdateManufactures(context, model); transaction.Commit(); return shop.GetViewModel; @@ -108,7 +108,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public ShopViewModel? Delete(ShopBindingModel model) { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); var element = context.Shops .Include(x => x.Manufactures) @@ -127,15 +127,15 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public bool SellManufactures(IManufactureModel model, int count) { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); using var transaction = context.Database.BeginTransaction(); try { var shops = context.ShopManufactures - .Include(x => x.Shop) - .ToList() - .Where(rec => rec.ManufactureId == model.Id); + .Include(x => x.Shop) + .ToList() + .Where(rec => rec.ManufactureId == model.Id); if (shops == null) { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/ManufactureShopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/ManufactureShopDatabase.cs deleted file mode 100644 index e4e0942..0000000 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/ManufactureShopDatabase.cs +++ /dev/null @@ -1,34 +0,0 @@ -using BlacksmithWorkshopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BlacksmithWorkshopDatabaseImplement -{ - public class ManufactureShopDatabase : DbContext - { - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (optionsBuilder.IsConfigured == false) - { - optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-CFLH20EE\SQLEXPRESS;Initial Catalog=ManufactureShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); - } - base.OnConfiguring(optionsBuilder); - } - - public virtual DbSet WorkPieces { set; get; } - - public virtual DbSet Manufactures { set; get; } - - public virtual DbSet ManufactureWorkPieces { set; get; } - - public virtual DbSet Orders { set; get; } - - public virtual DbSet Shops { set; get; } - - public virtual DbSet ShopManufactures { get; set; } - } -} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs index 46f8fe7..a41c2d3 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs @@ -44,7 +44,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models [ForeignKey("ShopId")] public virtual List Manufactures { get; set; } = new(); - public static Shop? Create(ManufactureShopDatabase context, ShopBindingModel model) + public static Shop? Create(BlacksmithWorkshopDatabase context, ShopBindingModel model) { if (model == null) { @@ -89,20 +89,20 @@ namespace BlacksmithWorkshopDatabaseImplement.Models ShopManufactures = ShopManufactures }; - public void UpdateIceCreams(ManufactureShopDatabase context, ShopBindingModel model) + public void UpdateManufactures(BlacksmithWorkshopDatabase context, ShopBindingModel model) { - var shopIceCreams = context.ShopManufactures.Where(rec => rec.ShopId == model.Id).ToList(); + var shopManufactures = context.ShopManufactures.Where(rec => rec.ShopId == model.Id).ToList(); - if (shopIceCreams != null && shopIceCreams.Count > 0) + if (shopManufactures != null && shopManufactures.Count > 0) { // удалили те, которых нет в модели - context.ShopManufactures.RemoveRange(shopIceCreams.Where(rec => !model.ShopManufactures.ContainsKey(rec.ManufactureId))); + context.ShopManufactures.RemoveRange(shopManufactures.Where(rec => !model.ShopManufactures.ContainsKey(rec.ManufactureId))); context.SaveChanges(); // обновили количество у существующих записей - foreach (var updateIceCream in shopIceCreams) + foreach (var _shopManufactures in shopManufactures) { - updateIceCream.Count = model.ShopManufactures[updateIceCream.ManufactureId].Item2; - model.ShopManufactures.Remove(updateIceCream.ManufactureId); + _shopManufactures.Count = model.ShopManufactures[_shopManufactures.ManufactureId].Item2; + model.ShopManufactures.Remove(_shopManufactures.ManufactureId); } context.SaveChanges();