using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.ViewModels; using ComputerStoreEmployeeApp.Models; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Mvc; using System.Data; using System.Diagnostics; namespace ComputerStoreEmployeeApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger 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 = Task.Run(() => APIClient.GetRequest>("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 = Task.Run(() => APIClient.GetRequest>("api/main/getcomponentslist")).Result; return View(); } [HttpPost] public IActionResult ComponentDelete(int component) { try { Task.Run(() => APIClient.DeleteRequest($"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(Task.Run(() => APIClient.GetRequest>("api/main/getcomponentslist")).Result); } [HttpGet] public IActionResult ProductMenu() { return View(); } [HttpGet] public IActionResult ProductAdd() { return View(); } [HttpPost] public void ProductAdd(string val) { } [HttpGet] public IActionResult ProductUpdate() { return View(); } [HttpPost] public void ProductUpdate(string val) { } [HttpGet] public IActionResult ProductDelete() { return View(); } [HttpPost] public void ProductDelete(string val) { } [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($"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 }); } } }