ISEbd-21_Agliullov.D.A._Con.../ConfectioneryShopApp/Controllers/HomeController.cs

198 lines
5.1 KiB
C#
Raw Normal View History

using ConfectioneryShopApp;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryShopApp.Models;
2023-03-05 13:39:11 +04:00
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using ConfectioneryContracts.SearchModels;
2023-03-05 13:39:11 +04:00
namespace ConfectioneryShopApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
if (APIClient.IsAccessAllowed is false)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshops"));
2023-03-05 13:39:11 +04:00
}
[HttpGet]
2023-03-05 13:39:11 +04:00
public IActionResult Privacy()
{
if (APIClient.IsAccessAllowed is false)
{
return Redirect("~/Home/Enter");
}
2023-03-05 13:39:11 +04:00
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("Введите пароль");
}
APIClient.IsAccessAllowed = password.Equals(APIClient.AccessPassword);
if (APIClient.IsAccessAllowed is false)
{
throw new Exception("Неверный пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public void Create(string name, string address, int maxCount)
{
if (APIClient.IsAccessAllowed is false)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (maxCount <= 0)
{
throw new Exception("Количество и сумма должны быть больше 0");
}
if (string.IsNullOrEmpty(name))
{
throw new Exception($"Имя магазина не должно быть пустым");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception($"Адрес магазина не должен быть пустым");
}
APIClient.PostRequest("api/shop/createshop", new ShopBindingModel
{
Name = name,
Address = address,
MaxCountPastries = maxCount,
});
Response.Redirect("Index");
}
[HttpGet]
public Tuple<string, ShopViewModel?> GetTablePastriesFromShop(int shop)
{
var sh = APIClient.GetRequest<ShopViewModel>($"api/shop/getjsonshop?id={shop}");
var resultHtml = "";
if (sh != null)
{
foreach (var (item, count) in sh.Pastries.Values)
{
resultHtml += "<tr>";
resultHtml += $"<td>{item?.PastryName ?? string.Empty}</td>";
resultHtml += $"<td>{item?.Price ?? 0}</td>";
resultHtml += $"<td>{count}</td>";
resultHtml += "</tr>";
}
}
return Tuple.Create(resultHtml, sh);
}
[HttpGet]
public IActionResult Update()
{
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
return View();
}
[HttpPost]
public void Update(int shop, string name, string address)
{
if (APIClient.IsAccessAllowed is false)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(name))
{
throw new Exception($"Имя магазина не должно быть пустым");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception($"Адрес магазина не должен быть пустым");
}
APIClient.PostRequest("api/shop/updateshop", new ShopBindingModel
{
Id = shop,
Name = name,
Address = address,
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Delete()
{
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
return View();
}
[HttpPost]
public void Delete(int shop)
{
if (APIClient.IsAccessAllowed is false)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIClient.PostRequest("api/shop/deleteshop", new ShopBindingModel
{
Id = shop,
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult AddPastry()
{
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
ViewBag.Pastries = APIClient.GetRequest<List<PastryViewModel>>("api/main/getpastrylist");
return View();
}
[HttpPost]
public void AddPastry(int shop, int pastry, int count)
{
if (APIClient.IsAccessAllowed is false)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (count <= 0)
{
throw new Exception("Количество должно быть больше 0");
}
APIClient.PostRequest("api/shop/addpastryinshop", Tuple.Create(
new ShopSearchModel() { Id = shop },
new PastryBindingModel() { Id = pastry },
count
));
Response.Redirect("Index");
}
2023-03-05 13:39:11 +04:00
}
}