Order delete/edit

This commit is contained in:
Илья Федотов 2024-06-01 00:42:46 +04:00
parent 105adb9568
commit d063e43236
12 changed files with 177 additions and 39 deletions

View File

@ -39,6 +39,24 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
return true;
}
public bool Delete(OrderBindingModel model) {
CheckModel(model, false);
_logger.LogInformation($"Delete. ID:{model.ID}");
if (_storage.Delete(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool DeleteProduct(OrderBindingModel model) {
if (_storage.DeleteProduct(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation($"ReadList:ID:{model?.ID}");

View File

@ -18,5 +18,9 @@ namespace ElectronicsShopContracts.BusinessLogicContracts
bool CreateOrder(OrderBindingModel model);
bool Update(OrderBindingModel model);
}
bool Delete(OrderBindingModel model);
bool DeleteProduct(OrderBindingModel model);
}
}

View File

@ -17,5 +17,8 @@ namespace ElectronicsShopContracts.StorageContracts
OrderViewModel? GetElement(OrderSearchModel model);
OrderViewModel? Insert(OrderBindingModel model);
OrderViewModel? Update(OrderBindingModel model);
}
OrderViewModel? Delete(OrderBindingModel model);
OrderViewModel? DeleteProduct(OrderBindingModel model);
}
}

View File

@ -4,6 +4,7 @@ using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System.Xml.Linq;
namespace ElectronicsShopDataBaseImplement.Implements
@ -26,19 +27,27 @@ namespace ElectronicsShopDataBaseImplement.Implements
public OrderViewModel? Update(OrderBindingModel model) {
using var context = new Database();
using var transcation = context.Database.BeginTransaction();
try {
var order = context.Orders.FirstOrDefault(rec => rec.ID == model.ID);
if (order == null) {
return null;
}
order.UpdateProducts(context, model);
transcation.Commit();
return order.GetViewModel;
}
catch {
transcation.Rollback();
throw;
}
var order = context.Orders.FirstOrDefault(rec => rec.ID == model.ID);
if (order == null) {
return null;
}
order.UpdateProducts(context, model);
transcation.Commit();
return order.GetViewModel;
}
public OrderViewModel? DeleteProduct(OrderBindingModel model) {
using var context = new Database();
using var transcation = context.Database.BeginTransaction();
var order = context.Orders.FirstOrDefault(rec => rec.ID == model.ID);
if (order == null) {
return null;
}
order.DeleteProducts(context, model);
transcation.Commit();
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
@ -105,6 +114,5 @@ namespace ElectronicsShopDataBaseImplement.Implements
.ThenInclude(x => x._product).ToList()
.Select(x => x.GetViewModel).ToList();
}
}
}

View File

@ -97,5 +97,11 @@ namespace ElectronicsShopDataBaseImplement.Models
}
_productList = null;
}
public void DeleteProducts(Database context, OrderBindingModel model) {
var orderProducts = context.OrderProducts.Where(rec => rec.OrderID == model.ID).ToList();
context.OrderProducts.RemoveRange(orderProducts.Where(rec => !model.ProductList.ContainsKey(rec.ProductID)));
context.SaveChanges();
}
}
}

View File

@ -252,12 +252,6 @@ namespace ElectronicsShopEmployeeApp.Controllers {
[HttpGet]
public IActionResult DeleteProduct(int id) {
var _product = APIEmployee.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={id}");
if (_product == null) {
return Redirect("/Home/Index");
}
APIEmployee.PostRequest("api/employee/deleteproduct", new ProductBindingModel {
ID = id
});

View File

@ -83,6 +83,17 @@ namespace ElectronicsShopRestAPI.Controllers {
}
}
[HttpPost]
public void DeleteOrders(OrderBindingModel model) {
try {
_order.Delete(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка удаления заказа");
throw;
}
}
[HttpPost]
public void CreateOrder(OrderBindingModel model) {
try {
@ -147,7 +158,8 @@ namespace ElectronicsShopRestAPI.Controllers {
ID = orderid,
ClientID = view.ClientID,
DateCreate = view.DateCreate,
ProductList = _productlist
ProductList = _productlist,
//sum
};
var operationResult = _order.Update(model);
if (!operationResult) {
@ -159,5 +171,43 @@ namespace ElectronicsShopRestAPI.Controllers {
throw;
}
}
[HttpPost]
public void DeleteProductOrder(List<string> jslist) {
int _orderId = JsonConvert.DeserializeObject<int>(jslist[0]);
int _productId = JsonConvert.DeserializeObject<int>(jslist[1]);
var view = _order.ReadElement(new OrderSearchModel { ID = _orderId });
if (view == null) {
_logger.LogError("Ошибка получения данных");
return;
}
_productlist = view.ProductList;
foreach (var item in _productlist) {
if (item.Key == _productId) {
_productlist.Remove(_productId);
}
}
try {
var model = new OrderBindingModel {
ID = _orderId,
ClientID = view.ClientID,
DateCreate = view.DateCreate,
ProductList = _productlist
//sum
};
var operationResult = _order.DeleteProduct(model);
if (!operationResult) {
throw new Exception("Ошибка при сохранении, дополнительная информация в логах");
}
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка");
throw;
}
}
}
}

View File

@ -40,6 +40,24 @@ namespace ElectronicsShopUserApp {
}
}
public static void PostRequestStr(string requstUrl, int _orderId, int _productId) {
var list = new List<string>() {
JsonConvert.SerializeObject(_orderId),
JsonConvert.SerializeObject(_productId),
};
var json = JsonConvert.SerializeObject(list);
var data = new StringContent(json, 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);
}
}
public static void ListPostRequest<T>(string requstUrl, T model, int count, int orderID) {
var list = new List<string>() {
JsonConvert.SerializeObject(model),

View File

@ -15,6 +15,7 @@ namespace ElectronicsShopUserApp.Controllers {
public class HomeController : Controller {
private readonly ILogger<HomeController> _logger;
private Dictionary<int, (IProductModel, int)> _productList;
public int Id;
//private readonly IOrderLogic _order;
public HomeController(ILogger<HomeController> logger/*, IOrderLogic orderLogic*/) {
@ -122,11 +123,21 @@ namespace ElectronicsShopUserApp.Controllers {
}
[HttpGet]
public IActionResult DeleteOrder(int id) {
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id });
return RedirectToAction("Orders");
}
[HttpGet]
public IActionResult OrderView() {
if (APIClient.Client == null) {
return Redirect("~/Home/Enter");
}
var view = APIClient.GetRequset<OrderViewModel>($"api/main/getorder?_clientid={APIClient.Client?.ID}");
if (view != null) {
Id = view.ID;
}
var products = APIClient.GetRequset<List<List<string>>>($"api/main/getorderproducts?_orderid={view?.ID}");
foreach (var pr in products) {
@ -135,15 +146,27 @@ namespace ElectronicsShopUserApp.Controllers {
_productList.Add(product.ID, (product, count));
}
return View(_productList);
(int, Dictionary<int, (IProductModel, int)>) tuple = (Id, _productList);
return View(tuple);
}
[HttpPost]
public void OrderView(int sum) {
public void OrderView(int sum, int id) {
if (sum <= 0) {
APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id});
}
Response.Redirect("Orders");
}
[HttpGet]
public IActionResult DeleteProductOrder(int id) {
var view = APIClient.GetRequset<OrderViewModel>($"api/main/getorder?_clientid={APIClient.Client?.ID}");
APIClient.PostRequestStr($"api/main/deleteproductorder", view.ID, id);
return RedirectToAction("OrderView");
}
[HttpGet]
public IActionResult AddProduct() {
ViewBag.Products = APIClient.GetRequset<List<ProductViewModel>>($"api/main/getproducts");
return View();

View File

@ -33,7 +33,7 @@
</thead>
<tbody>
@foreach (var item in Model) {
<th>
<tr>
<th>
@Html.DisplayFor(modelItem => item.ID)
</th>
@ -43,7 +43,7 @@
<th>
@Html.DisplayFor(modelItem => item.Sum)
</th>
</th>
</tr>
}
</tbody>
</table>

View File

@ -1,7 +1,7 @@
@using ElectronicsShopContracts.ViewModels
@using ElectronicsShopDataModels.Models
@model Dictionary<int, (IProductModel, int)>
@model (int, Dictionary<int, (IProductModel, int)>)
@{
ViewData["Title"] = "OrderView";
@ -11,7 +11,15 @@
<h1 class="display-4">Создание корзины</h1>
</div>
<form method="post">
<div class="row">
<div class="col-4"></div>
<div class="col-8">
<input id="id" type="hidden" name="id" readonly value="@Model.Item1" />
</div>
</div>
<div class=" text-center">
<div class="row">
<div class="col-4">Сумма:</div>
@ -25,6 +33,9 @@
<table class="table">
<thead>
<tr>
<th>
Номер
</th>
<th>
Продукт
</th>
@ -37,11 +48,14 @@
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
@foreach (var item in Model.Item2) {
<tr class="element">
<th>
@Html.DisplayFor(modelItem => item.Value.Item1.ProductName)
</th>
<th>
@Html.DisplayFor(modelItem => item.Key)
</th>
<th>
@Html.DisplayFor(modelItem => item.Value.Item1.ProductName)
</th>
<th class="count">
@Html.DisplayFor(modelItem => item.Value.Item2)
@ -51,9 +65,9 @@
@Html.DisplayFor(modelItem => item.Value.Item1.Price)
</th>
<td>
<a class="btn btn-primary btn-sm" asp-action="DeleteProductOrder" asp->Удалить</a>
</td>
<td>
<a class="btn btn-primary btn-sm" asp-action="DeleteProductOrder" asp-route-ID="@item.Key">Удалить</a>
</td>
</tr>
}
</tbody>

View File

@ -48,8 +48,8 @@
@Html.DisplayFor(modelItem => item.Sum)
</th>
<td>
<a class="btn btn-primary btn-sm" asp-action="EditCostItem" asp-route-ID="@item.ID">Изменить</a>
<a class="btn btn-primary btn-sm" asp-action="DeleteCostItem" asp-route-ID="@item.ID">Удалить</a>
<a class="btn btn-primary btn-sm" asp-action="EditOrder" asp-route-ID="@item.ID">Изменить</a>
<a class="btn btn-primary btn-sm" asp-action="DeleteOrder" asp-route-ID="@item.ID">Удалить</a>
</td>
</tr>
}