ComputerHardwareStore_YouAr.../ComputerHardwareStore/StoreKeeperClient/Controllers/StoreKeeperController.cs

388 lines
10 KiB
C#

using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BusinessLogicsContracts;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreContracts.ViewModels.HelperModels;
using ComputerHardwareStoreDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace StoreKeeperClient.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class StoreKeeperController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IStoreKeeperLogic _storeKeeperLogic;
private readonly IComponentLogic _componentLogic;
private readonly IBuildLogic _buildLogic;
private readonly IProductLogic _productLogic;
public StoreKeeperController(ILogger<HomeController> logger, IComponentLogic componentLogic, IBuildLogic buildLogic, IProductLogic productLogic, IStoreKeeperLogic storeKeeperLogic)
{
_logger = logger;
_componentLogic = componentLogic;
_buildLogic = buildLogic;
_productLogic = productLogic;
_storeKeeperLogic = storeKeeperLogic;
}
[HttpPost]
public void CreateComponent(string name, double price)
{
if (string.IsNullOrEmpty(name))
{
throw new Exception("Enter name");
}
_componentLogic.Create(
new ComponentBindingModel
{
Name = name,
Cost = price,
StoreKeeper = APIClient.Client!
}
);
Response.Redirect("Components");
}
[HttpGet]
public IActionResult UpdateComponent()
{
ViewBag.Components = _componentLogic.ReadList(
new ComponentSearchModel
{
StoreKeeperId = APIClient.Client.Id
}
);
return View();
}
[HttpPost]
public void UpdateComponent(int component, double price, string name)
{
if (component == 0)
{
throw new Exception("Chose component");
}
if (string.IsNullOrEmpty(name))
{
throw new Exception("Enter name");
}
_componentLogic.Update(
new ComponentBindingModel
{
Id = component,
Name = name,
Cost = price,
StoreKeeper = APIClient.Client!
}
);
Response.Redirect("Components");
}
[HttpGet]
public IActionResult ComponentToBuild()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Assemblies = _buildLogic.ReadList(null);
ViewBag.Components = _componentLogic.ReadList(
new ComponentSearchModel
{
StoreKeeperId = APIClient.Client!.Id
}
);
return View();
}
[HttpPost]
public void ComponentToBuild(int buildId, int componentId, int count)
{
if (buildId == 0)
{
throw new Exception("chose build");
}
if (componentId == 0)
{
throw new Exception("chose component");
}
if (count <= 0)
{
throw new Exception("chose count");
}
BuildViewModel build = _buildLogic.ReadElement(new BuildSearchModel { Id = buildId })!;
ComponentViewModel component = _componentLogic.ReadElement(new ComponentSearchModel { Id = componentId })!;
if (build.BuildComponents.ContainsKey(componentId))
build.BuildComponents[componentId] = (component, build.BuildComponents[componentId].Item2 + count);
else
build.BuildComponents.Add(componentId, (component, count));
build.Price += component.Cost * count;
_buildLogic.Update(
new BuildBindingModel
{
Id = buildId,
Vendor = build.Vendor,
Name = build.Name,
Price = build.Price,
BuildComponents = build.BuildComponents
});
Response.Redirect("Components");
}
[HttpGet]
public IActionResult RemoveComponentFromBuild()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Assemblies = _buildLogic.ReadList(null);
ViewBag.Components = _componentLogic.ReadList(
new ComponentSearchModel
{
StoreKeeperId = APIClient.Client!.Id
}
);
return View();
}
[HttpPost]
public void RemoveComponentFromBuild(int buildId, int componentId, int count)
{
if (buildId == 0)
{
throw new Exception("chose build");
}
if (componentId == 0)
{
throw new Exception("chose component");
}
if (count <= 0)
{
throw new Exception("chose count");
}
BuildViewModel build = _buildLogic.ReadElement(new BuildSearchModel { Id = buildId })!;
ComponentViewModel component = _componentLogic.ReadElement(new ComponentSearchModel { Id = componentId })!;
if (build.BuildComponents.ContainsKey(componentId))
{
if (build.BuildComponents[componentId].Item2 < count)
throw new Exception("ther is not enough components in build");
if (build.BuildComponents[componentId].Item2 == count)
build.BuildComponents.Remove(componentId);
else
build.BuildComponents[componentId] = (component, build.BuildComponents[componentId].Item2 - count);
}
else
throw new Exception("there is no that component in build");
build.Price -= component.Cost * count;
_buildLogic.Update(
new BuildBindingModel
{
Id = buildId,
Vendor = build.Vendor,
Name = build.Name,
Price = build.Price,
BuildComponents = build.BuildComponents
});
Response.Redirect("Components");
}
[HttpGet]
public IActionResult DeleteComponent()
{
ViewBag.Components = _componentLogic.ReadList(
new ComponentSearchModel
{
StoreKeeperId = APIClient.Client.Id
}
);
return View();
}
[HttpPost]
public void DeleteComponent(int component)
{
if (component == 0)
{
throw new Exception("Chose component");
}
_componentLogic.Delete(
new ComponentBindingModel { Id = component }
);
Response.Redirect("Components");
}
// товары
[HttpGet]
public IActionResult CreateProduct()
{
return View(_componentLogic.ReadList(null));
}
[HttpPost]
public void CreateProduct(double price, string name, List<ComponentSelectionViewModel> components)
{
var selectedComponents = components.Where(g => g.IsSelected && g.Quantity > 0).ToList();
List<ComponentViewModel> componentsViewModels = _componentLogic.ReadList(null)!;
Dictionary<int, (IComponentModel, int)> productComponents = new Dictionary<int, (IComponentModel, int)>();
price = 0;
foreach (var component in selectedComponents)
{
productComponents.Add(component.Id, (
componentsViewModels.FirstOrDefault(x => x.Id == component.Id)!,
component.Quantity
));
price += component.Quantity * componentsViewModels.FirstOrDefault(x => x.Id == component.Id)!.Cost;
}
_productLogic.Create(
new ProductBindingModel
{
Name = name,
Price = price,
StoreKeeper = APIClient.Client!,
ProductComponents = productComponents,
}
);
Response.Redirect("Products");
}
[HttpGet]
public IActionResult UpdateProduct(int productId)
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>($"api/component/getcomponents?clientId={APIClient.Client.Id}");
return View(productId);
}
[HttpPost]
public void UpdateProduct(double price, string name, List<ComponentSelectionViewModel> components, int product)
{
if (product == 0)
{
throw new Exception("chose product");
}
var selectedComponents = components.Where(g => g.IsSelected && g.Quantity > 0).ToList();
List<ComponentViewModel> componentsViewModels = _componentLogic.ReadList(null)!;
Dictionary<int, (IComponentModel, int)> productComponents = new Dictionary<int, (IComponentModel, int)>();
price = 0;
// добавляем в
foreach (var component in selectedComponents)
{
productComponents.Add(component.Id, (
componentsViewModels.FirstOrDefault(x => x.Id == component.Id)!,
component.Quantity
));
price += component.Quantity * componentsViewModels.FirstOrDefault(x => x.Id == component.Id)!.Cost;
}
_productLogic.Update(
new ProductBindingModel
{
Id = product,
StoreKeeper = APIClient.Client,
Price = price,
Name = name,
ProductComponents = productComponents
});
Response.Redirect("Products");
}
[HttpGet]
public IActionResult DeleteProduct()
{
var products = _productLogic.ReadList(
new ProductSearchModel
{
StoreKeeperId = APIClient.Client!.Id
}
);
ViewBag.Products = products;
return View();
}
[HttpPost]
public void DeleteProduct(int product)
{
_productLogic.Delete(
new ProductBindingModel { Id = product }
);
Response.Redirect("Products");
}
// заказы
//[HttpGet]
//public IActionResult CreateOrder()
//{
// ViewBag.Products = _productLogic.ReadList(
// new ProductSearchModel
// {
// StoreKeeperId = APIClient.Client!.Id
// }
// );
// return View();
//}
//[HttpPost]
//public void CreateOrder(DateTime date, string address, int product)
//{
// _orderForProductsLogic.Create(
// new OrderForProductsBindingModel
// {
// OrderDate = date,
// DeliveryAddres = address,
// ProductId = product,
// }
// );
// Response.Redirect("Orders");
//}
//[HttpGet]
//public IActionResult UpdateOrder()
//{
// ViewBag.Products = _productLogic.ReadList(
// new ProductSearchModel
// {
// StoreKeeperId = APIClient.Client!.Id
// }
// );
// ViewBag.OrdersForProducts = _orderForProductsLogic.ReadList(
// new OrderForProductsSearchModel
// {
// StoreKeeperId = APIClient.Client!.Id
// }
// );
// return View();
//}
//[HttpPost]
//public void UpdateOrder(DateTime date, string address, int product, int order)
//{
// _orderForProductsLogic.Update(
// new OrderForProductsBindingModel
// {
// Id = order,
// OrderDate = date,
// DeliveryAddres = address,
// ProductId = product
// }
// );
// Response.Redirect("Orders");
//}
//[HttpGet]
//public IActionResult DeleteOrder()
//{
// ViewBag.OrdersForProducts = _orderForProductsLogic.ReadList(
// new OrderForProductsSearchModel
// {
// StoreKeeperId = APIClient.Client!.Id
// }
// );
// return View();
//}
//[HttpPost]
//public void DeleteOrder(int order)
//{
// _orderForProductsLogic.Delete(
// new OrderForProductsBindingModel
// {
// Id = order,
// }
// );
// Response.Redirect("Orders");
//}
}
}