PIbd-21_Ihonkina_E.S._Preca.../PrecastConcretePlantShopApp/Controllers/HomeController.cs
2023-06-13 14:33:24 +03:00

213 lines
7.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using PrecastConcretePlantShopApp.Models;
using System.Diagnostics;
namespace PrecastConcretePlantShopApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
if (!APIClient.AuthorizationDone)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist"));
}
public IActionResult Privacy()
{
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("Введите пароль");
}
else if (!APIClient.TryLogin(password))
{
throw new Exception("Неверный пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public void Create(string shopName, string address, DateTime dateOpening, int capacity)
{
if (!APIClient.AuthorizationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(shopName))
{
throw new Exception("Название магазина не указано");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception("Адрес магазина не указано");
}
if (capacity <= 0)
{
throw new Exception("Вместимость магазина должна быть больше нуля");
}
APIClient.PostRequest("api/shop/createshop", new ShopBindingModel
{
ShopName = shopName,
Address = address,
DateOpening = dateOpening,
Capacity = capacity
});
Response.Redirect("Index");
}
[HttpGet]
public Tuple<ShopViewModel, string>? GetShopWithReinforceds(int shopId)
{
var result = APIClient.GetRequest<Tuple<ShopViewModel, List<ReinforcedViewModel>, List<int>>>
($"api/shop/getshopwithreinforceds?shopId={shopId}");
if (result == null)
{
return default;
}
string reinforcedTable = "";
for (int i = 0; i < result.Item2.Count; i++)
{
var reinforced = result.Item2[i];
var count = result.Item3[i];
reinforcedTable += "<tr>";
reinforcedTable += $"<td>{reinforced.ReinforcedName}</td>";
reinforcedTable += $"<td>{reinforced.Price}</td>";
reinforcedTable += $"<td>{count}</td>";
reinforcedTable += "</tr>";
}
return Tuple.Create(result.Item1, reinforcedTable);
}
[HttpGet]
public IActionResult Update()
{
if (!APIClient.AuthorizationDone)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist");
return View();
}
[HttpPost]
public void Update(int shop, string shopName, string address, DateTime dateOpening, int capacity)
{
if (!APIClient.AuthorizationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(shopName))
{
throw new Exception("Название магазина не указано");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception("Адрес магазина не указано");
}
if (capacity <= 0)
{
throw new Exception("Вместимость магазина должна быть больше нуля");
}
var shopReinforceds = APIClient.GetRequest<Dictionary<int, (IReinforcedModel, int)>>($"api/shop/getshopreinforceds?shopId={shop}");
APIClient.PostRequest("api/shop/updateshop", new ShopBindingModel
{
Id = shop,
ShopName = shopName,
Address = address,
DateOpening = dateOpening.Date,
Capacity = capacity,
ShopReinforceds = shopReinforceds!
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Delete()
{
if (!APIClient.AuthorizationDone)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist");
return View();
}
[HttpPost]
public void Delete(int shop)
{
if (!APIClient.AuthorizationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIClient.PostRequest("api/shop/deleteshop", new ShopBindingModel
{
Id = shop,
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult AddReinforced()
{
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist");
ViewBag.Reinforceds = APIClient.GetRequest<List<ReinforcedViewModel>>("api/main/getreinforcedlist");
return View();
}
[HttpPost]
public void AddReinforced(int shop, int reinforced, int count)
{
if (!APIClient.AuthorizationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (count <= 0)
{
throw new Exception("Количество должно быть больше 0");
}
APIClient.PostRequest("api/shop/addreinforcedinshop", Tuple.Create(
new ShopSearchModel() { Id = shop },
new ReinforcedViewModel() { Id = reinforced },
count
));
Response.Redirect("Index");
}
}
}