using Microsoft.AspNetCore.Mvc; using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.SearchModels; using PrecastConcretePlantContracts.ViewModels; using PrecastConcretePlantDataModels.Models; using PrecastConcretePlantShopApp.Models; using System.Diagnostics; namespace PrecastConcretePlantShopApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { if (!APIClient.AuthorizationDone) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/shop/getshoplist")); } public IActionResult Privacy() { return View(); } [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 password) { if (string.IsNullOrEmpty(password)) { throw new Exception("Введите пароль"); } else if (!APIClient.TryLogin(password)) { throw new Exception("Неверный пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Create() { return View(); } [HttpPost] public void Create(string shopName, string address, DateTime dateOpening, int capacity) { if (!APIClient.AuthorizationDone) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(shopName)) { throw new Exception("Название магазина не указано"); } if (string.IsNullOrEmpty(address)) { throw new Exception("Адрес магазина не указано"); } if (capacity <= 0) { throw new Exception("Вместимость магазина должна быть больше нуля"); } APIClient.PostRequest("api/shop/createshop", new ShopBindingModel { ShopName = shopName, Address = address, DateOpening = dateOpening, Capacity = capacity }); Response.Redirect("Index"); } [HttpGet] public Tuple? GetShopWithReinforceds(int shopId) { var result = APIClient.GetRequest, List>> ($"api/shop/getshopwithreinforceds?shopId={shopId}"); if (result == null) { return default; } string reinforcedTable = ""; for (int i = 0; i < result.Item2.Count; i++) { var reinforced = result.Item2[i]; var count = result.Item3[i]; reinforcedTable += ""; reinforcedTable += $"{reinforced.ReinforcedName}"; reinforcedTable += $"{reinforced.Price}"; reinforcedTable += $"{count}"; reinforcedTable += ""; } return Tuple.Create(result.Item1, reinforcedTable); } [HttpGet] public IActionResult Update() { if (!APIClient.AuthorizationDone) { return Redirect("~/Home/Enter"); } ViewBag.Shops = APIClient.GetRequest>($"api/shop/getshoplist"); return View(); } [HttpPost] public void Update(int shop, string shopName, string address, DateTime dateOpening, int capacity) { if (!APIClient.AuthorizationDone) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(shopName)) { throw new Exception("Название магазина не указано"); } if (string.IsNullOrEmpty(address)) { throw new Exception("Адрес магазина не указано"); } if (capacity <= 0) { throw new Exception("Вместимость магазина должна быть больше нуля"); } var shopReinforceds = APIClient.GetRequest>($"api/shop/getshopreinforceds?shopId={shop}"); APIClient.PostRequest("api/shop/updateshop", new ShopBindingModel { Id = shop, ShopName = shopName, Address = address, DateOpening = dateOpening.Date, Capacity = capacity, ShopReinforceds = shopReinforceds! }); Response.Redirect("Index"); } [HttpGet] public IActionResult Delete() { if (!APIClient.AuthorizationDone) { return Redirect("~/Home/Enter"); } ViewBag.Shops = APIClient.GetRequest>($"api/shop/getshoplist"); return View(); } [HttpPost] public void Delete(int shop) { if (!APIClient.AuthorizationDone) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIClient.PostRequest("api/shop/deleteshop", new ShopBindingModel { Id = shop, }); Response.Redirect("Index"); } [HttpGet] public IActionResult AddReinforced() { ViewBag.Shops = APIClient.GetRequest>("api/shop/getshoplist"); ViewBag.Reinforceds = APIClient.GetRequest>("api/main/getreinforcedlist"); return View(); } [HttpPost] public void AddReinforced(int shop, int reinforced, int count) { if (!APIClient.AuthorizationDone) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (count <= 0) { throw new Exception("Количество должно быть больше 0"); } APIClient.PostRequest("api/shop/addreinforcedinshop", Tuple.Create( new ShopSearchModel() { Id = shop }, new ReinforcedViewModel() { Id = reinforced }, count )); Response.Redirect("Index"); } } }