using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.ViewModels; using ElectronicsShopEmployeeApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; namespace ElectronicsShopEmployeeApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult CostItem() { if (APIEmployee.Employee == null) { return Redirect("~/Home/Enter"); } return View(APIEmployee.GetRequset>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}")); } public IActionResult Index() { if (APIEmployee.Employee == null) { return Redirect("~/Home/Enter"); } return View(APIEmployee.GetRequset>($"api/main/getproducts")); } [HttpGet] public IActionResult Privacy() { if (APIEmployee.Employee == null) { return Redirect("~/Home/Enter"); } return View(APIEmployee.Employee); } [HttpPost] public void Privacy(string login, string password, string fio) { if (APIEmployee.Employee == null) { throw new Exception("Вход только для авторизованных"); } if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) { throw new Exception("Введите логин, пароль, ФИО"); } APIEmployee.PostRequest("api/employee/updatedata", new EmployeeBindingModel { ID = APIEmployee.Employee.ID, EmployeeFIO = fio, Login = login, Password = password, }); APIEmployee.Employee.EmployeeFIO = fio; APIEmployee.Employee.Login = login; APIEmployee.Employee.Password = password; Response.Redirect("Index"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } [HttpGet] public IActionResult Enter() { return View(); } [HttpPost] public void Enter(string login, string password) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) { throw new Exception("Введите логин и пароль"); } APIEmployee.Employee = APIEmployee.GetRequset($"api/employee/login?login={login}&password={password}"); if (APIEmployee.Employee == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Register() { return View(); } [HttpPost] public void Register(string login, string password, string fio) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) { throw new Exception("Введите логин, пароль и ФИО"); } APIEmployee.PostRequest("api/employee/register", new EmployeeBindingModel { EmployeeFIO = fio, Login = login, Password = password }); Response.Redirect("Enter"); return; } [HttpGet] public IActionResult CreateCostItem() { return View(); } [HttpPost] public void CreateCostItem(string name, double price, int costNum) { if (APIEmployee.Employee == null) { throw new Exception("Только для авторизованых"); } if (price <= 0) { throw new Exception("Сумма затрат должна быть больше 0"); } APIEmployee.PostRequest("api/employee/createcostitem", new CostItemBindingModel { EmployeeID = APIEmployee.Employee.ID, Name = name, Price = price, CostNum = costNum }); Response.Redirect("CostItem"); } [HttpGet] public IActionResult CreateProduct() { ViewBag.CostItems = APIEmployee.GetRequset>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}"); return View(); } [HttpPost] public void CreateProduct(string name, int costitem, double productprice) { if (APIEmployee.Employee == null) { throw new Exception("Только для авторизованных"); } if (productprice <= 0) { throw new Exception("Стоимость товара должна быть больше 0"); } APIEmployee.PostRequest("api/employee/createproduct", new ProductBindingModel { CostItemID = costitem, ProductName = name, Price = Calc(costitem, productprice) }); Response.Redirect("Index"); } [HttpPost] public double Calc(int costitem, double productprice) { var _costItem = APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={costitem}"); return productprice + (_costItem?.Price ?? 500); } } }