CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs
Илья Федотов 1a5a000ef4 CostItem create
2024-05-27 22:05:47 +04:00

141 lines
4.1 KiB
C#

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<HomeController> _logger;
public HomeController(ILogger<HomeController> logger) {
_logger = logger;
}
public IActionResult Index() {
if (APIEmployee.Employee == null) {
return Redirect("~/Home/Enter");
}
return View(APIEmployee.GetRequset<List<ProductViewModel>>($"api/main/getproducts?_employeeid={APIEmployee.Employee.ID}"));
}
[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/main/createcostitem", new CostItemBindingModel {
EmployeeID = APIEmployee.Employee.ID,
Name = name,
Price = price,
CostNum = costNum
});
}
[HttpGet]
public IActionResult Create() {
ViewBag.Orders = APIEmployee.GetRequset<List<ProductViewModel>>("api/main/getproductlist");
return View();
}
[HttpPost]
public void Create(int price, int costNum, string productName) {
if (APIEmployee.Employee == null) {
throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ");
}
if (price <= 0) {
throw new Exception("Ñòîèìîñòü òîâàðà äîëæíà áûòü áîëüøå 0");
}
APIEmployee.PostRequest("api/main/createproduct", new ProductBindingModel {
CostItemID = costNum,
ProductName = productName,
Price = Calc(price, costNum)
});
Response.Redirect("Index");
}
private double Calc(int price, int costItem) {
var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"api/main/getcostitem?_employeeID={costItem}");
return price * (_costItem?.Price ?? 1);
}
}
}