PIbd-23-Salin-O.A.-IceCream.../IceCreamShop/IceCreamShopsApp/Controllers/HomeController.cs

199 lines
5.0 KiB
C#
Raw Normal View History

2024-04-14 14:57:27 +04:00
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<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
if (ApiClient.Access == false)
{
return Redirect("~/Home/Enter");
}
return
View(ApiClient.GetRequest<List<ShopViewModel>>($"api/shop/getshops"));
}
2024-04-14 16:06:36 +04:00
2024-04-14 14:57:27 +04:00
[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();
}
2024-04-14 16:06:36 +04:00
2024-04-14 14:57:27 +04:00
[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<List<ShopViewModel>>("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<List<ShopViewModel>>("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,
2024-04-14 16:46:26 +04:00
MaxCapacity = maxCapacity,
2024-04-14 14:57:27 +04:00
});
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<ShopViewModel, string>? GetShop(int shopId)
{
if (ApiClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
var result = ApiClient.GetRequest<Tuple<ShopViewModel, List<Tuple<string, int>>>>($"api/shop/getshop?shopid={shopId}");
if (result == null)
{
return default;
}
string table = "";
for (int i = 0; i < result.Item2.Count; i++)
{
var icecream = result.Item2[i].Item1;
var count = result.Item2[i].Item2;
table += "<tr>";
table += $"<td>{icecream}</td>";
table += $"<td>{count}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
public IActionResult MakeSupply()
{
if (ApiClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = ApiClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
ViewBag.ListIceCream = ApiClient.GetRequest<List<IceCreamViewModel>>("api/main/GetIceCreamList");
return View();
}
[HttpPost]
public void MakeSupply(int shop, int icecream, int count)
{
if (ApiClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
2024-04-14 16:06:36 +04:00
ApiClient.PostRequest("api/shop/makesupply", Tuple.Create(
2024-04-14 14:57:27 +04:00
new ShopSearchModel() { Id = shop },
new IceCreamViewModel() { Id = icecream },
count
));
Response.Redirect("Index");
}
}
}