274 lines
7.8 KiB
C#
274 lines
7.8 KiB
C#
using ElectronicsShopContracts.BindingModels;
|
|
using ElectronicsShopContracts.ViewModels;
|
|
using ElectronicsShopEmployeeApp.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using System.Diagnostics;
|
|
|
|
namespace ElectronicsShopEmployeeApp.Controllers {
|
|
public class HomeController : Controller {
|
|
private readonly ILogger<HomeController> _logger;
|
|
|
|
public HomeController(ILogger<HomeController> logger) {
|
|
_logger = logger;
|
|
}
|
|
|
|
public IActionResult CostItem() {
|
|
if (APIEmployee.Employee == null) {
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View(APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}"));
|
|
}
|
|
|
|
public IActionResult Index() {
|
|
if (APIEmployee.Employee == null) {
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View(APIEmployee.GetRequset<List<ProductViewModel>>($"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<EmployeeViewModel>($"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
|
|
});
|
|
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult EditCostItem(int id) {
|
|
var _costitem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={id}");
|
|
|
|
if (_costitem == null) {
|
|
return Redirect("/Home/CostItem");
|
|
}
|
|
|
|
var obj = new CostItemViewModel {
|
|
ID = _costitem.ID,
|
|
Name = _costitem.Name,
|
|
Price = _costitem.Price,
|
|
CostNum = _costitem.CostNum,
|
|
};
|
|
|
|
return View(obj);
|
|
}
|
|
|
|
[HttpPost]
|
|
public void EditCostItem(string name, double price, int costNum, int _ID) {
|
|
if (APIEmployee.Employee == null) {
|
|
throw new Exception("Òîëüêî äëÿ àâòîðèçîâàíûõ");
|
|
}
|
|
if (price <= 0) {
|
|
throw new Exception("Ñóììà çàòðàò äîëæíà áûòü áîëüøå 0");
|
|
}
|
|
APIEmployee.PostRequest("api/employee/editcostitem", new CostItemBindingModel {
|
|
ID = _ID,
|
|
Name = name,
|
|
Price = price,
|
|
CostNum = costNum,
|
|
EmployeeID = APIEmployee.Employee.ID
|
|
});
|
|
Response.Redirect("CostItem");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult CreateProduct() {
|
|
ViewBag.CostItems = APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}");
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void CreateProduct(string name, int costitem, double productprice, double price) {
|
|
if (APIEmployee.Employee == null) {
|
|
throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ");
|
|
}
|
|
if (price <= 0) {
|
|
throw new Exception("Ñòîèìîñòü òîâàðà äîëæíà áûòü áîëüøå 0");
|
|
}
|
|
APIEmployee.PostRequest("api/employee/createproduct", new ProductBindingModel {
|
|
CostItemID = costitem,
|
|
ProductName = name,
|
|
Price = Calc(costitem, productprice)
|
|
});
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult DeleteCostItem(int id) {
|
|
var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={id}");
|
|
|
|
if (_costItem == null) {
|
|
return Redirect("/Home/Index");
|
|
}
|
|
|
|
APIEmployee.PostRequest("api/employee/deletecostitem", new CostItemBindingModel { ID = id });
|
|
return RedirectToAction("CostItem");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult EditProduct(int id) {
|
|
var _product = APIEmployee.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={id}");
|
|
|
|
if (_product == null) {
|
|
return Redirect("/Home/Index");
|
|
}
|
|
|
|
ViewBag.CostItems = APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee?.ID}");
|
|
|
|
var obj = new ProductViewModel {
|
|
ProductName = _product.ProductName,
|
|
CostItemID = _product.CostItemID,
|
|
Price = _product.Price,
|
|
ID = _product.ID
|
|
};
|
|
|
|
|
|
if (ViewBag.CostItems.Count != 0) {
|
|
var _costitemLoad = (APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={obj.CostItemID}"));
|
|
|
|
if (_costitemLoad == null) {
|
|
return Redirect("/Home/Index");
|
|
}
|
|
|
|
if (_costitemLoad?.Name != ViewBag.CostItems[0].Name) {
|
|
int index = 0;
|
|
|
|
for (int i = 0; i < ViewBag.CostItems.Count; i++) {
|
|
if (ViewBag.CostItems[i].Name == _costitemLoad?.Name) {
|
|
index = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
var tmp = ViewBag.CostItems[0];
|
|
ViewBag.CostItems[0] = _costitemLoad;
|
|
ViewBag.CostItems[index] = tmp;
|
|
}
|
|
}
|
|
|
|
return View(obj);
|
|
}
|
|
|
|
[HttpPost]
|
|
public void EditProduct(string name, int costitem, double productprice, int _ID, double price) {
|
|
if (APIEmployee.Employee == null) {
|
|
throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ");
|
|
}
|
|
if (price <= 0) {
|
|
throw new Exception("Ñòîèìîñòü òîâàðà äîëæíà áûòü áîëüøå 0");
|
|
}
|
|
APIEmployee.PostRequest("api/employee/editproduct", new ProductBindingModel {
|
|
ID = _ID,
|
|
CostItemID = costitem,
|
|
ProductName = name,
|
|
Price = Calc(costitem, productprice)
|
|
});
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult DeleteProduct(int id) {
|
|
var _product = APIEmployee.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={id}");
|
|
|
|
if (_product == null) {
|
|
return Redirect("/Home/Index");
|
|
}
|
|
|
|
APIEmployee.PostRequest("api/employee/deleteproduct", new ProductBindingModel {
|
|
ID = id
|
|
});
|
|
return RedirectToAction("Index");
|
|
}
|
|
|
|
[HttpPost]
|
|
public double Calc(int costitem, double productprice) {
|
|
var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={costitem}");
|
|
return productprice + (_costItem?.Price ?? 500);
|
|
}
|
|
}
|
|
}
|