PIbd21_Makarov_DV_FlowerShop/FlowerShop/FlowerShopShopApp/Controllers/HomeController.cs

199 lines
7.0 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 Tuple<ShopViewModel, string>? GetShopFlowers(int shopId)
{
var result = APIClient.GetRequest<Tuple<ShopViewModel, List<FlowerViewModel>, List<int>>>
($"api/shop/getshopflowers?shopId={shopId}");
if (result == null)
{
return default;
}
string flowerTable = "";
for (int i = 0; i < result.Item2.Count; i++)
{
var flower = result.Item2[i];
var count = result.Item3[i];
flowerTable += "<tr>";
flowerTable += $"<td>{flower.FlowerName}</td>";
flowerTable += $"<td>{flower.Price}</td>";
flowerTable += $"<td>{count}</td>";
flowerTable += "</tr>";
}
return Tuple.Create(result.Item1, flowerTable);
}
[HttpGet]
public IActionResult Update()
{
if (!APIClient.AuthenticationDone)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist");
return View("Shop");
}
[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/storereplenishment", Tuple.Create(
new ShopSearchModel() { Id = shop },
new FlowerViewModel() { Id = flower },
count
));
Response.Redirect("Index");
}
}
}