using IceCreamShopContracts.BindingModels; using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.ViewModels; using IceCreamShopsApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; namespace IceCreamShopsApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { if (ApiClient.Access == false) { return Redirect("~/Home/Enter"); } return View(ApiClient.GetRequest>($"api/shop/getshops")); } [HttpGet] public IActionResult Enter() { return View(); } [HttpPost] public void Enter(string password) { if (string.IsNullOrEmpty(password)) { throw new Exception("Введите пароль"); } ApiClient.CheckPassword(password); if (ApiClient.Access == false) { throw new Exception("Неправильный пароль"); } Response.Redirect("Index"); } public IActionResult Create() { if (ApiClient.Access == false) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void Create(string name, string address, DateTime dateOpen, int maxCapacity) { if (ApiClient.Access == false) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(address) || maxCapacity <= 0) { throw new Exception("Ошибка в введенных данных"); } ApiClient.PostRequest("api/shop/createshop", new ShopBindingModel { ShopName = name, Address = address, MaxCapacity = maxCapacity, DateOpen = dateOpen }); Response.Redirect("Index"); } public IActionResult Delete() { if (ApiClient.Access == false) { return Redirect("~/Home/Enter"); } ViewBag.Shops = ApiClient.GetRequest>("api/shop/getshops"); return View(); } [HttpPost] public void Delete(int shop) { if (ApiClient.Access == false) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } ApiClient.PostRequest("api/shop/deleteshop", new ShopBindingModel { Id = shop }); Response.Redirect("Index"); } public IActionResult Update() { if (ApiClient.Access == false) { return Redirect("~/Home/Enter"); } ViewBag.Shops = ApiClient.GetRequest>("api/shop/getshops"); return View(); } [HttpPost] public void Update(int shop, string name, string address, DateTime dateOpen, int maxCapacity) { if (ApiClient.Access == false) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(address) || maxCapacity <= 0) { throw new Exception("Ошибка в введенных данных"); } ApiClient.PostRequest("api/shop/updateshop", new ShopBindingModel { Id = shop, ShopName = name, Address = address, DateOpen = dateOpen, MaxCapacity = maxCapacity, }); Response.Redirect("Index"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } [HttpGet] public Tuple? GetShop(int shopId) { if (ApiClient.Access == false) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } var result = ApiClient.GetRequest>>>($"api/shop/getshop?shopid={shopId}"); if (result == null) { return default; } string table = ""; result.Item1.ShopIceCreams.Clear(); for (int i = 0; i < result.Item2.Count; i++) { var icecream = result.Item2[i].Item1; var count = result.Item2[i].Item2; table += ""; table += $"{icecream}"; table += $"{count}"; table += ""; } return Tuple.Create(result.Item1, table); } public IActionResult MakeSupply() { if (ApiClient.Access == false) { return Redirect("~/Home/Enter"); } ViewBag.Shops = ApiClient.GetRequest>("api/shop/getshops"); ViewBag.ListIceCream = ApiClient.GetRequest>("api/main/GetIceCreamList"); return View(); } [HttpPost] public void MakeSupply(int shop, int icecream, int count) { if (ApiClient.Access == false) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } ApiClient.PostRequest("api/shop/makesupply", Tuple.Create( new ShopSearchModel() { Id = shop }, new IceCreamViewModel() { Id = icecream }, count )); Response.Redirect("Index"); } } }