using Microsoft.AspNetCore.Mvc; using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.SearchModels; using PrecastConcretePlantContracts.ViewModels; 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.IsAccessAllowed is false) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/shop/getshops")); } [HttpGet] public IActionResult Privacy() { if (APIClient.IsAccessAllowed is false) { return Redirect("~/Home/Enter"); } 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("Введите пароль"); } APIClient.IsAccessAllowed = password.Equals(APIClient.AccessPassword); if (APIClient.IsAccessAllowed is false) { throw new Exception("Неверный пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Create() { return View(); } [HttpPost] public void Create(string name, string address, int maxCount) { if (APIClient.IsAccessAllowed is false) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (maxCount <= 0) { throw new Exception("Количество и сумма должны быть больше 0"); } if (string.IsNullOrEmpty(name)) { throw new Exception($"Имя магазина не должно быть пустым"); } if (string.IsNullOrEmpty(address)) { throw new Exception($"Адрес магазина не должен быть пустым"); } APIClient.PostRequest("api/shop/createshop", new ShopBindingModel { Name = name, Address = address, ReinforcedMaxCount = maxCount, }); Response.Redirect("Index"); } [HttpGet] public Tuple? GetTableReinforcediesFromShop(int shop) { var result = APIClient.GetRequest, IEnumerable>?>($"api/shop/getshopwithreinforcedies?id={shop}"); if (result == null) { return null; } var shopModel = result.Item1; var resultHtml = ""; foreach (var (item, count) in result.Item2.Zip(result.Item3)) { resultHtml += ""; resultHtml += $"{item?.ReinforcedName ?? string.Empty}"; resultHtml += $"{item?.Price ?? 0}"; resultHtml += $"{count}"; resultHtml += ""; } return Tuple.Create(resultHtml, shopModel); } [HttpGet] public IActionResult Update() { ViewBag.Shops = APIClient.GetRequest>("api/shop/getshops"); return View(); } [HttpPost] public void Update(int shop, string name, string address) { if (APIClient.IsAccessAllowed is false) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(name)) { throw new Exception($"Имя магазина не должно быть пустым"); } if (string.IsNullOrEmpty(address)) { throw new Exception($"Адрес магазина не должен быть пустым"); } APIClient.PostRequest("api/shop/updateshop", new ShopBindingModel { Id = shop, Name = name, Address = address, }); Response.Redirect("Index"); } [HttpGet] public IActionResult Delete() { ViewBag.Shops = APIClient.GetRequest>("api/shop/getshops"); return View(); } [HttpPost] public void Delete(int shop) { if (APIClient.IsAccessAllowed is false) { 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/getshops"); ViewBag.Reinforcedies = APIClient.GetRequest>("api/main/getreinforcedlist"); return View(); } [HttpPost] public void AddReinforced(int shop, int reinforced, int count) { if (APIClient.IsAccessAllowed is false) { 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"); } } }