From a10bd7a5ad5d4c2cde1182ed6cffbdfd88a173c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 15 May 2024 11:26:56 +0400 Subject: [PATCH] =?UTF-8?q?=D1=83=D1=80=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/SaleStorage.cs | 10 +++++++++- .../Implements/SupplyStorage.cs | 8 +++++++- ...er.cs => 20240515071016_FirstMigration.Designer.cs} | 2 +- ...stMigration.cs => 20240515071016_FirstMigration.cs} | 0 4 files changed, 17 insertions(+), 3 deletions(-) rename Storehouse/StorehouseDatabaseImplement/Migrations/{20240515061146_FirstMigration.Designer.cs => 20240515071016_FirstMigration.Designer.cs} (99%) rename Storehouse/StorehouseDatabaseImplement/Migrations/{20240515061146_FirstMigration.cs => 20240515071016_FirstMigration.cs} (100%) diff --git a/Storehouse/StorehouseDatabaseImplement/Implements/SaleStorage.cs b/Storehouse/StorehouseDatabaseImplement/Implements/SaleStorage.cs index 3280f5f..cb066a4 100644 --- a/Storehouse/StorehouseDatabaseImplement/Implements/SaleStorage.cs +++ b/Storehouse/StorehouseDatabaseImplement/Implements/SaleStorage.cs @@ -4,6 +4,8 @@ using StorehouseContracts.SearchModels; using StorehouseContracts.StoragesContracts; using StorehouseContracts.ViewModels; using StorehouseDatabaseImplement.Models; +using StorehouseDataModels.Models; +using System.ComponentModel.DataAnnotations; namespace StorehouseDatabaseImplement.Implements { @@ -41,12 +43,18 @@ namespace StorehouseDatabaseImplement.Implements public SaleViewModel? Insert(SaleBindingModel model) { + using var context = new StorehouseDatabase(); + var product = context.Products.FirstOrDefault(p => p.Id == model.ProductId); + if (product == null || model.SaleNum > product.ProductNum) + { + return null; + } var newSale = Sale.Create(model); if (newSale == null) { return null; } - using var context = new StorehouseDatabase(); + product.ProductNum -= model.SaleNum; context.Sales.Add(newSale); context.SaveChanges(); return context.Sales.Include(x => x.Product).Include(x => x.Client).FirstOrDefault(x => x.Id == newSale.Id)?.GetViewModel; diff --git a/Storehouse/StorehouseDatabaseImplement/Implements/SupplyStorage.cs b/Storehouse/StorehouseDatabaseImplement/Implements/SupplyStorage.cs index 9c9a8b2..d2dcb0b 100644 --- a/Storehouse/StorehouseDatabaseImplement/Implements/SupplyStorage.cs +++ b/Storehouse/StorehouseDatabaseImplement/Implements/SupplyStorage.cs @@ -41,12 +41,18 @@ namespace StorehouseDatabaseImplement.Implements public SupplyViewModel? Insert(SupplyBindingModel model) { + using var context = new StorehouseDatabase(); + var product = context.Products.FirstOrDefault(p => p.Id == model.ProductId); + if (product == null) + { + return null; + } var newSupply = Supply.Create(model); if (newSupply == null) { return null; } - using var context = new StorehouseDatabase(); + product.ProductNum += model.SupplyNum; context.Supplys.Add(newSupply); context.SaveChanges(); return context.Supplys.Include(x => x.Product).Include(x => x.Provider).FirstOrDefault(x => x.Id == newSupply.Id)?.GetViewModel; diff --git a/Storehouse/StorehouseDatabaseImplement/Migrations/20240515061146_FirstMigration.Designer.cs b/Storehouse/StorehouseDatabaseImplement/Migrations/20240515071016_FirstMigration.Designer.cs similarity index 99% rename from Storehouse/StorehouseDatabaseImplement/Migrations/20240515061146_FirstMigration.Designer.cs rename to Storehouse/StorehouseDatabaseImplement/Migrations/20240515071016_FirstMigration.Designer.cs index 7723208..79031fd 100644 --- a/Storehouse/StorehouseDatabaseImplement/Migrations/20240515061146_FirstMigration.Designer.cs +++ b/Storehouse/StorehouseDatabaseImplement/Migrations/20240515071016_FirstMigration.Designer.cs @@ -11,7 +11,7 @@ using StorehouseDatabaseImplement; namespace StorehouseDatabaseImplement.Migrations { [DbContext(typeof(StorehouseDatabase))] - [Migration("20240515061146_FirstMigration")] + [Migration("20240515071016_FirstMigration")] partial class FirstMigration { /// diff --git a/Storehouse/StorehouseDatabaseImplement/Migrations/20240515061146_FirstMigration.cs b/Storehouse/StorehouseDatabaseImplement/Migrations/20240515071016_FirstMigration.cs similarity index 100% rename from Storehouse/StorehouseDatabaseImplement/Migrations/20240515061146_FirstMigration.cs rename to Storehouse/StorehouseDatabaseImplement/Migrations/20240515071016_FirstMigration.cs