using AutomobilePlantContracts.BindingModels; using AutomobilePlantContracts.SearchModels; using AutomobilePlantContracts.ViewModels; using AutomobilePlantShopApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; namespace AutomobilePlantShopApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { if (!APIClient.isAuth) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/shop/getshoplist")); } public IActionResult Privacy() { if (!APIClient.isAuth) { return Redirect("~/Home/Enter"); } return View(); } [HttpGet] public IActionResult Enter() { return View(); } [HttpPost] public void Enter(string password) { if (string.IsNullOrEmpty(password)) { throw new Exception("Введите пароль"); } if (!password.Equals(APIClient.ConfigPassword)) { throw new Exception("Неверный пароль"); } APIClient.isAuth = true; Response.Redirect("Index"); } [HttpGet] public IActionResult Create() { return View(); } [HttpPost] public void Create(string name, string address, DateTime openingDate, int count) { if (!APIClient.isAuth) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (count <= 0) { throw new Exception("Максимальное число изделий в магазине должно быть больше нуля"); } APIClient.PostRequest("api/shop/createshop", new ShopBindingModel { Name = name, MaxCountCars = count, Address = address, OpeningDate = openingDate }); Response.Redirect("Index"); } [HttpGet] public IActionResult Delete() { ViewBag.Shops = APIClient.GetRequest>("api/shop/getshoplist"); return View(); } [HttpPost] public void Delete(int shop) { if (!APIClient.isAuth) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIClient.PostRequest($"api/shop/deleteshop", new ShopBindingModel { Id = shop }); Response.Redirect("Index"); } [HttpGet] public IActionResult Update() { ViewBag.Shops = APIClient.GetRequest>("api/shop/getshoplist"); return View(); } [HttpPost] public void Update(int shop, string shopName, string address, DateTime dateOpening, int count) { if (!APIClient.isAuth) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIClient.PostRequest($"api/shop/updateshop", new ShopBindingModel { Id = shop, Name = shopName, Address = address, OpeningDate = dateOpening, MaxCountCars = 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 }); } } }