using CanteenContracts.BindingModels; using CanteenContracts.View; using CanteenManagerApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; namespace CanteenManagerApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } return View(); } public IActionResult Cooks() { if (APIClient.Manager == null) { return Redirect("~/Home/Enter"); } ViewBag.Cooks = APIClient.GetRequest>($"api/main/getcooklist?managerId={APIClient.Manager.Id}"); return View(); } [HttpGet] public IActionResult Products() { ViewBag.Products = new List(); return View(); } [HttpGet] public IActionResult Dishes() { ViewBag.Dishes = new List(); return View(); } public IActionResult Privacy() { return View(); } [HttpGet] public IActionResult CreateCook() { return View(); } [HttpPost] public void CreateCook(string FIO, string position) { if (APIClient.Manager == null) { throw new Exception("Доступ возможен только авторизованным пользователям"); } if (string.IsNullOrEmpty(FIO)) { throw new Exception("ФИО не должно быть пустым"); } APIClient.PostRequest("api/main/createcook", new CookBindingModel { ManagerId = APIClient.Manager.Id, FIO = FIO, Position = position }); Response.Redirect("Cooks"); } [HttpPost] public IActionResult DeleteCook(int CookId) { APIClient.PostRequest("api/main/deletecook", new CookBindingModel { Id = CookId }); ; return Redirect("~/Home/Cooks"); } [HttpGet] public IActionResult CreateProduct() { return View(); } [HttpGet] public IActionResult CreateDish() { 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"); return; } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } }