diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs index e4868b2..23bfaec 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs @@ -12,7 +12,7 @@ namespace ElectronicsShopDataBaseImplement optionsBuilder) { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-E2VPEN3\SQLEXPRESS;Initial Catalog=ElectronicsShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-O0N00SH\SQLEXPRESS;Initial Catalog=ElectronicsShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs index 9002793..a7b0037 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs @@ -69,11 +69,18 @@ namespace ElectronicsShopDataBaseImplement.Implements public List GetFilteredList(OrderSearchModel model) { - if (!model.ID.HasValue && (model.DateFrom == null || model.DateTo == null)) + using var context = new Database(); + if (!model.ID.HasValue && (model.DateFrom == null || model.DateTo == null)) { return new(); } - using var context = new Database(); + else if (model.ClientID.HasValue) { + return context.Orders + .Include (x => x.Products) + .Where(x => x.ClientID == model.ClientID) + .Select(x => x.GetViewModel) + .ToList(); + } return context.Orders .Include(x => x.Products) .ThenInclude(x => x._product) diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs index 6d4b734..56e226a 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs @@ -116,7 +116,7 @@ namespace ElectronicsShopEmployeeApp.Controllers { Price = price, CostNum = costNum }); - Response.Redirect("CostItem"); + } [HttpGet] diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs index 37d2866..d35ad5f 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs @@ -3,6 +3,7 @@ using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataBaseImplement.Models; +using ElectronicsShopDataModels.Models; using Microsoft.AspNetCore.Mvc; namespace ElectronicsShopRestAPI.Controllers { @@ -14,13 +15,15 @@ namespace ElectronicsShopRestAPI.Controllers { private readonly ILogger _logger; private readonly IProductLogic _product; private readonly IOrderLogic _order; - + private Dictionary _ProductList; + private int? _ID; public MainController(ILogger logger, IProductLogic product, IOrderLogic orderLogic) { _logger = logger; _product = product; _order = orderLogic; + _ProductList = new Dictionary(); } [HttpGet] @@ -65,6 +68,17 @@ namespace ElectronicsShopRestAPI.Controllers { } } + [HttpGet] + public OrderViewModel? GetOrder(int _clientID) { + try { + return _order.ReadElement(new OrderSearchModel { ClientID = _clientID }); + } + catch (Exception ex) { + _logger.LogError(ex, $"Ошибка получения данных clientid = {_clientID}"); + throw; + } + } + [HttpPost] public void CreateOrder(OrderBindingModel model) { @@ -78,43 +92,10 @@ namespace ElectronicsShopRestAPI.Controllers { throw; } } - [HttpGet] - public List? GetAddProduct(int _clientID,int _orderID) - { - try - { - return _order.ReadList(new OrderSearchModel - { - ID = _orderID, - ClientID = _clientID - }); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка получения списка заказов клиента id = {Id} ", _clientID); - throw; - } - } - - - [HttpPost] - public void AddProduct(OrderBindingModel model) - { - 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; - } - } + [HttpPost] + public void SaveOrder(Dictionary list) { + + } } } diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs b/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs index 90dba5c..8cc92d9 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs +++ b/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs @@ -1,5 +1,7 @@ -using ElectronicsShopContracts.ViewModels; +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.ViewModels; using Newtonsoft.Json; +using System.Collections.Generic; using System.Net.Http.Headers; using System.Text; @@ -20,7 +22,7 @@ namespace ElectronicsShopUserApp { var result = response.Result.Content.ReadAsStringAsync().Result; if (response.Result.IsSuccessStatusCode) { return JsonConvert.DeserializeObject(result); - } + } else { throw new Exception(result); } @@ -37,5 +39,26 @@ namespace ElectronicsShopUserApp { throw new Exception(result); } } + + public static void PostDictRequest(string requstUrl, OrderBindingModel model) { + var dict = new Dictionary(); + + 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 data = new StringContent(dictjs, Encoding.UTF8, "application/json"); + + var response = _client.PostAsync(requstUrl, data,); + + 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 49e1181..70834d7 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs @@ -12,11 +12,12 @@ using System.Runtime.Serialization; namespace ElectronicsShopUserApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; - //private readonly IOrderLogic _order; + private Dictionary _productList; + //private readonly IOrderLogic _order; - public HomeController(ILogger logger/*, IOrderLogic orderLogic*/) { + public HomeController(ILogger logger/*, IOrderLogic orderLogic*/) { _logger = logger; - //_order = orderLogic; + _productList = new Dictionary(); } public IActionResult Index() { @@ -95,26 +96,56 @@ namespace ElectronicsShopUserApp.Controllers { return; } - [HttpGet] - public IActionResult CreateOrders() { + public IActionResult Orders() { + if (APIClient.Client == null) { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequset>($"api/main/getorders?_clientid={APIClient.Client.ID}")); + } - ViewBag.Products =APIClient.GetRequset>("api/main/getproducts"); - return View(); + [HttpGet] + public IActionResult CreateOrder() { + var view = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}") ?? new OrderViewModel { + ProductList = _productList + }; + return View(view); + } + + [HttpGet] + public IActionResult AddProduct() { + ViewBag.Products = APIClient.GetRequset>($"api/main/getproducts"); + return View(); } - [HttpPost] - public void CreateOrders(OrderBindingModel order) { - if (APIClient.Client == null) - { - throw new Exception(" ? "); + + [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; + + if (_productList.ContainsKey(product)) { + _productList[product] = (_product, count); } - if (order.ProductList.Count > 0) - { - throw new Exception(" 0"); + else { + _productList.Add(product, (_product, count)); } - Response.Redirect("Index"); - } + + APIClient.PostDictRequest($"api/main/saveorder", new OrderBindingModel { + ProductList = _productList, + Sum = CalcAll(_productList), + ClientID = APIClient.Client.ID, + }); + + Response.Redirect("CreateOrder"); + } + [HttpPost] - private double CalcAll(Dictionary ProductList) + private double CalcAll(Dictionary ProductList) { Double Sum = 0; foreach (var ProductItem in ProductList) @@ -126,30 +157,8 @@ namespace ElectronicsShopUserApp.Controllers { [HttpPost] public double Calc(int count, int product) { - var prod = - APIClient.GetRequset($"api/main/getproduct?_productid={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"); - } } } diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/AddProduct.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/AddProduct.cshtml index 5166693..9ec8b4d 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/AddProduct.cshtml +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/AddProduct.cshtml @@ -8,12 +8,7 @@
Продукты:
- +
@@ -52,9 +47,8 @@ method: "POST", url: "/Home/Calc", data: { count: count, product: product }, - success: function (result) { - var roundedResult = parseFloat(result).toFixed(2); - $("#sum").val(roundedResult); + success: function (result) { + $("#sum").val(result); } }); }; diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/CreateOrder.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/CreateOrder.cshtml new file mode 100644 index 0000000..8a48800 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/CreateOrder.cshtml @@ -0,0 +1,50 @@ +@using ElectronicsShopContracts.ViewModels + +@model OrderViewModel + +@{ + ViewData["Title"] = "CreateOrder"; +} + +
+

Создание корзины

+
+ +
+

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

+ + + + + + + + + + @foreach (var item in Model.ProductList) { + + + + + + + } + +
+ Продукт + + Количество + + Сумма +
+ @Html.DisplayFor(modelItem => item.Value.Item1.ProductName); + + @Html.DisplayFor(modelItem => item.Value.Item2); + + @Html.DisplayFor(modelItem => (item.Value.Item1.Price * item.Value.Item2)); + + +
+
\ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/CreateOrders.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/CreateOrders.cshtml deleted file mode 100644 index d5c95ae..0000000 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/CreateOrders.cshtml +++ /dev/null @@ -1,45 +0,0 @@ -@using ElectronicsShopContracts.ViewModels - -@model OrderViewModel -@{ - ViewData["Title"] = "CreateOrders"; -} - -
-

Корзина

- @{ - - - - - - - - - @if(Model!=null){ - @foreach (var item in Model.ProductList) - { - - - - - } - } - -
- - Продукт - - Количество - - Сумма -
- - @Html.DisplayFor(modelItem => item.Value.Item1.ProductName) - - @Html.DisplayFor(modelItem => item.Value.Item2) - - @Html.DisplayFor(modelItem => item.Value.Item1.Price*item.Value.Item2) -
- } -
\ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Orders.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Orders.cshtml new file mode 100644 index 0000000..b633899 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Orders.cshtml @@ -0,0 +1,59 @@ +@using ElectronicsShopContracts.ViewModels + +@model List + +@{ + ViewData["Title"] = "Orders"; +} + +
+

Корзины

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

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

+ return; + } + +

+ Добавить корзину +

+ + + + + + + + + + @foreach (var item in Model) { + + + + + + + } + +
+ Номер + + Дата создания + + Сумма +
+ @Html.DisplayFor(modelItem => item.ID) + + @Html.DisplayFor(modelItem => item.DateCreate) + + @Html.DisplayFor(modelItem => item.Sum) + + Изменить + Удалить +
+ } +
\ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Shared/_Layout.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Shared/_Layout.cshtml index f572c08..1a63db2 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Shared/_Layout.cshtml +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Shared/_Layout.cshtml @@ -32,10 +32,7 @@ Регистрация -