2023-06-21 23:55:37 +04:00
|
|
|
|
using ComputerStoreBusinessLogic.BusinessLogic;
|
|
|
|
|
using ComputerStoreContracts.BindingModels;
|
2023-06-20 21:41:33 +04:00
|
|
|
|
using ComputerStoreContracts.BusinessLogicContracts;
|
|
|
|
|
using ComputerStoreContracts.SearchModels;
|
2023-06-21 23:55:37 +04:00
|
|
|
|
using ComputerStoreContracts.ViewModels;
|
2023-06-20 21:41:33 +04:00
|
|
|
|
using ComputerStoreDatabaseImplement.Models;
|
|
|
|
|
using ComputerStoreDataModels.Models;
|
|
|
|
|
using DocumentFormat.OpenXml.Office2010.Excel;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace ComputerStoreSellerApp.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class ConsignmentController : Controller
|
|
|
|
|
{
|
|
|
|
|
private IConsignmentLogic _consignmentLogic;
|
|
|
|
|
private IProductLogic _productLogic;
|
|
|
|
|
|
|
|
|
|
private static List<(int productId, int count)> selectedProducts = new();
|
|
|
|
|
|
|
|
|
|
public ConsignmentController(IConsignmentLogic consignmentLogic, IProductLogic productLogic)
|
|
|
|
|
{
|
|
|
|
|
_consignmentLogic = consignmentLogic;
|
|
|
|
|
_productLogic = productLogic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("[controller]/[action]")]
|
|
|
|
|
public IActionResult AddProduct(int product, int amount)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
if (amount <= 0)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Create", "Consignment");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tmp = selectedProducts.FirstOrDefault(x => x.productId == product && x.count > 0);
|
|
|
|
|
if (tmp.productId != 0)
|
|
|
|
|
{
|
|
|
|
|
selectedProducts.Remove((tmp.productId, tmp.count));
|
|
|
|
|
selectedProducts.Add((product, tmp.count + amount));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
selectedProducts.Add((product, amount));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("CreatePage", "Consignment");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("[controller]/[action]")]
|
|
|
|
|
public IActionResult UpdateProduct(int product, int amount, int id)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
if (amount <= 0)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Create", "Consignment");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tmp = selectedProducts.FirstOrDefault(x => x.productId == product && x.count > 0);
|
|
|
|
|
if (tmp.productId != 0)
|
|
|
|
|
{
|
|
|
|
|
selectedProducts.Remove((tmp.productId, tmp.count));
|
|
|
|
|
selectedProducts.Add((product, tmp.count + amount));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
selectedProducts.Add((product, amount));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewData["currentConsignment"] = _consignmentLogic.ReadElement(new ConsignmentSearchModel() { ID = id });
|
|
|
|
|
ViewData["products"] = _productLogic.ReadList(null);
|
|
|
|
|
|
|
|
|
|
ViewData["selectedProducts"] = selectedProducts;
|
|
|
|
|
|
|
|
|
|
return View("Views/Consignment/Create.cshtml");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("[controller]/[action]")]
|
|
|
|
|
public IActionResult List()
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
selectedProducts.Clear();
|
|
|
|
|
ViewData["consignments"] = _consignmentLogic.ReadList(null);
|
|
|
|
|
|
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("[controller]/[action]/{id}")]
|
|
|
|
|
public IActionResult View(int id)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var consignment = _consignmentLogic.ReadElement(new ConsignmentSearchModel() { ID = id });
|
|
|
|
|
|
|
|
|
|
if (consignment is null)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", "Партии с таким идентификатором не существует");
|
|
|
|
|
return RedirectToAction("List", "Consignment", null);
|
|
|
|
|
}
|
|
|
|
|
ViewData["currentConsignment"] = consignment;
|
|
|
|
|
ViewData["products"] = _productLogic.ReadList(null);
|
|
|
|
|
|
|
|
|
|
foreach (var selprod in consignment.ConsignmentProducts)
|
|
|
|
|
{
|
|
|
|
|
selectedProducts.Add((selprod.Value.Item1.ID, selprod.Value.Item2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ViewData["selectedProducts"] = selectedProducts;
|
|
|
|
|
|
|
|
|
|
return View("Views/Consignment/Create.cshtml");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("[controller]/[action]")]
|
|
|
|
|
public IActionResult CreatePage()
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewData["selectedProducts"] = selectedProducts;
|
|
|
|
|
ViewData["products"] = _productLogic.ReadList(null);
|
|
|
|
|
|
|
|
|
|
return View("Views/Consignment/Create.cshtml");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("[controller]/[action]")]
|
|
|
|
|
public IActionResult Create()
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 23:55:37 +04:00
|
|
|
|
if (selectedProducts.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", "Партия должна содержать продукты");
|
|
|
|
|
return RedirectToAction("CreatePage", "Consignment");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 21:41:33 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
double tmpPrice = 0;
|
|
|
|
|
foreach (var (productId, count) in selectedProducts)
|
|
|
|
|
{
|
|
|
|
|
tmpPrice += _productLogic.ReadElement(new ProductSearchModel() { ID = productId })!.Price * count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_consignmentLogic.Create(new ConsignmentBindingModel()
|
|
|
|
|
{
|
|
|
|
|
OrderID = null,
|
|
|
|
|
ConsignmentProducts = selectedProducts.ToDictionary(x => x.productId, x => ((IProductModel)_productLogic.ReadElement(new ProductSearchModel() { ID = x.productId })!, x.count)),
|
|
|
|
|
Price = tmpPrice,
|
|
|
|
|
});
|
|
|
|
|
selectedProducts.Clear();
|
|
|
|
|
return RedirectToAction("List", "Consignment", null);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", Regex.Replace(e.Message, "[a-zA-Z\\(\\)']", ""));
|
|
|
|
|
selectedProducts.Clear();
|
|
|
|
|
return RedirectToAction("List", "Consignment", null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("[controller]/[action]/{id}")]
|
|
|
|
|
public IActionResult Edit(int id)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 23:55:37 +04:00
|
|
|
|
if (selectedProducts.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", "Партия должна содержать продукты");
|
|
|
|
|
ViewData["currentConsignment"] = _consignmentLogic.ReadElement(new ConsignmentSearchModel() { ID = id });
|
|
|
|
|
ViewData["products"] = _productLogic.ReadList(null);
|
|
|
|
|
ViewData["selectedProducts"] = selectedProducts;
|
|
|
|
|
return View("Views/Consignment/Create.cshtml");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 21:41:33 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
double tmpPrice = 0;
|
|
|
|
|
foreach (var (productId, count) in selectedProducts)
|
|
|
|
|
{
|
|
|
|
|
tmpPrice += _productLogic.ReadElement(new ProductSearchModel() { ID = productId })!.Price * count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_consignmentLogic.Update(new ConsignmentBindingModel()
|
|
|
|
|
{
|
|
|
|
|
ID = id,
|
|
|
|
|
OrderID = _consignmentLogic.ReadElement( new ConsignmentSearchModel() { ID = id })?.OrderID,
|
|
|
|
|
ConsignmentProducts = selectedProducts.ToDictionary(x => x.productId, x => ((IProductModel)_productLogic.ReadElement(new ProductSearchModel() { ID = x.productId })!, x.count)),
|
|
|
|
|
Price = tmpPrice,
|
|
|
|
|
});
|
|
|
|
|
selectedProducts.Clear();
|
|
|
|
|
return RedirectToAction("List", "Consignment", null);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", Regex.Replace(e.Message, "[a-zA-Z\\(\\)']", ""));
|
|
|
|
|
return RedirectToAction("View", "Consignment", new { id = id });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("[controller]/[action]/{id}")]
|
|
|
|
|
public IActionResult Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_consignmentLogic.ReadElement(new ConsignmentSearchModel { ID = id })?.OrderID == null)
|
|
|
|
|
{
|
|
|
|
|
_consignmentLogic.Delete(new ConsignmentBindingModel() { ID = id });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("List", "Consignment", null);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 23:55:37 +04:00
|
|
|
|
[HttpPost("[controller]/[action]/{selectedproductid}/{currentconsignmentid}")]
|
|
|
|
|
public IActionResult DeleteProduct(int selectedproductid, int currentconsignmentid)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectedProducts.Remove(selectedProducts.FirstOrDefault(x => x.productId == selectedproductid && x.count > 0));
|
|
|
|
|
if (currentconsignmentid > 0)
|
|
|
|
|
{
|
|
|
|
|
ViewData["currentConsignment"] = _consignmentLogic.ReadElement(new ConsignmentSearchModel() { ID = currentconsignmentid });
|
|
|
|
|
ViewData["products"] = _productLogic.ReadList(null);
|
|
|
|
|
ViewData["selectedProducts"] = selectedProducts;
|
|
|
|
|
return View("Views/Consignment/Create.cshtml");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("CreatePage", "Consignment");
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-20 21:41:33 +04:00
|
|
|
|
}
|
|
|
|
|
}
|