PIbd21_Makarov_DV_FlowerShop/FlowerShop/FlowerShopShopApp/Controllers/HomeController.cs
2024-05-17 01:17:21 +04:00

176 lines
6.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using FlowerShopContracts.ViewModels;
using FlowerShopContracts.BindingModels;
using FlowerShopDataModels.Models;
using FlowerShopShopApp.Models;
using System.Diagnostics;
using FlowerShopContracts.SearchModels;
namespace FlowerShopShopApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
if (!APIClient.AuthenticationDone)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist"));
}
[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("Введите пароль");
}
if (!APIClient.Authentication(password))
{
throw new Exception("Неверный пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Create()
{
if (!APIClient.AuthenticationDone)
{
return Redirect("~/Home/Enter");
}
return View("Shop");
}
[HttpPost]
public void Create(int Id, string shopName, string address, DateTime dateOpening, int maximumFlowers)
{
if (!APIClient.AuthenticationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(shopName))
{
throw new Exception("Название магазина не указано");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception("Адрес магазина не указан");
}
if (maximumFlowers <= 0)
{
throw new Exception("Вместимость магазина должна быть больше нуля");
}
APIClient.PostRequest("api/shop/createshop", new ShopBindingModel
{
Id = Id,
ShopName = shopName,
Address = address,
DateOpening = dateOpening,
MaximumFlowers = maximumFlowers
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Update(int Id)
{
if (!APIClient.AuthenticationDone)
{
return Redirect("~/Home/Enter");
}
return View("Shop", APIClient.GetRequest<ShopViewModel>($"api/shop/getshop?shopId={Id}"));
}
[HttpPost]
public void Update(int Id, string shopName, string address, DateTime dateOpening, int maximumFlowers)
{
if (!APIClient.AuthenticationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(shopName))
{
throw new Exception("Название магазина не указано");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception("Адрес магазина не указан");
}
if (maximumFlowers <= 0)
{
throw new Exception("Вместимость магазина должна быть больше нуля");
}
var shopFlowers = APIClient.GetRequest<Dictionary<int, (IFlowerModel, int)>>($"api/shop/getlistflower?shopId={Id}");
APIClient.PostRequest("api/shop/updateshop", new ShopBindingModel
{
Id = Id,
ShopName = shopName,
Address = address,
DateOpening = dateOpening.Date,
MaximumFlowers = maximumFlowers,
ShopFlowers = shopFlowers
});
Response.Redirect("Index");
}
[HttpPost]
public void Delete(int Id)
{
if (!APIClient.AuthenticationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIClient.PostRequest("api/shop/deleteshop", new ShopBindingModel
{
Id = Id,
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Supply()
{
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist");
ViewBag.Flowers = APIClient.GetRequest<List<FlowerViewModel>>("api/main/getflowerlist");
return View();
}
[HttpPost]
public void Supply(int shop, int flower, int count)
{
if (!APIClient.AuthenticationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (count <= 0)
{
throw new Exception("Количество должно быть больше 0");
}
APIClient.PostRequest("api/shop/makesupply", Tuple.Create(
new ShopSearchModel() { Id = shop },
new FlowerViewModel() { Id = flower },
count
));
Response.Redirect("Index");
}
}
}