using CanteenContracts.BindingModels; using CanteenContracts.View; using CanteenVisitorApp.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using System.Diagnostics; namespace CanteenVisitorApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } [HttpGet] public IActionResult Index() { if (APIClient.Visitor == 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.Visitor = APIClient.GetRequest($"api/visitor/login?login={login}&password={password}"); if (APIClient.Visitor == 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/visitor/register", new VisitorBindingModel { FIO = fio, Login = login, Password = password, PhoneNumber = phoneNumber }); Response.Redirect("Enter"); } [HttpGet] public IActionResult Privacy() { return View(); } [HttpGet] public IActionResult Tablewares() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Tablewares = APIClient.GetRequest>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpGet] public IActionResult CreateTableware() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void CreateTableware(string tablewarename) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } APIClient.PostRequest("api/main/createtableware", new TablewareBindingModel { VisitorId = APIClient.Visitor.Id, TablewareName = tablewarename }); Response.Redirect("Tablewares"); } [HttpGet] public IActionResult DeleteTableware() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Tablewares = APIClient.GetRequest>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpPost] public void DeleteTableware(int id) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (id <= 0) { throw new Exception("Выберите пhb,jh"); } APIClient.PostRequest("api/main/DeleteTableware", new TablewareBindingModel { Id = id }); Response.Redirect("Tablewares"); } [HttpGet] public IActionResult UpdateTableware() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Tablewares = APIClient.GetRequest>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpPost] public void UpdateTableware(int id, string tablewarename) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } APIClient.PostRequest("api/main/UpdateTableware", new TablewareBindingModel { Id = id, VisitorId = APIClient.Visitor.Id, TablewareName = tablewarename }); Response.Redirect("Tablewares"); } [HttpGet] public IActionResult Orders() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Orders = APIClient.GetRequest>($"api/main/getorderlist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpGet] public IActionResult CreateOrder() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void CreateOrder(string description) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } APIClient.PostRequest("api/main/createorder", new OrderBindingModel { VisitorId = APIClient.Visitor.Id, Description = description }); Response.Redirect("Orders"); } [HttpGet] public IActionResult DeleteOrder() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Orders = APIClient.GetRequest>($"api/main/getorderlist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpPost] public void DeleteOrder(int id) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (id <= 0) { throw new Exception("Выберите заказ"); } APIClient.PostRequest("api/main/DeleteOrder", new OrderBindingModel { Id = id }); Response.Redirect("Orders"); } [HttpGet] public IActionResult LunchComplete() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Lunches = APIClient.GetRequest>($"api/main/getlunchlist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpPost] public void LunchComplete(int id) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (id <= 0) { throw new Exception("Выберите обед"); } APIClient.PostRequest("api/main/LunchComplete", new LunchBindingModel { Id = id }); Response.Redirect("Lunches"); } [HttpGet] public IActionResult UpdateOrder() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Orders = APIClient.GetRequest>($"api/main/getorderlist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpPost] public void UpdateOrder(int Id, string description) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } APIClient.PostRequest("api/main/updateorder", new OrderBindingModel { Id = Id, Description = description, VisitorId = APIClient.Visitor.Id }); Response.Redirect("Orders"); } [HttpGet] public IActionResult OrderAddTablewares() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.OrderList = APIClient.GetRequest>($"api/main/getorderlist?visitorId={APIClient.Visitor.Id}"); ViewBag.TablewareList = APIClient.GetRequest>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpPost] public void OrderAddTablewares(int selectedOrder, int selectedTableware, int count) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (selectedOrder <= 0) { throw new Exception("Должно быть выбран заказ"); } if (selectedTableware <= 0) { throw new Exception("Должен быть выбран прибор"); } if (count < -1) { throw new Exception("Количество прибора должно быть больше 0"); } APIClient.PostRequest("api/main/orderaddtablewares", Tuple.Create ( new OrderBindingModel { Id = selectedOrder }, new TablewareBindingModel { Id = selectedTableware }, count )); Response.Redirect("Orders"); } [HttpGet] public IActionResult OrderAddCooks() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.OrderList = APIClient.GetRequest>($"api/main/getorderlist?visitorId={APIClient.Visitor.Id}"); //ViewBag.CookList = new List(); ViewBag.CookList = APIClient.GetRequest>("api/main/getcookfulllist"); return View(); } [HttpPost] public void OrderAddCooks(int selectedOrder, int selectedCook) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (selectedOrder <= 0) { throw new Exception("Должно быть выбран заказ"); } if (selectedCook <= 0) { throw new Exception("Должен быть выбран повар"); } APIClient.PostRequest("api/main/OrderAddCooks", Tuple.Create ( new OrderBindingModel { Id = selectedOrder }, new TablewareBindingModel { Id = selectedCook } )); Response.Redirect("Orders"); } [HttpGet] public IActionResult Lunches() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Lunches = APIClient.GetRequest>($"api/main/getlunchlist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpGet] public IActionResult CreateLunch() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void CreateLunch(string name) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } APIClient.PostRequest("api/main/createlunch", new LunchBindingModel { VisitorId = APIClient.Visitor.Id, LunchName = name }); Response.Redirect("Lunches"); } [HttpGet] public IActionResult DeleteLunch() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Lunches = APIClient.GetRequest>($"api/main/getlunchlist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpPost] public void DeleteLunch(int id) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (id <= 0) { throw new Exception("Выберите заказ"); } APIClient.PostRequest("api/main/DeleteLunch", new LunchBindingModel { Id = id }); Response.Redirect("Lunches"); } [HttpGet] public IActionResult UpdateLunch() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.Lunches = APIClient.GetRequest>($"api/main/getlunchlist?visitorId={APIClient.Visitor.Id}"); return View(); } [HttpPost] public void UpdateLunch(int id, string name) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } APIClient.PostRequest("api/main/UpdateLunch", new LunchBindingModel { Id = id, VisitorId = APIClient.Visitor.Id, LunchName = name, }); Response.Redirect("Lunches"); } [HttpGet] public IActionResult LunchAddProducts() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } ViewBag.LunchList = APIClient.GetRequest>($"api/main/getlunchlist?visitorId={APIClient.Visitor.Id}"); ViewBag.ProductList = APIClient.GetRequest>("api/main/getproductfulllist"); return View(); } [HttpPost] public void LunchAddProducts(int selectedLunch, int selectedProduct, int count) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (selectedLunch <= 0) { throw new Exception("Должен быть выбран обед"); } if (selectedProduct <= 0) { throw new Exception("Должен быть выбран продукт"); } if (count <= 0) { throw new Exception("Количество продукта должно быть больше 0"); } var product = APIClient.GetRequest($"api/main/getproduct?Id={selectedProduct}"); APIClient.PostRequest("api/main/lunchaddproducts", Tuple.Create ( new LunchBindingModel { Id = selectedLunch }, new ProductBindingModel { Id = selectedProduct, Price = product.Price }, count )); Response.Redirect("Lunches"); } [HttpGet] public IActionResult LunchAddOrders() { if (APIClient.Visitor == null) { return Redirect("~/Home/Enter"); } // Получение данных и сохранение их в переменные var lunchList = APIClient.GetRequest>($"api/main/getlunchlist?visitorId={APIClient.Visitor.Id}"); var orderList = APIClient.GetRequest>($"api/main/getorderlist?visitorId={APIClient.Visitor.Id}"); // Сохранение данных в ViewBag ViewBag.LunchList = lunchList; ViewBag.OrderList = orderList; // Возвращение представления return View(); } [HttpPost] public void LunchAddOrders(int selectedLunch, int selectedOrder) { if (APIClient.Visitor == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (selectedLunch <= 0) { throw new Exception("Должен быть выбран обед"); } if (selectedOrder <= 0) { throw new Exception("Должен быть выбран заказ"); } APIClient.PostRequest("api/main/lunchaddorders", Tuple.Create ( new LunchBindingModel { Id = selectedLunch }, new OrderBindingModel { Id = selectedOrder } )); Response.Redirect("Lunches"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } [HttpGet] public IActionResult Report() { ViewBag.LunchList = APIClient.GetRequest>($"api/main/getlunchlist?visitorId={APIClient.Visitor.Id}"); return View(new ReportBindingModel()); } [HttpPost] public void ReportPdf(ReportBindingModel model) { model.UserId = APIClient.Visitor.Id; APIClient.PostRequest("api/main/SaveLunchesToPDF", model); Response.Redirect("Index"); } [HttpPost] public void ReportXsl(ReportBindingModel model) { model.UserId = APIClient.Visitor.Id; APIClient.PostRequest("api/main/SaveCooksToXSL", model); Response.Redirect("Index"); } [HttpPost] public void ReportWord(ReportBindingModel model) { model.UserId = APIClient.Visitor.Id; APIClient.PostRequest("api/main/SaveCooksToWORD", model); Response.Redirect("Index"); } [HttpPost] public void ReportEmail(ReportBindingModel model) { model.UserId = APIClient.Visitor.Id; APIClient.PostRequest($"api/visitor/SendEmail", new MailSendInfoBindingModel { MailAddress = APIClient.Visitor.Login, Subject = "Отчет", report = model }); Response.Redirect("Report"); } } }