diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs index 3c2ad25..70c9c48 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -1,9 +1,11 @@ -using ElectronicsShopContracts.BindingModels; +using DocumentFormat.OpenXml.Drawing.Charts; +using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataModels.Enums; +using ElectronicsShopDataModels.Models; using Microsoft.Extensions.Logging; @@ -27,7 +29,8 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic } return true; } - public List? ReadList(OrderSearchModel? model) + + public List? ReadList(OrderSearchModel? model) { _logger.LogInformation($"ReadList:ID:{model?.ID}"); var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ; @@ -46,10 +49,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic if (string.IsNullOrEmpty((model.ID).ToString())) { throw new ArgumentNullException("Нет ID заказа", nameof(model.ID)); } - if (model.Sum <= 0) {////byda - throw new ArgumentNullException("Цена зака должна быть больше 0", nameof(model.Sum)); - } - if (model.ClientID < 0) { + if (string.IsNullOrEmpty(model.ClientID.ToString())) { throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientID)); } _logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.DateCreate:{model.DateCreate}.ClientID:{model.ClientID}"); @@ -69,5 +69,28 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic return element; } + public bool AddProduct(IProductModel product, int count, int orderID) { + var _order = ReadElement(new OrderSearchModel { ID = orderID }); + try { + if (_order.ProductList.ContainsKey(product.ID)) { + _order.ProductList[product.ID] = (product, count); + } + else { + _order.ProductList.Add(product.ID, (product, count)); + _storage.Update(new OrderBindingModel { + ClientID = _order.ClientID, + ProductList = _order.ProductList, + Sum = 5000, + DateCreate = DateTime.Now, + ID = _order.ID, + }); + } + } + catch { + _logger.LogWarning("AddProduct. operation is failed"); + return false; + } + return true; + } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs index e1e5cc6..ffc04a6 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs @@ -1,9 +1,11 @@ using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataModels.Models; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; using System.Threading.Tasks; @@ -15,5 +17,6 @@ namespace ElectronicsShopContracts.BusinessLogicContracts OrderViewModel? ReadElement(OrderSearchModel model); bool CreateOrder(OrderBindingModel model); + bool AddProduct(IProductModel product, int count, int orderID); } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs index a7b0037..d13eb78 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs @@ -55,16 +55,23 @@ namespace ElectronicsShopDataBaseImplement.Implements } public OrderViewModel? GetElement(OrderSearchModel model) { - if (!model.ID.HasValue) + using var context = new Database(); + if (model.ClientID.HasValue) { + return context.Orders + .Include(x => x.Payments) + .Include(x => x.Products) + .ThenInclude(x => x._product) + .FirstOrDefault(x => (model.ClientID.HasValue && x.ClientID == model.ClientID))?.GetViewModel; + } + if (model.ID.HasValue) { - return null; - } - using var context = new Database(); - return context.Orders - .Include(x => x.Payments) - .Include(x => x.Products) - .ThenInclude(x => x._product) - .FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel; + return context.Orders + .Include(x => x.Payments) + .Include(x => x.Products) + .ThenInclude(x => x._product) + .FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel; + } + return null; } public List GetFilteredList(OrderSearchModel model) diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs index 0d71f41..d1e15b4 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs @@ -66,7 +66,7 @@ namespace ElectronicsShopRestAPI.Controllers { _costItem.Create(model); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка создания заказа"); + _logger.LogError(ex, "Ошибка создания статьи"); throw; } } diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs index 00fb832..38b9be9 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs @@ -5,6 +5,7 @@ using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataBaseImplement.Models; using ElectronicsShopDataModels.Models; using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; namespace ElectronicsShopRestAPI.Controllers { @@ -16,14 +17,12 @@ namespace ElectronicsShopRestAPI.Controllers { private readonly IProductLogic _product; private readonly IOrderLogic _order; private readonly IMessageInfoLogic _message; - public MainController(ILogger logger, IProductLogic product, IOrderLogic orderLogic) { - _logger = logger; + _logger = logger; _product = product; _order = orderLogic; - _ProductList = new Dictionary(); } [HttpGet] @@ -79,24 +78,31 @@ namespace ElectronicsShopRestAPI.Controllers { } } + [HttpPost] + public void CreateOrder(OrderBindingModel model) { + try { + _order.CreateOrder(model); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка создания заказа"); + throw; + } + } [HttpPost] - public void AddProduct(OrderBindingModel model) + public void AddProduct(List jslist) { - try - { - var order=_order.ReadElement(new OrderSearchModel { ID = model.ID });//возвращает null - if (model != null&& order!=null) - { - order.ProductList = model.ProductList; - order.Sum += model.Sum; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка создания заказа"); - throw; - } + var product = JsonConvert.DeserializeObject(jslist[0]); + int count = JsonConvert.DeserializeObject(jslist[1]); + int orderid = JsonConvert.DeserializeObject(jslist[2]); + + try { + _order.AddProduct(product, count, orderid); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка добавления заказа"); + throw; + } } } } diff --git a/ElectronicsShop/ElectronicsShopRestAPI/ElectronicsShopRestAPI.csproj b/ElectronicsShop/ElectronicsShopRestAPI/ElectronicsShopRestAPI.csproj index 9cc54fc..ddb68ec 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/ElectronicsShopRestAPI.csproj +++ b/ElectronicsShop/ElectronicsShopRestAPI/ElectronicsShopRestAPI.csproj @@ -8,6 +8,7 @@ + diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs b/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs index 8cc92d9..a1e3c8e 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs +++ b/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs @@ -40,25 +40,23 @@ namespace ElectronicsShopUserApp { } } - public static void PostDictRequest(string requstUrl, OrderBindingModel model) { - var dict = new Dictionary(); + public static void ListPostRequest(string requstUrl, T model, int count, int orderID) { + var list = new List() { + JsonConvert.SerializeObject(model), + JsonConvert.SerializeObject(count), + JsonConvert.SerializeObject(orderID), + }; - foreach (var item in model.ProductList) { - var key = item.Key.ToString(); - var item1 = JsonConvert.SerializeObject(item.Value.Item1); - var item2 = item.Value.Item2.ToString(); - dict.Add(key, (item1, item2)); - } - var dictjs = JsonConvert.SerializeObject(dict); + var json = JsonConvert.SerializeObject(list); - var data = new StringContent(dictjs, Encoding.UTF8, "application/json"); + var data = new StringContent(json, Encoding.UTF8, "application/json"); - var response = _client.PostAsync(requstUrl, data,); + var response = _client.PostAsync(requstUrl, data); - var result = response.Result.Content.ReadAsStringAsync().Result; - if (!response.Result.IsSuccessStatusCode) { - throw new Exception(result); - } - } - } + var result = response.Result.Content.ReadAsStringAsync().Result; + if (!response.Result.IsSuccessStatusCode) { + throw new Exception(result); + } + } + } } diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs index c0fcf5e..f88098f 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs @@ -100,17 +100,33 @@ namespace ElectronicsShopUserApp.Controllers { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } + + return View(APIClient.GetRequset>($"api/main/getorders?_clientid={APIClient.Client.ID}")); } [HttpGet] public IActionResult CreateOrder() { - var view = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}") ?? new OrderViewModel { - ProductList = _productList - }; - return View(view); + if (APIClient.Client == null) { + return Redirect("~/Home/Enter"); + } + APIClient.PostRequest("api/main/createorder", new OrderBindingModel { + ClientID = APIClient.Client.ID, + DateCreate = DateTime.Now, + }); + return RedirectToAction("OrderView"); } + [HttpGet] + public IActionResult OrderView() { + if (APIClient.Client == null) { + return Redirect("~/Home/Enter"); + } + var view = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}"); + + return View(view); + } + [HttpGet] public IActionResult AddProduct() { ViewBag.Products = APIClient.GetRequset>($"api/main/getproducts"); @@ -118,30 +134,12 @@ namespace ElectronicsShopUserApp.Controllers { } [HttpPost] - public void AddProduct(double sum, int product, int count) { - if (APIClient.Client == null) { - throw new Exception(" "); - } - if (sum <= 0) { - throw new Exception(" 0"); - } - var _product = APIClient.GetRequset($"api/main/getproduct?_productid={product}"); - _product.Price = sum; + public void AddProduct(int product, int count) { + var _product = APIClient.GetRequset($"api/main/getproduct?_productid={product}"); + var _order = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}"); - if (_productList.ContainsKey(product)) { - _productList[product] = (_product, count); - } - else { - _productList.Add(product, (_product, count)); - } - - APIClient.PostDictRequest($"api/main/saveorder", new OrderBindingModel { - ProductList = _productList, - Sum = CalcAll(_productList), - ClientID = APIClient.Client.ID, - }); - - Response.Redirect("CreateOrder"); + APIClient.ListPostRequest($"api/main/addproduct", _product, count, _order.ID); + Response.Redirect("OrderView"); } [HttpPost] @@ -157,28 +155,8 @@ namespace ElectronicsShopUserApp.Controllers { [HttpPost] public double Calc(int count, int product) { - var prod = APIClient.GetRequset($"api/main/getproduct?_productid={product}"); - return count * (prod?.Price ?? 1); - } - [HttpGet] - public IActionResult AddProduct() - { - ViewBag.Products = APIClient.GetRequset>("api/main/getproducts"); - return View(); - } - [HttpPost] - public void AddProduct(int count/*, int product*/) - { - if (APIClient.Client == null) - { - throw new Exception(" ? "); - } - if (count <= 0) - { - throw new Exception(" 0"); - } - - Response.Redirect("CreateOrder"); + var _product = APIClient.GetRequset($"api/main/getproduct?_productid={product}"); + return count * (_product?.Price ?? 1); } } } diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/CreateOrder.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml similarity index 78% rename from ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/CreateOrder.cshtml rename to ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml index 8a48800..58a0f13 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/CreateOrder.cshtml +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml @@ -3,7 +3,7 @@ @model OrderViewModel @{ - ViewData["Title"] = "CreateOrder"; + ViewData["Title"] = "OrderView"; }
@@ -11,6 +11,12 @@
+
+ +
+ +
+

Добавить товар