using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.ViewModels; using SushiBarStoresMvc.Models; namespace SushiBarStoresMvc.Controllers; public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { return View(); } [HttpGet] public IActionResult Enter() { return View(); } [HttpPost] public void Enter(string password) { if (string.IsNullOrEmpty(password)) { throw new Exception("Enter password"); } ApiStores.Access = password.Equals(ApiStores.Password); if (ApiStores.Access == false) { throw new Exception("Invalid password"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Create() { if (ApiStores.Access == false) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void Create(string name, string address, DateTime date, int count) { if (ApiStores.Access == false) { throw new Exception("Need auth"); } if (string.IsNullOrEmpty(name)) { throw new Exception("Enter name"); } if (string.IsNullOrEmpty(address)) { throw new Exception("Enter address"); } if (count <= 0) { throw new Exception("Enter count"); } ApiStores.PostRequest("api/store/CreateStore", new StoreBindingModel { StoreName = name, StoreAddress = address, maxSushi = count, OpeningDate = date }); Response.Redirect("Index"); } [HttpGet] public IActionResult Delete() { if (ApiStores.Access == false) { return Redirect("~/Home/Enter"); } ViewBag.Stores = ApiStores.GetRequest>("api/store/getstores"); return View(); } [HttpPost] public void Delete(int store) { if (ApiStores.Access == false) { throw new Exception("Need auth"); } ApiStores.PostRequest("api/store/DeleteStore", new StoreBindingModel { Id = store }); Response.Redirect("Index"); } [HttpGet] public IActionResult Update() { if (ApiStores.Access == false) { return Redirect("~/Home/Enter"); } ViewBag.Stores = ApiStores.GetRequest>("api/store/GetStores"); return View(); } [HttpPost] public void Update(int store, string name, string address, DateTime date, int count) { if (ApiStores.Access == false) { throw new Exception("Need auth"); } if (string.IsNullOrEmpty(name)) { throw new Exception("Name of store must be not null"); } if (string.IsNullOrEmpty(address)) { throw new Exception("Address of store must be not null"); } if (count <= 0) { throw new Exception("Quantity of store must be more then zero"); } ApiStores.PostRequest("api/store/UpdateData", new StoreBindingModel { Id = store, StoreName = name, StoreAddress = address, OpeningDate = date, maxSushi = count }); Response.Redirect("Index"); } [HttpGet] public Tuple? GetStore(int storeId) { if (ApiStores.Access == false) { throw new Exception("Need auth"); } var result = ApiStores.GetRequest>>>($"api/store/GetStore?storeId={storeId}"); if (result == null) { return default; } var table = ""; foreach (var (sushi, count) in result.Item2) { table += $"{sushi}{count}"; } return Tuple.Create(result.Item1, table); } public IActionResult Supply() { if (ApiStores.Access == false) { return Redirect("~/Home/Enter"); } ViewBag.Stores = ApiStores.GetRequest>("api/store/GetStores"); ViewBag.Packages = ApiStores.GetRequest>("api/main/GetSushiList"); return View(); } [HttpPost] public void Supply(int store, int package, int count) { if (ApiStores.Access == false) { throw new Exception("Need auth"); } ApiStores.PostRequest("api/store/Supply", Tuple.Create( new StoreSearchModel { Id = store }, new SushiViewModel { Id = package }, count )); Response.Redirect("Index"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } }