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;
|
|
|
|
|
using ComputerStoreDataModels.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace ComputerStoreSellerApp.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class RequestController : Controller
|
|
|
|
|
{
|
|
|
|
|
private IRequestLogic _requestLogic;
|
|
|
|
|
private IComponentLogic _componentLogic;
|
|
|
|
|
|
|
|
|
|
private static List<(int componentId, int count)> selectedComponents = new();
|
|
|
|
|
|
|
|
|
|
public RequestController(IRequestLogic requestLogic, IComponentLogic componentLogic)
|
|
|
|
|
{
|
|
|
|
|
_requestLogic = requestLogic;
|
|
|
|
|
_componentLogic = componentLogic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("[controller]/[action]")]
|
|
|
|
|
public IActionResult AddComponent(int component, int amount)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
if (amount <= 0)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Create", "Request");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tmp = selectedComponents.FirstOrDefault(x => x.componentId == component && x.count > 0);
|
|
|
|
|
if (tmp.componentId != 0)
|
|
|
|
|
{
|
|
|
|
|
selectedComponents.Remove((tmp.componentId, tmp.count));
|
|
|
|
|
selectedComponents.Add((component, tmp.count + amount));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
selectedComponents.Add((component, amount));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("CreatePage", "Request");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("[controller]/[action]")]
|
|
|
|
|
public IActionResult UpdateComponent(int component, int amount, int id)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
if (amount <= 0)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Create", "Request");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tmp = selectedComponents.FirstOrDefault(x => x.componentId == component && x.count > 0);
|
|
|
|
|
if (tmp.componentId != 0)
|
|
|
|
|
{
|
|
|
|
|
selectedComponents.Remove((tmp.componentId, tmp.count));
|
|
|
|
|
selectedComponents.Add((component, tmp.count + amount));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
selectedComponents.Add((component, amount));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewData["currentRequest"] = _requestLogic.ReadElement(new RequestSearchModel() { ID = id });
|
|
|
|
|
ViewData["components"] = _componentLogic.ReadList(null);
|
|
|
|
|
|
|
|
|
|
ViewData["selectedComponents"] = selectedComponents;
|
|
|
|
|
|
|
|
|
|
return View("Views/Request/Create.cshtml");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("[controller]/[action]")]
|
|
|
|
|
public IActionResult List()
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
selectedComponents.Clear();
|
|
|
|
|
ViewData["requests"] = _requestLogic.ReadList(null);
|
|
|
|
|
|
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("[controller]/[action]/{id}")]
|
|
|
|
|
public IActionResult View(int id)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var request = _requestLogic.ReadElement(new RequestSearchModel() { ID = id });
|
|
|
|
|
|
|
|
|
|
if (request is null)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", "Заявки с таким идентификатором не существует");
|
|
|
|
|
return RedirectToAction("List", "Request", null);
|
|
|
|
|
}
|
|
|
|
|
ViewData["currentRequest"] = request;
|
|
|
|
|
ViewData["components"] = _componentLogic.ReadList(null);
|
|
|
|
|
|
|
|
|
|
foreach (var selcomp in request.RequestComponents)
|
|
|
|
|
{
|
|
|
|
|
selectedComponents.Add((selcomp.Item1.ID, selcomp.Item2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ViewData["selectedComponents"] = selectedComponents;
|
|
|
|
|
|
|
|
|
|
return View("Views/Request/Create.cshtml");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("[controller]/[action]")]
|
|
|
|
|
public IActionResult CreatePage()
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewData["selectedComponents"] = selectedComponents;
|
|
|
|
|
ViewData["components"] = _componentLogic.ReadList(null);
|
|
|
|
|
|
|
|
|
|
return View("Views/Request/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 (selectedComponents.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", "Заявка должна содержать компоненты");
|
|
|
|
|
return RedirectToAction("CreatePage", "Request");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 21:41:33 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
double tmpPrice = 0;
|
|
|
|
|
foreach (var (componentId, count) in selectedComponents)
|
|
|
|
|
{
|
|
|
|
|
tmpPrice += _componentLogic.ReadElement(new ComponentSearchModel() { ID = componentId })!.Price * count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_requestLogic.Create(new RequestBindingModel()
|
|
|
|
|
{
|
|
|
|
|
OrderID = null,
|
|
|
|
|
PCID = null,
|
|
|
|
|
Price = tmpPrice,
|
|
|
|
|
RequestComponents = selectedComponents.Select(x => ((IComponentModel)_componentLogic.ReadElement(new ComponentSearchModel() { ID = x.componentId })!, x.count)).ToList()
|
|
|
|
|
});
|
|
|
|
|
selectedComponents.Clear();
|
|
|
|
|
return RedirectToAction("List", "Request", null);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", Regex.Replace(e.Message, "[a-zA-Z\\(\\)']", ""));
|
|
|
|
|
selectedComponents.Clear();
|
|
|
|
|
return RedirectToAction("List", "Request", 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 (selectedComponents.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", "Заявка должна содержать компоненты");
|
|
|
|
|
ViewData["currentRequest"] = _requestLogic.ReadElement(new RequestSearchModel() { ID = id });
|
|
|
|
|
ViewData["components"] = _componentLogic.ReadList(null);
|
|
|
|
|
ViewData["selectedComponents"] = selectedComponents;
|
|
|
|
|
return View("Views/Request/Create.cshtml");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 21:41:33 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
double tmpPrice = 0;
|
|
|
|
|
foreach (var (componentId, count) in selectedComponents)
|
|
|
|
|
{
|
|
|
|
|
tmpPrice += _componentLogic.ReadElement(new ComponentSearchModel() { ID = componentId })!.Price * count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_requestLogic.Update(new RequestBindingModel()
|
|
|
|
|
{
|
|
|
|
|
ID = id,
|
|
|
|
|
OrderID = _requestLogic.ReadElement(new RequestSearchModel() { ID = id })?.OrderID,
|
|
|
|
|
PCID = _requestLogic.ReadElement(new RequestSearchModel() { ID = id })?.PCID,
|
|
|
|
|
Price = tmpPrice,
|
|
|
|
|
RequestComponents = selectedComponents.Select(x => ((IComponentModel)_componentLogic.ReadElement(new ComponentSearchModel() { ID = x.componentId })!, x.count)).ToList(),
|
|
|
|
|
});
|
|
|
|
|
selectedComponents.Clear();
|
|
|
|
|
return RedirectToAction("List", "Request", null);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
TempData.Add("error", Regex.Replace(e.Message, "[a-zA-Z\\(\\)']", ""));
|
|
|
|
|
return RedirectToAction("View", "Request", new { id = id });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("[controller]/[action]/{id}")]
|
|
|
|
|
public IActionResult Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var deletedRequest = _requestLogic.ReadElement(new RequestSearchModel { ID = id });
|
|
|
|
|
if (deletedRequest != null && deletedRequest.PCID == null && deletedRequest.OrderID == null)
|
|
|
|
|
{
|
|
|
|
|
_requestLogic.Delete(new RequestBindingModel() { ID = id });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("List", "Request", null);
|
|
|
|
|
}
|
2023-06-21 23:55:37 +04:00
|
|
|
|
|
|
|
|
|
[HttpPost("[controller]/[action]/{selectedcomponentid}/{currentrequestid}")]
|
|
|
|
|
public IActionResult DeleteComponent(int selectedcomponentid, int currentrequestid)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Session.Keys.Contains("login"))
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "Auth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectedComponents.Remove(selectedComponents.FirstOrDefault(x => x.componentId == selectedcomponentid && x.count > 0));
|
|
|
|
|
if (currentrequestid > 0)
|
|
|
|
|
{
|
|
|
|
|
ViewData["currentRequest"] = _requestLogic.ReadElement(new RequestSearchModel() { ID = currentrequestid });
|
|
|
|
|
ViewData["components"] = _componentLogic.ReadList(null);
|
|
|
|
|
ViewData["selectedComponents"] = selectedComponents;
|
|
|
|
|
return View("Views/Request/Create.cshtml");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("CreatePage", "Request");
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-20 21:41:33 +04:00
|
|
|
|
}
|
|
|
|
|
}
|