diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs index 0435ff5..942d003 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs @@ -101,7 +101,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic _logger.LogInformation($"CostItem. ID:{model.ID}.EmployeeID:{model.EmployeeID}.Name:{model.Name}.Price:{model.Price}" + $"CostNum:{model.CostNum}"); var element = _storage.GetElement(new CostItemSearchModel { Name = model.Name }); - if (element != null && element.Name == model.Name) + if (element != null && element.Name == model.Name && element.ID != model.ID) { throw new InvalidOperationException("Такая статья затрат уде есть"); } diff --git a/ElectronicsShop/ElectronicsShopContracts/SearchModels/ProductSearchModel.cs b/ElectronicsShop/ElectronicsShopContracts/SearchModels/ProductSearchModel.cs index 43a0e67..a8af438 100644 --- a/ElectronicsShop/ElectronicsShopContracts/SearchModels/ProductSearchModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/SearchModels/ProductSearchModel.cs @@ -11,7 +11,6 @@ namespace ElectronicsShopContracts.SearchModels { public int? ID { get; set; } public string? ProductName { get; set; } - public string? CostItem { get; set; } - public PaymeantOption? PayOption { get; set; } + public int? CostItemID { get; set; } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs index a4c3d3b..62ba372 100644 --- a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs +++ b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs @@ -12,7 +12,7 @@ namespace ElectronicsShopContracts.StorageContracts public interface IProductStorage { List GetFullList(); - List GetFilteredList(ProductSearchModel model); + List? GetFilteredList(ProductSearchModel model); ProductViewModel? GetElement(ProductSearchModel model); ProductViewModel? Insert(ProductBindingModel model); ProductViewModel? Update(ProductBindingModel model); diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs index 96d263c..086307a 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs @@ -57,25 +57,36 @@ namespace ElectronicsShopDataBaseImplement.Implements public ProductViewModel? GetElement(ProductSearchModel model) { - if (string.IsNullOrEmpty(model.ProductName) && !model.ID.HasValue) - { - return null; - } using var context = new Database(); - return context.Products - .Include(x => x.CostItem) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName) || - (model.ID.HasValue && x.ID == model.ID))?.GetViewModel; + + if (model.ID.HasValue && !string.IsNullOrEmpty(model.ProductName)) { + return context.Products + .Include(x => x.CostItem) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName) || + (model.ID.HasValue && x.ID == model.ID))?.GetViewModel; + } + else if (model.CostItemID.HasValue) { + return context.Products + .Include(x => x.CostItem) + .FirstOrDefault(x => model.CostItemID == x.CostItemID)?.GetViewModel; + + } + return null; } - public List GetFilteredList(ProductSearchModel model) + public List? GetFilteredList(ProductSearchModel model) { - if (string.IsNullOrEmpty(model.ProductName)) - { - return new(); - } using var context = new Database(); - return context.Products.Where(x => x.ProductName.Contains(model.ProductName)).Select(x => x.GetViewModel).ToList(); + + if (model.CostItemID.HasValue && string.IsNullOrEmpty(model.ProductName)) { + return context.Products + .Where(x => x.CostItemID == model.CostItemID) + .Select(x => x.GetViewModel).ToList(); + } + else if (!string.IsNullOrEmpty(model.ProductName) && model.ID.HasValue) { + return context.Products.Where(x => x.ProductName.Contains(model.ProductName)).Select(x => x.GetViewModel).ToList(); + } + return null; } public List GetFullList() diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs index 6e8c886..496d352 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs @@ -1,4 +1,5 @@ using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.ViewModels; using ElectronicsShopEmployeeApp.Models; using Microsoft.AspNetCore.Mvc; @@ -145,6 +146,15 @@ namespace ElectronicsShopEmployeeApp.Controllers { if (price <= 0) { throw new Exception(" 0"); } + + var _costitem = APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={_ID}"); + + if (_costitem == null) { + throw new Exception(" "); + } + + double oldPrice = _costitem.Price; + APIEmployee.PostRequest("api/employee/editcostitem", new CostItemBindingModel { ID = _ID, Name = name, @@ -152,12 +162,32 @@ namespace ElectronicsShopEmployeeApp.Controllers { CostNum = costNum, EmployeeID = APIEmployee.Employee.ID }); + + // costitemid + // + // + // + + var productList = APIEmployee.GetRequset>($"api/main/getproducts?_costitemid={_ID}"); + if (productList != null) { + foreach (var item in productList) { + APIEmployee.PostRequest("api/employee/editproduct", new ProductBindingModel { + ID = item.ID, + CostItemID = item.CostItemID, + ProductName = item.ProductName, + Price = Calc(item.CostItemID, item.Price - oldPrice) + }); + } + } + + + Response.Redirect("CostItem"); } [HttpGet] public IActionResult CreateProduct() { - ViewBag.CostItems = APIEmployee.GetRequset>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}"); + ViewBag.CostItems = APIEmployee.GetRequset>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee?.ID}"); return View(); } @@ -182,7 +212,12 @@ namespace ElectronicsShopEmployeeApp.Controllers { var _costItem = APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={id}"); if (_costItem == null) { - return Redirect("/Home/Index"); + throw new Exception(" "); + } + + var _product = APIEmployee.GetRequset($"api/main/getproduct?_costitemID={_costItem.ID}"); + if (_product != null) { + throw new Exception(" "); } APIEmployee.PostRequest("api/employee/deletecostitem", new CostItemBindingModel { ID = id }); @@ -192,8 +227,15 @@ namespace ElectronicsShopEmployeeApp.Controllers { [HttpGet] public IActionResult EditProduct(int id) { var _product = APIEmployee.GetRequset($"api/main/getproduct?_productid={id}"); + if (_product == null) { + throw new Exception(" "); + } + + var _costitem = APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={_product.CostItemID}"); + + if (_costitem == null) { return Redirect("/Home/Index"); } @@ -202,7 +244,7 @@ namespace ElectronicsShopEmployeeApp.Controllers { var obj = new ProductViewModel { ProductName = _product.ProductName, CostItemID = _product.CostItemID, - Price = _product.Price, + Price = _product.Price - _costitem.Price, ID = _product.ID }; @@ -234,7 +276,7 @@ namespace ElectronicsShopEmployeeApp.Controllers { } [HttpPost] - public void EditProduct(string name, int costitem, double productprice, int _ID, double price) { + public void EditProduct(string name, int costitem, int _ID, double price, double productprice) { if (APIEmployee.Employee == null) { throw new Exception(" "); } @@ -261,7 +303,18 @@ namespace ElectronicsShopEmployeeApp.Controllers { [HttpPost] public double Calc(int costitem, double productprice) { var _costItem = APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={costitem}"); - return productprice + (_costItem?.Price ?? 500); + return productprice + (_costItem?.Price ?? 0); + } + + [HttpPost] + public double CalcReload(int costitem, int productid) { + var _costItem = APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={costitem}"); + var product = APIEmployee.GetRequset($"api/main/getproduct?_productid={productid}"); + + if (product == null) { + throw new Exception(" "); + } + return product.Price + (_costItem?.Price ?? 0); } } } diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml index f32a31d..865f1c2 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml @@ -61,7 +61,7 @@ Изменить - Удалить + Удалить } diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/EditProduct.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/EditProduct.cshtml index cd86060..ca77d0b 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/EditProduct.cshtml +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/EditProduct.cshtml @@ -50,13 +50,15 @@