From e78b0ffdbeb5f556307b14089f1995c7a9576bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=20=D0=A4=D0=B5=D0=B4=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Tue, 28 May 2024 12:50:24 +0400 Subject: [PATCH] Product create --- .../BusinessLogic/CostItemLogic.cs | 4 +- .../SearchModels/CostItemSearchModel.cs | 1 + .../ViewModels/CostItemViewModel.cs | 3 + .../ViewModels/ProductViewModel.cs | 2 + .../Implements/CostItemStorage.cs | 24 +++--- .../Models/CostItem.cs | 3 + .../Models/Product.cs | 8 +- .../Controllers/HomeController.cs | 33 ++++++--- .../Views/Home/CostItem.cshtml | 74 +++++++++++++++++++ .../Views/Home/Create.cshtml | 3 - .../Views/Home/CreateCostItem.cshtml | 14 ---- .../Views/Home/CreateProduct.cshtml | 64 ++++++++++++++++ .../Views/Home/Index.cshtml | 10 +-- .../Views/Shared/_Layout.cshtml | 4 +- .../Controllers/EmployeeController.cs | 51 ++++++++++++- .../Controllers/MainController.cs | 53 ++++--------- 16 files changed, 263 insertions(+), 88 deletions(-) create mode 100644 ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml delete mode 100644 ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/Create.cshtml create mode 100644 ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CreateProduct.cshtml diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs index f0375d8..0435ff5 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs @@ -100,8 +100,8 @@ 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 { ID = model.ID }); - if (element != null && element.ID != model.EmployeeID) + var element = _storage.GetElement(new CostItemSearchModel { Name = model.Name }); + if (element != null && element.Name == model.Name) { throw new InvalidOperationException("Такая статья затрат уде есть"); } diff --git a/ElectronicsShop/ElectronicsShopContracts/SearchModels/CostItemSearchModel.cs b/ElectronicsShop/ElectronicsShopContracts/SearchModels/CostItemSearchModel.cs index ba55a1a..b383d5b 100644 --- a/ElectronicsShop/ElectronicsShopContracts/SearchModels/CostItemSearchModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/SearchModels/CostItemSearchModel.cs @@ -9,5 +9,6 @@ namespace ElectronicsShopContracts.SearchModels { public int? ID { get; set; } public string? Name { get; set; } public int? CostNum { get; set; } + public int? EmployeeID { get; set; } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/ViewModels/CostItemViewModel.cs b/ElectronicsShop/ElectronicsShopContracts/ViewModels/CostItemViewModel.cs index 2388648..070a5cd 100644 --- a/ElectronicsShop/ElectronicsShopContracts/ViewModels/CostItemViewModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/ViewModels/CostItemViewModel.cs @@ -21,5 +21,8 @@ namespace ElectronicsShopContracts.ViewModels { [DisplayName("Номер счета")] public int CostNum { get; set; } + + [DisplayName("Сотрудник")] + public string EmployeeFIO { get; set; } = string.Empty; } } diff --git a/ElectronicsShop/ElectronicsShopContracts/ViewModels/ProductViewModel.cs b/ElectronicsShop/ElectronicsShopContracts/ViewModels/ProductViewModel.cs index 7ce3836..0d67b90 100644 --- a/ElectronicsShop/ElectronicsShopContracts/ViewModels/ProductViewModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/ViewModels/ProductViewModel.cs @@ -20,5 +20,7 @@ namespace ElectronicsShopContracts.ViewModels [DisplayName("Стоимость продукта")] public double Price { get; set; } + [DisplayName("Статья затрат")] + public string CostItemName { get; set; } = string.Empty; } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs index 816065c..12a4925 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs @@ -3,9 +3,11 @@ using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Intrinsics.X86; using System.Text; using System.Threading.Tasks; @@ -21,7 +23,9 @@ namespace ElectronicsShopDataBaseImplement.Implements using var context = new Database(); context.CostItems.Add(newComponent); context.SaveChanges(); - return newComponent.GetViewModel; + return context.CostItems + .Include(x => x.Employee) + .FirstOrDefault(x => x.ID == newComponent.ID)?.GetViewModel; } public CostItemViewModel? Update(CostItemBindingModel model) { @@ -56,20 +60,22 @@ namespace ElectronicsShopDataBaseImplement.Implements } using var context = new Database(); return context.CostItems - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) - && x.Name == model.Name) || + .Include(x => x.Employee) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.ID.HasValue && x.ID == model.ID))?.GetViewModel; } public List GetFillteredList(CostItemSearchModel model) { - if (string.IsNullOrEmpty(model.Name)) - { - return new(); - } using var context = new Database(); - return context.CostItems.Where(x => x.Name.Contains(model.Name)) - .Select(x => x.GetViewModel).ToList(); + if (model.EmployeeID.HasValue) { + return context.CostItems + .Include(x => x.Employee) + .Where(x => x.EmployeeID == model.EmployeeID) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); } public List GetFullList() diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CostItem.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CostItem.cs index af4193e..b31c66a 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CostItem.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CostItem.cs @@ -27,6 +27,8 @@ namespace ElectronicsShopDataBaseImplement.Models [Required] public int CostNum { get; set; } + public virtual Employee Employee { get; set; } + public static CostItem? Create(CostItemBindingModel? model) { if (model == null) @@ -61,6 +63,7 @@ namespace ElectronicsShopDataBaseImplement.Models Name = Name, Price = Price, CostNum = CostNum, + EmployeeFIO = Employee.EmployeeFIO }; } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs index 9fa3923..0ca9adf 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs @@ -12,8 +12,7 @@ using System.Threading.Tasks; namespace ElectronicsShopDataBaseImplement.Models { - public class Product : IProductModel - { + public class Product : IProductModel { public int ID { get; set; } [Required] @@ -23,7 +22,9 @@ namespace ElectronicsShopDataBaseImplement.Models public double Price { get; set; } [ForeignKey("CostItemID")] - public int CostItemID { get; set; } + public int CostItemID { get; set; } + + public virtual CostItem CostItem {get; set;} public static Product? Create(ProductBindingModel? model) { @@ -55,6 +56,7 @@ namespace ElectronicsShopDataBaseImplement.Models ProductName = ProductName, Price = Price, CostItemID = CostItemID, + CostItemName = CostItem.Name }; } } diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs index 7bb35c6..7d782b7 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs @@ -12,6 +12,13 @@ namespace ElectronicsShopEmployeeApp.Controllers { _logger = logger; } + public IActionResult CostItem() { + if (APIEmployee.Employee == null) { + return Redirect("~/Home/Enter"); + } + return View(APIEmployee.GetRequset>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}")); + } + public IActionResult Index() { if (APIEmployee.Employee == null) { return Redirect("~/Home/Enter"); @@ -102,39 +109,41 @@ namespace ElectronicsShopEmployeeApp.Controllers { if (price <= 0) { throw new Exception(" 0"); } - APIEmployee.PostRequest("api/main/createcostitem", new CostItemBindingModel { + APIEmployee.PostRequest("api/employee/createcostitem", new CostItemBindingModel { EmployeeID = APIEmployee.Employee.ID, Name = name, Price = price, CostNum = costNum }); + Response.Redirect("CostItem"); } [HttpGet] - public IActionResult Create() { - ViewBag.Orders = APIEmployee.GetRequset>("api/main/getproductlist"); + public IActionResult CreateProduct() { + ViewBag.CostItems = APIEmployee.GetRequset>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}"); return View(); } [HttpPost] - public void Create(int price, int costNum, string productName) { + public void CreateProduct(string name, int costitem, double productprice) { if (APIEmployee.Employee == null) { throw new Exception(" "); } - if (price <= 0) { + if (productprice <= 0) { throw new Exception(" 0"); } - APIEmployee.PostRequest("api/main/createproduct", new ProductBindingModel { - CostItemID = costNum, - ProductName = productName, - Price = Calc(price, costNum) + APIEmployee.PostRequest("api/employee/createproduct", new ProductBindingModel { + CostItemID = costitem, + ProductName = name, + Price = Calc(costitem, productprice) }); Response.Redirect("Index"); } - private double Calc(int price, int costItem) { - var _costItem = APIEmployee.GetRequset($"api/main/getcostitem?_employeeID={costItem}"); - return price * (_costItem?.Price ?? 1); + [HttpPost] + public double Calc(int costitem, double productprice) { + var _costItem = APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={costitem}"); + return productprice + (_costItem?.Price ?? 500); } } } diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml new file mode 100644 index 0000000..40831f9 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml @@ -0,0 +1,74 @@ +@using ElectronicsShopContracts.ViewModels + +@model List + +@{ + ViewData["Title"] = "CostItem"; +} + +
+

Статьи затрат

+
+ + +
+ @{ + if (Model == null) { +

Авторизируйтесь

+ return; + } + +

+ Создать +

+

+ Изменить +

+

+ Удалить +

+ // todo delete updates + + + + + + + + + + + + @foreach (var item in Model) { + + + + + + + + } + +
+ Номер + + Название + + Сотрудник + + Сумма затрат + + Номер счета +
+ @Html.DisplayFor(modelItem => item.ID) + + @Html.DisplayFor(modelItem => item.Name) + + @Html.DisplayFor(modelItem => item.EmployeeFIO) + + @Html.DisplayFor(modelItem => item.Price) + + @Html.DisplayFor(modelItem => item.CostNum) +
+ } +
\ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/Create.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/Create.cshtml deleted file mode 100644 index 47f177d..0000000 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/Create.cshtml +++ /dev/null @@ -1,3 +0,0 @@ -@{ - ViewData["Title"] = "Create"; -} \ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CreateCostItem.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CreateCostItem.cshtml index d9d921c..1200a0f 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CreateCostItem.cshtml +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CreateCostItem.cshtml @@ -30,18 +30,4 @@ - - - \ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CreateProduct.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CreateProduct.cshtml new file mode 100644 index 0000000..b8a45f3 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CreateProduct.cshtml @@ -0,0 +1,64 @@ +@{ + ViewData["Title"] = "CreateProduct"; +} + +
+

Создание товара

+
+
+
+
Название:
+
+ +
+
+
+
Статья затрат:
+
+ +
+
+
+
Стоимость товара:
+
+ +
+
+
+
С учетом затрат:
+
+ +
+
+
+
+
+ +
+
+ +
+ + \ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/Index.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/Index.cshtml index 2eafdfb..a7b936a 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/Index.cshtml +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/Index.cshtml @@ -18,7 +18,7 @@ return; }

- Создать продукта + Создать Товар

@@ -27,10 +27,10 @@ Номер продукта
- Статья затрат + Название продукта - Название продукта + Статья затрат Сумма @@ -44,10 +44,10 @@ @Html.DisplayFor(modelItem => item.ID) - @Html.DisplayFor(modelItem => item.CostItemID) + @Html.DisplayFor(modelItem => item.ProductName) - @Html.DisplayFor(modelItem => item.ProductName) + @Html.DisplayFor(modelItem => item.CostItemName) @Html.DisplayFor(modelItem => item.Price) diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Shared/_Layout.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Shared/_Layout.cshtml index 5b2ad4b..d09e699 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Shared/_Layout.cshtml +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Shared/_Layout.cshtml @@ -12,7 +12,7 @@