PIbd-21_Balberova_D.N._Sush.../SushiBar/SushiBarShopApp/Controllers/HomeController.cs

208 lines
7.0 KiB
C#

using Microsoft.AspNetCore.Mvc;
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using SushiBarShopApp.Models;
using System.Diagnostics;
namespace SushiBarShopApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Enter()
{
return View();
}
[HttpPost]
public void Enter(string password)
{
if (string.IsNullOrEmpty(password))
{
throw new Exception("Введите пароль");
}
APIClient.Access = password.Equals(APIClient.Password);
if (APIClient.Access == false)
{
throw new Exception("Неправильный пароль");
}
Response.Redirect("Index");
}
public IActionResult Index()
{
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshops"));
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
public IActionResult Create()
{
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void Create(string name, string address, DateTime date, int count)
{
if (APIClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(address)
|| count <= 0)
{
throw new Exception("Введите название");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception("Введите адресс");
}
if (count <= 0)
{
throw new Exception("Введите количество");
}
APIClient.PostRequest("api/shop/createshop", new ShopBindingModel
{
ShopName = name,
Address = address,
MaxCountSushi = count,
DateOpening = date
});
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 date, int count)
{
if (APIClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(name))
{
throw new Exception("Название магазина не может быть пустым");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception("Адрес магазина не может быть пустым");
}
if (count <= 0)
{
throw new Exception("Вместимость магазина не может быть меньше нуля");
}
APIClient.PostRequest("api/shop/updatedata", new ShopBindingModel
{
Id = shop,
ShopName = name,
Address = address,
DateOpening = date,
MaxCountSushi = count
});
Response.Redirect("Index");
}
[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 sushi = result.Item2[i].Item1;
var count = result.Item2[i].Item2;
table += "<tr>";
table += $"<td>{sushi}</td>";
table += $"<td>{count}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
public IActionResult AddSushi()
{
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
ViewBag.ListSushi = APIClient.GetRequest<List<SushiViewModel>>("api/main/getlistsushi");
return View();
}
[HttpPost]
public void AddSushi(int shop, int sushi, int count)
{
if (APIClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
APIClient.PostRequest("api/shop/AddSushiInShop", Tuple.Create(
new ShopSearchModel() { Id = shop },
new SushiViewModel() { Id = sushi },
count
));
Response.Redirect("Index");
}
}
}