384 lines
13 KiB
C#
384 lines
13 KiB
C#
using ComputerStoreContracts.APIModels;
|
|
using ComputerStoreContracts.BindingModels;
|
|
using ComputerStoreContracts.ViewModels;
|
|
using ComputerStoreDataModels.Models;
|
|
using ComputerStoreEmployeeApp.Models;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;
|
|
|
|
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 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 });
|
|
}
|
|
}
|
|
} |