using ConfectioneryShopApp; using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.ViewModels; using ConfectioneryShopApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using ConfectioneryContracts.SearchModels; namespace ConfectioneryShopApp.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, MaxCountPastries = maxCount, }); Response.Redirect("Index"); } [HttpGet] public Tuple? GetTablePastriesFromShop(int shop) { var result = APIClient.GetRequest, IEnumerable>?>($"api/shop/getshopwithpastries?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?.PastryName ?? 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 AddPastry() { ViewBag.Shops = APIClient.GetRequest>("api/shop/getshops"); ViewBag.Pastries = APIClient.GetRequest>("api/main/getpastrylist"); return View(); } [HttpPost] public void AddPastry(int shop, int pastry, int count) { if (APIClient.IsAccessAllowed is false) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (count <= 0) { throw new Exception("Количество должно быть больше 0"); } APIClient.PostRequest("api/shop/addpastryinshop", Tuple.Create( new ShopSearchModel() { Id = shop }, new PastryViewModel() { Id = pastry }, count )); Response.Redirect("Index"); } } }