using CanteenContracts.BindingModels; using CanteenContracts.SearchModel; using CanteenContracts.View; using CanteenContracts.ViewModels; using CanteenDatabaseImplement.Models; using CanteenDataModels.Models; using CanteenManagerApp.Models; using FluentNHibernate.Conventions; using Microsoft.AspNetCore.Mvc; using System.Data; using System.Diagnostics; namespace CanteenManagerApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } [HttpGet] public IActionResult Index() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpGet] public IActionResult Enter() { return View(); } [HttpPost] public void Enter(string login, string password) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) { throw new Exception("Введите логин и пароль"); } APIClient.Manager = APIClient.GetRequest($"api/manager/login?login={login}&password={password}"); if (APIClient.Manager == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Register() { return View(); } [HttpPost] public void Register(string login, string password, string fio, string phoneNumber) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) { throw new Exception("Введите логин, пароль и ФИО"); } APIClient.PostRequest("api/manager/register", new ManagerBindingModel { FIO = fio, Login = login, Password = password, PhoneNumber = phoneNumber }); Response.Redirect("Enter"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } [HttpGet] public IActionResult CookList() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.CookList = APIClient.GetRequest>($"api/main/getcooklist?managerId={APIClient.Manager.Id}"); return View(); } [HttpGet] public IActionResult CookCreate() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void CookCreate(string FIO, string position) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (string.IsNullOrEmpty(FIO)) { throw new Exception("ФИО не должно быть пустым"); } APIClient.PostRequest("api/main/cookcreate", new CookBindingModel { ManagerId = APIClient.Manager.Id, FIO = FIO, Position = position }); Response.Redirect("CookList"); } [HttpGet] public IActionResult CookDelete() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.CookList = APIClient.GetRequest>($"api/main/getcooklist?managerId={APIClient.Manager.Id}"); return View(); } [HttpPost] public void CookDelete(int id) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (id <= 0) { throw new Exception("Выберите повара"); } APIClient.PostRequest("api/main/CookDelete", new CookBindingModel { Id = id }); Response.Redirect("CookList"); } [HttpGet] public IActionResult CookUpdate() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.CookList = APIClient.GetRequest>($"api/main/getcooklist?managerId={APIClient.Manager.Id}"); return View(); } [HttpPost] public void CookUpdate(int Id, string FIO, string position) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } APIClient.PostRequest("api/main/CookUpdate", new CookBindingModel { Id = Id, FIO = FIO, Position = position, ManagerId = APIClient.Manager.Id }); Response.Redirect("CookList"); } [HttpGet] public IActionResult ProductList() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.ProductList = APIClient.GetRequest>($"api/main/getproductlist?managerId={APIClient.Manager.Id}"); return View(); } [HttpGet] public IActionResult ProductCreate() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.CookList = APIClient.GetRequest>($"api/main/getcooklist?managerId={APIClient.Manager.Id}"); return View(); } [HttpPost] public void ProductCreate(string name, double price) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (string.IsNullOrEmpty(name)) { throw new Exception("Наименование продукта не должно быть пустым"); } APIClient.PostRequest("api/main/ProductCreate", new ProductBindingModel { ManagerId = APIClient.Manager.Id, ProductName = name, Price = price }); Response.Redirect("ProductList"); } [HttpGet] public IActionResult ProductAddCooks() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.ProductList = APIClient.GetRequest>($"api/main/getproductlist?managerId={APIClient.Manager.Id}"); ViewBag.CookList = APIClient.GetRequest>($"api/main/getcooklist?managerId={APIClient.Manager.Id}"); return View(); } [HttpPost] public void ProductAddCooks(int selectedProduct, int selectedCook) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (selectedProduct <= 0) { throw new Exception("Должен быть выбран продукт"); } if (selectedCook <= 0) { throw new Exception("Должен быть выбран повар"); } APIClient.PostRequest("api/main/ProductAddCooks", Tuple.Create ( new ProductSearchModel { Id = selectedProduct }, new CookViewModel { Id = selectedCook } )); Response.Redirect("ProductList"); } [HttpGet] public IActionResult ProductDelete() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.ProductList = APIClient.GetRequest>($"api/main/getproductlist?managerId={APIClient.Manager.Id}"); return View(); } [HttpPost] public void ProductDelete(int id) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (id <= 0) { throw new Exception("Выберите продукт"); } APIClient.PostRequest("api/main/ProductDelete", new ProductBindingModel { Id = id }); Response.Redirect("ProductList"); } [HttpGet] public IActionResult ProductUpdate() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.ProductList = APIClient.GetRequest>($"api/main/getproductlist?managerId={APIClient.Manager.Id}"); return View(); } [HttpPost] public void ProductUpdate(int Id, string name, double price) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } APIClient.PostRequest("api/main/productupdate", new ProductBindingModel { Id = Id, ProductName = name, Price = price, ManagerId = APIClient.Manager.Id }); Response.Redirect("ProductList"); } [HttpGet] public IActionResult DishList() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.DishList = APIClient.GetRequest>($"api/main/GetDishList?managerId={APIClient.Manager.Id}"); return View(); } [HttpGet] public IActionResult DishCreate() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void DishCreate(string name) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (string.IsNullOrEmpty(name)) { throw new Exception("Наименование блюда не должно быть пустым"); } APIClient.PostRequest("api/main/dishcreate", new DishBindingModel { ManagerId = APIClient.Manager.Id, DishName = name }); Response.Redirect("DishList"); } [HttpGet] public IActionResult DishAddProducts() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.DishList = APIClient.GetRequest>($"api/main/getdishlist?managerId={APIClient.Manager.Id}"); ViewBag.productList = APIClient.GetRequest>($"api/main/getproductlist?managerId={APIClient.Manager.Id}"); return View(); } [HttpPost] public void DishAddProducts(int selectedDish, int selectedProduct, int count) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (selectedDish <= 0) { throw new Exception("Должно быть выбрано блюдо"); } if (selectedProduct <= 0) { throw new Exception("Должен быть выбран продукт"); } if (count <= 0) { throw new Exception("Количество продукта должно быть больше 0"); } ProductViewModel product = APIClient.GetRequest($"api/main/getproduct?id={selectedProduct}"); APIClient.PostRequest("api/main/dishaddproducts", Tuple.Create ( new DishBindingModel { Id = selectedDish }, new ProductViewModel { Id = selectedProduct, Price = product.Price }, count )); Response.Redirect("DishList"); } [HttpGet] public IActionResult DishDelete() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.DishList = APIClient.GetRequest>($"api/main/getdishlist?managerId={APIClient.Manager.Id}"); return View(); } [HttpPost] public void DishDelete(int id) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (id <= 0) { throw new Exception("Выберите блюдо"); } APIClient.PostRequest("api/main/DishDelete", new DishBindingModel { Id = id }); Response.Redirect("ProductList"); } [HttpGet] public IActionResult DishUpdate() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.DishList = APIClient.GetRequest>($"api/main/getdishlist?managerId={APIClient.Manager.Id}"); return View(); } [HttpPost] public void DishUpdate(int Id, string name, double price) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } APIClient.PostRequest("api/main/DishUpdate", new DishBindingModel { Id = Id, DishName = name, Price = price, ManagerId = APIClient.Manager.Id }); Response.Redirect("DishList"); } [HttpGet] public IActionResult Graphic() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.Model = APIClient.GetRequest>($"api/main/GetGraphic"); return View(); } } }