Coursework_ComputerStore_Li.../ComputerStoreEmployeeApp/Controllers/HomeController.cs
2023-05-17 17:54:05 +04:00

574 lines
20 KiB
C#

using ComputerStoreContracts.APIModels;
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models;
using ComputerStoreEmployeeApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Diagnostics;
namespace ComputerStoreEmployeeApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
if (APIClient.Employee == null) { return Redirect("Home/Enter"); }
return View();
}
public IActionResult ComponentMenu()
{
return View();
}
public IActionResult ComponentAdd()
{
return View();
}
[HttpPost]
public IActionResult ComponentAdd(string componentname, double componentprice)
{
try
{
if (string.IsNullOrEmpty(componentname) || string.IsNullOrEmpty(componentprice.ToString()))
{
throw new Exception("Enter component's name and price.");
}
if (!Task.Run(() => APIClient.PostRequest("api/main/insertcomponent", new ComponentBindingModel
{
Name = componentname,
Price = componentprice
})).Result)
{
throw new InvalidOperationException("Component with such name already exists");
}
}
catch (Exception ex)
{
ViewBag.Message = new string(ex.Message.ToString());
return View();
}
return Redirect("ComponentMenu");
}
[HttpGet]
public IActionResult ComponentUpdate()
{
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View();
}
[HttpPost]
public IActionResult ComponentUpdate(int component, string? componentname, double? componentprice)
{
try
{
if (string.IsNullOrEmpty(componentname) && !componentprice.HasValue)
{
throw new Exception("Enter at least one field.");
}
if (string.IsNullOrEmpty(componentname))
{
Task.Run(() => APIClient.PatchRequest("api/main/updatecomponent", new ComponentBindingModel
{
ID = component,
Price = componentprice.Value
}));
}
else if (!componentprice.HasValue)
{
Task.Run(() => APIClient.PatchRequest("api/main/updatecomponent", new ComponentBindingModel
{
ID = component,
Name = componentname
}));
}
else
{
Task.Run(() => APIClient.PatchRequest("api/main/updatecomponent", new ComponentBindingModel
{
ID = component,
Name = componentname,
Price = componentprice.Value
}));
}
}
catch (Exception ex)
{
ViewBag.Message = new string(ex.Message.ToString());
return View();
}
return Redirect("ComponentMenu");
}
[HttpGet]
public IActionResult ComponentDelete()
{
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View();
}
[HttpPost]
public IActionResult ComponentDelete(int component)
{
try
{
Task.Run(() => APIClient.DeleteRequest<string>($"api/main/deletecomponent/{component}"));
}
catch (Exception ex)
{
ViewBag.Message = new string(ex.Message.ToString());
return View();
}
return Redirect("ComponentMenu");
}
[HttpGet]
public IActionResult ComponentCheck()
{
return View(APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result);
}
[HttpGet]
public IActionResult ProductMenu()
{
APIClient.productComponents = new Dictionary<int, (IComponentModel Component, int Quantity)>();
return View();
}
[HttpGet]
public IActionResult ProductAdd()
{
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View(APIClient.productComponents);
}
[HttpPost]
public IActionResult ProductAdd(string productname, double productprice)
{
try
{
if (string.IsNullOrEmpty(productname) || string.IsNullOrEmpty(productprice.ToString()))
{
throw new Exception("Enter product's name or product doesn't have any components.");
}
if (!Task.Run(() => APIClient.PostRequest("api/main/insertproduct", new ProductBindingModel
{
Name = productname,
Price = productprice,
EmployeeID = APIClient.Employee.ID,
ProductComponents = APIClient.productComponents
})).Result)
{
throw new InvalidOperationException("Product with such name already exists");
}
}
catch (Exception ex)
{
APIClient.productComponents.Clear();
ViewBag.Message = new string(ex.Message.ToString());
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View(APIClient.productComponents);
}
APIClient.productComponents.Clear();
return Redirect("ProductMenu");
}
[HttpPost]
public double ProductComponents(int id, int componentquantity)
{
var component = APIClient.GetRequest<ComponentViewModel>($"api/main/getcomponent?id={id}").Result;
if (APIClient.productComponents.ContainsKey(component.ID))
{
APIClient.productComponents[component.ID] = (component, componentquantity);
}
else
{
APIClient.productComponents.Add(component.ID, (component, componentquantity));
}
return APIClient.productComponents.Sum(x => x.Value.Component.Price * x.Value.Quantity);
}
[HttpGet("/Home/ProductUpdate/{id}")]
public IActionResult ProductUpdate(int? id, bool selectChange = false)
{
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
var list = APIClient.GetRequest<List<ProductViewModel>>("api/main/getproductslist").Result;
ViewBag.Products = list;
if (!id.HasValue)
{
var product = list.First();
APIClient.productComponents = product.ProductComponents;
ViewBag.Price = product.Price;
return View(APIClient.productComponents);
}
ViewBag.ID = id;
if (selectChange)
{
var specproduct = list.First(x => x.ID == id);
APIClient.productComponents = specproduct.ProductComponents;
ViewBag.Price = specproduct.Price;
return View(specproduct.ProductComponents);
}
ViewBag.Price = APIClient.productComponents.Sum(x => x.Value.Component.Price * x.Value.Quantity);
return View(APIClient.productComponents);
}
[HttpPost]
public IActionResult ProductUpdate(int id, double productprice, string? productname)
{
try
{
if (!Task.Run(() => APIClient.PatchRequest("api/main/updateproduct", new ProductBindingModel
{
ID = id,
Name = productname,
Price = productprice,
EmployeeID = APIClient.Employee.ID,
ProductComponents = APIClient.productComponents.Where(component => component.Value.Quantity != 0).ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity))
})).Result)
{
throw new InvalidOperationException("Something went wrong.");
}
}
catch (Exception ex)
{
APIClient.productComponents.Clear();
ViewBag.Message = new string(ex.Message.ToString());
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View(APIClient.productComponents);
}
APIClient.productComponents.Clear();
return Redirect("ProductMenu");
}
[HttpGet]
public IActionResult ProductDelete()
{
ViewBag.Products = APIClient.GetRequest<List<ProductViewModel>>("api/main/getproductslist").Result;
return View();
}
[HttpPost]
public IActionResult ProductDelete(int product)
{
try
{
Task.Run(() => APIClient.DeleteRequest<string>($"api/main/deleteproduct/{product}"));
}
catch (Exception ex)
{
ViewBag.Message = new string(ex.Message.ToString());
return View();
}
return Redirect("ProductMenu");
}
[HttpGet]
public IActionResult ProductCheck()
{
return View(APIClient.GetRequest<List<ProductViewModel>>("api/main/getproductslist").Result);
}
[HttpGet]
public IActionResult PCMenu()
{
APIClient.pcComponents = new Dictionary<int, (IComponentModel Component, int Quantity)>();
return View();
}
[HttpGet]
public IActionResult PCAdd(int? request)
{
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
ViewBag.Requests = APIClient.GetRequest<List<RequestViewModel>>($"api/main/getrequestlist?id={null}").Result;
ViewBag.PCComponents = APIClient.pcComponents;
if(request == null)
{
ViewBag.ID = ViewBag.Requests[0].ID;
}
else
{
ViewBag.ID = request;
}
return View(Requests(request));
}
public List<RequestComponentViewModel> Requests(int? id)
{
if (id == null)
{
var wholelist = APIClient.GetRequest<List<RequestComponentViewModel>>($"api/main/getrequestcomponentlist?id={null}").Result;
ViewBag.Requests = APIClient.GetRequest<List<RequestViewModel>>($"api/main/getrequestlist?id={null}").Result;
var componentsList = wholelist.Where(x => x.RequestID == ViewBag.Requests[0].ID).Select(x => new RequestComponentViewModel { ComponentName = x.ComponentName, ComponentCount = x.ComponentCount }).ToList();
return componentsList;
}
var speclist = APIClient.GetRequest<List<RequestComponentViewModel>>($"api/main/getrequestcomponentlist?id={id}").Result;
var specificcomponentList = speclist.Where(x => x.RequestID == id).Select(x => new RequestComponentViewModel { ComponentName = x.ComponentName, ComponentCount = x.ComponentCount }).ToList();
return specificcomponentList;
}
[HttpPost]
public IActionResult PCAdd(string pcname, double pcprice, int request)
{
try
{
if (string.IsNullOrEmpty(pcname) || string.IsNullOrEmpty(pcprice.ToString()))
{
throw new Exception("Enter pc's name or pc doesn't have any components.");
}
if (APIClient.PostRequest("api/main/insertpc",new PCBindingModel
{
Name = pcname,
Price = pcprice,
EmployeeID = APIClient.Employee.ID,
RequestID = request,
PCComponents = APIClient.pcComponents
}).Result)
{
throw new InvalidOperationException("PC with such name already exists");
}
}
catch (Exception ex)
{
APIClient.pcComponents.Clear();
ViewBag.Message = new string(ex.Message.ToString());
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
ViewBag.Requests = APIClient.GetRequest<List<RequestViewModel>>($"api/main/getrequestlist?id={null}").Result;
ViewBag.PCComponents = APIClient.pcComponents;
return View(Requests(request));
}
APIClient.pcComponents.Clear();
return Redirect("PCMenu");
}
[HttpPost]
public double PCComponents(int id, int componentquantity)
{
var component = APIClient.GetRequest<ComponentViewModel>($"api/main/getcomponent?id={id}").Result;
if (APIClient.pcComponents.ContainsKey(component.ID))
{
APIClient.pcComponents[component.ID] = (component, componentquantity);
}
else
{
APIClient.pcComponents.Add(component.ID, (component, componentquantity));
}
return APIClient.pcComponents.Sum(x => x.Value.Component.Price * x.Value.Quantity);
}
[HttpGet("/Home/PCUpdate/{id}")]
public IActionResult PCUpdate(int? id, bool selectChange = false)
{
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
var list = APIClient.GetRequest<List<PCViewModel>>("api/main/getpcslist").Result;
ViewBag.PCs = list;
if (!id.HasValue)
{
var pc = list.First();
APIClient.pcComponents = pc.PCComponents;
ViewBag.Price = pc.Price;
return View(APIClient.pcComponents);
}
ViewBag.ID = id;
if (selectChange)
{
var specpc = list.First(x => x.ID == id);
APIClient.pcComponents = specpc.PCComponents;
ViewBag.Price = specpc.Price;
return View(specpc.PCComponents);
}
ViewBag.Price = APIClient.pcComponents.Sum(x => x.Value.Component.Price * x.Value.Quantity);
return View(APIClient.pcComponents);
}
[HttpPost]
public IActionResult PCUpdate(int id, double pcprice, string? pcname)
{
try
{
if (!Task.Run(() => APIClient.PatchRequest("api/main/updatepc", new PCBindingModel
{
ID = id,
Name = pcname,
Price = pcprice,
EmployeeID = APIClient.Employee.ID,
PCComponents = APIClient.pcComponents.Where(component => component.Value.Quantity != 0).ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity))
})).Result)
{
throw new InvalidOperationException("Something went wrong.");
}
}
catch (Exception ex)
{
APIClient.pcComponents.Clear();
ViewBag.Message = new string(ex.Message.ToString());
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
return View(APIClient.pcComponents);
}
APIClient.pcComponents.Clear();
return Redirect("PCMenu");
}
[HttpGet]
public IActionResult PCDelete()
{
ViewBag.PCs = APIClient.GetRequest<List<PCViewModel>>("api/main/getpcslist").Result;
return View();
}
[HttpPost]
public IActionResult PCDelete(int pc)
{
try
{
Task.Run(() => APIClient.DeleteRequest<string>($"api/main/deletepc/{pc}"));
}
catch (Exception ex)
{
ViewBag.Message = new string(ex.Message.ToString());
return View();
}
return Redirect("PCMenu");
}
[HttpGet]
public IActionResult PCCheck()
{
return View(APIClient.GetRequest<List<PCViewModel>>("api/main/getpcslist").Result);
}
[HttpGet]
public IActionResult Register()
{
ViewBag.ShowTopBar = false;
return View();
}
[HttpPost]
public IActionResult Register(string username, string password, string? firstname, string? middlename,string? lastname)
{
try
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
throw new Exception("Enter login and password.");
}
Task.Run(() => APIClient.PostRequest("api/employee/register", new EmployeeBindingModel
{
Username = username,
Password = password,
FirstName = firstname != null ? firstname : null,
MiddleName = middlename != null ? middlename : null,
LastName = lastname != null ? lastname : null
}));
}
catch (Exception ex)
{
ViewBag.Message = new string(ex.Message.ToString());
ViewBag.ShowTopBar = false;
return View();
}
return Redirect("Enter");
}
[HttpGet]
public IActionResult Enter()
{
ViewBag.ShowTopBar = false;
return View();
}
[HttpPost]
public IActionResult Enter(string login, string password)
{
try
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) { throw new Exception("Enter login and password"); }
APIClient.Employee = APIClient.GetRequest<EmployeeViewModel>($"api/employee/login?login={login}&password={password}").Result;
if (APIClient.Employee == null) { throw new Exception("Incorrect login/password"); }
}
catch (Exception ex)
{
ViewBag.Message = new string(ex.Message.ToString());
ViewBag.ShowTopBar = false;
return View();
}
return Redirect("Index");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}