Траблы с заказом

This commit is contained in:
Игорь Гордеев 2024-05-29 18:32:24 +04:00
parent 9b14abf7e3
commit 06baeba759
6 changed files with 142 additions and 25 deletions

View File

@ -5,11 +5,7 @@ using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
@ -63,7 +59,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
{
if (model == null) throw new ArgumentNullException(nameof(model));
_logger.LogInformation($"ReadElement. ID:{model.ID}");
var element = _storage.GetElement(model);
var element = _storage.GetElement(model);//Не находит в бд
if (element == null)
{
_logger.LogWarning("ReadElement. elementn not found");
@ -72,5 +68,6 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
_logger.LogInformation($"ReadElement find. ID:{element.ID}");
return element;
}
}
}
}

View File

@ -78,5 +78,42 @@ namespace ElectronicsShopRestAPI.Controllers {
throw;
}
}
}
[HttpGet]
public List<OrderViewModel>? 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;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
}
}

View File

@ -1,4 +1,5 @@
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Models;
using ElectronicsShopUserApp.Models;
@ -91,31 +92,26 @@ namespace ElectronicsShopUserApp.Controllers {
}
[HttpGet]
public IActionResult Create() {
public IActionResult CreateOrder() {
ViewBag.Products = APIClient.GetRequset<List<ProductViewModel>>("api/main/getproducts");
return View();
}
[HttpPost]
public void Create(OrderBindingModel order) {
public void CreateOrder(OrderBindingModel order) {
if (APIClient.Client == null)
{
throw new Exception("Âû êàê ñóäà ïîïàëè? Ñóäà âõîä òîëüêî àâòîðèçîâàííûì");
}
if (order.ProductList.Count <= 0)
if (order.ProductList.Count > 0)
{
throw new Exception("Êîëè÷åñòâî è ñóììà äîëæíû áûòü áîëüøå 0");
}
APIClient.PostRequest("api/main/createorder", new OrderBindingModel
{
ClientID = APIClient.Client.ID,
Sum = Calc(order.ProductList),
ProductList = order.ProductList,
DateCreate = DateTime.Now,
});
Response.Redirect("Index");
}
[HttpPost]
private double Calc(Dictionary<int, (IProductModel, int)> ProductList)
private double CalcAll(Dictionary<int, (IProductModel, int)> ProductList)
{
Double Sum = 0;
foreach (var ProductItem in ProductList)
@ -124,5 +120,37 @@ namespace ElectronicsShopUserApp.Controllers {
}
return Sum;
}
}
[HttpPost]
public double CalcProduct(int count, int product)
{
var prod =
APIClient.GetRequset<ProductViewModel>($"api/main/getproduct?productID={product}"
);
return count * (prod?.Price ?? 1);
}
[HttpGet]
public IActionResult AddProduct()
{
ViewBag.Products = APIClient.GetRequset<List<ProductViewModel>>("api/main/getproducts");
return View();
}
[HttpPost]
public void AddProduct(int count)
{
if (APIClient.Client == null)
{
throw new Exception("Âû êàê ñóäà ïîïàëè? Ñóäà âõîä òîëüêî àâòîðèçîâàííûì");
}
if (count <= 0)
{
throw new Exception("Êîëè÷åñòâî è ñóììà äîëæíû áûòü áîëüøå 0");
}
APIClient.PostRequest("api/main/addproduct", new OrderSearchModel
{
ID = 1,
ClientID=APIClient.Client.ID,
});
Response.Redirect("CreateOrder");
}
}
}

View File

@ -1,5 +1,5 @@
@{
ViewData["Title"] = "Create";
ViewData["Title"] = "Add Product";
}
<div class="text-center">
<h2 class="display-4">Создание заказа</h2>
@ -31,7 +31,7 @@
<div class="row">
<div class="col-8"></div>
<div class="col-4">
<input type="submit" value="Создать" class="btn btn-primary" />
<input type="submit" value="Добавить" class="btn btn-primary" />
</div>
</div>
</form>
@ -48,8 +48,8 @@
if (count && product) {
$.ajax({
method: "POST",
url: "/Home/Calc",
data: { count: count, product: product },
url: "/Home/CalcProduct",
data: { count: count, product: product.price },
success: function (result) {
var roundedResult = parseFloat(result).toFixed(2);
$("#sum").val(roundedResult);

View File

@ -0,0 +1,55 @@
@using ElectronicsShopContracts.ViewModels
@model OrderViewModel
@{
ViewData["Title"] = "Create Order";
}
<div class="row">
<p>
<a asp-action="AddProduct">Добавть продукт в корзину</a>
</p>
<div class="col-4">
<input type="submit" value="Создать заказ" class="btn btn-primary" />
</div>
</div>
<div class="text-center">
<h1 class="display-4">Корзина</h1>
@{
<table class="table">
<thead>
<th>
<th>
Продукт
</th>
<th>
Количество
</th>
<th>
Сумма
</th>
</th>
</thead>
<tbody>
@if(Model!=null){
@foreach (var item in Model.ProductList)
{
<th>
<th>
@Html.DisplayFor(modelItem => item.Value.Item1.ProductName)
</th>
<th>
@Html.DisplayFor(modelItem => item.Value.Item2)
</th>
<th>
@Html.DisplayFor(modelItem => item.Value.Item1.Price*item.Value.Item2)
</th>
</th>
}
}
</tbody>
</table>
}
</div>

View File

@ -18,7 +18,7 @@
return;
}
<p>
<a asp-action="Create">Создать заказ</a>
<a asp-action="CreateOrder">Создать заказ</a>
</p>
<table class="table">
<thead>