using IceCreamShopContracts.BindingModels; using IceCreamShopContracts.ViewModels; using IceCreamShopApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using System.Net; using IceCreamShopContracts.SearchModels; namespace IceCreamShopApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { if (!APIClient.IsAuthenticated) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/shop/getshoplist")); } [HttpGet] public IActionResult Enter() { return View(); } [HttpPost] public void Enter(string password) { if (string.IsNullOrEmpty(password)) { throw new Exception("Введите пароль"); } if (!APIClient.Authentication(password)) { throw new Exception("Неверный пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Create() { if (!APIClient.IsAuthenticated) { return Redirect("~/Home/Enter"); } return View("Shop"); } [HttpPost] public void Create(string shopName, string address, DateTime dateOpening, int iceCreamsMaximum) { if (string.IsNullOrEmpty(shopName)) { throw new Exception("Название магазина не указано"); } if (string.IsNullOrEmpty(address)) { throw new Exception("Адрес магазина не указан"); } if (iceCreamsMaximum <= 0) { throw new Exception("Вместимость магазина должна быть больше нуля"); } APIClient.PostRequest("api/shop/createshop", new ShopBindingModel { ShopName = shopName, Address = address, DateOpening = dateOpening, IceCreamsMaximum = iceCreamsMaximum }); Response.Redirect("Index"); } [HttpGet] public IActionResult Update(int id) { if (!APIClient.IsAuthenticated) { return Redirect("~/Home/Enter"); } return View("Shop", APIClient.GetRequest($"api/shop/getshop?shopId={id}")); } [HttpPost] public void Update(int id, string shopName, string address, DateTime dateOpening, int iceCreamsMaximum) { if (string.IsNullOrEmpty(shopName)) { throw new Exception("Название магазина не указано"); } if (string.IsNullOrEmpty(address)) { throw new Exception("Адрес магазина не указан"); } if (iceCreamsMaximum <= 0) { throw new Exception("Вместимость магазина должна быть больше нуля"); } APIClient.PostRequest($"api/shop/updateshop", new ShopBindingModel { Id = id, ShopName = shopName, Address = address, DateOpening = dateOpening, IceCreamsMaximum = iceCreamsMaximum }); Response.Redirect("../Index"); } [HttpPost] public void Delete(int id) { APIClient.PostRequest($"api/shop/deleteshop", new ShopBindingModel { Id = id, }); Response.Redirect("../Index"); } [HttpGet] public IActionResult MakeShipment() { if(!APIClient.IsAuthenticated) { return Redirect("~/Home/Enter"); } ViewBag.Shops = APIClient.GetRequest>($"api/shop/getshoplist"); ViewBag.IceCreams = APIClient.GetRequest>($"api/main/geticecreamlist"); return View(); } [HttpPost] public void MakeShipment(int shop, int iceCream, int count) { if (count <= 0) { throw new Exception("Количество мороженого в поставке должно быть больше 0"); } APIClient.PostRequest("api/shop/makeshipment", Tuple.Create( new ShopSearchModel() { Id = shop }, new IceCreamViewModel() { Id = iceCream }, 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 }); } } }