PIbd-23_Polevoy_S.V._Flower.../FlowerShop/FlowerShopShopApp/Controllers/HomeController.cs

171 lines
5.2 KiB
C#
Raw Normal View History

using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.ViewModels;
using FlowerShopShopApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace FlowerShopShopApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
if (!APIClient.isAuth)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ShopViewModel>>($"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("Максимальное количество букетов должно быть больше чем 0");
}
APIClient.PostRequest("api/Shop/CreateShop", new ShopBindingModel
{
Name = name,
Address = address,
OpeningDate = openingDate,
MaxCountBouquets = count,
ShopBouquets = new()
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Delete()
{
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("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, ShopBouquets = new() });
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Update()
{
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist");
return View();
}
[HttpPost]
public void Update(int shop, string name, string address, DateTime openingDate, int count)
{
if (!APIClient.isAuth)
{
throw new Exception("Доступ к этой странице возможен только для авторизованных пользователей");
}
APIClient.PostRequest($"api/shop/updateshop", new ShopBindingModel
{
Id = shop,
Name = name,
Address = address,
OpeningDate = openingDate,
MaxCountBouquets = count,
});
Response.Redirect("Index");
}
[HttpGet]
public ShopViewModel? GetShop(int shopId)
{
return APIClient.GetRequest<ShopViewModel>($"api/Shop/GetShop?shopId={shopId}");
}
public IActionResult Supply()
{
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/Shop/GetShopList");
ViewBag.Bouquets = APIClient.GetRequest<List<BouquetViewModel>>("api/Main/GetBouquetList");
return View();
}
[HttpPost]
public void Supply(int shopId, int bouquetId, int count)
{
if (!APIClient.isAuth)
{
throw new Exception("Доступ к этой странице возможен только для авторизованных пользователей");
}
APIClient.PostRequest($"api/Shop/SupplyBouquetsToShop", (new ShopSearchModel { Id = shopId }, new BouquetBindingModel { Id = bouquetId }, 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 });
}
}
}