PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantShopApp/Controllers/HomeController.cs

297 lines
7.5 KiB
C#
Raw Normal View History

2024-04-21 02:28:27 +04:00
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantShopApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace AircraftPlantShopApp.Controllers
{
/// <summary>
/// Контроллер
/// </summary>
public class HomeController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger<HomeController> _logger;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
/// <summary>
/// Вход в приложение
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Enter()
{
return View();
}
/// <summary>
/// Вход в приложение
/// </summary>
/// <param name="password"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void Enter(string password)
{
if (string.IsNullOrEmpty(password))
{
throw new Exception("Введите пароль");
}
if (!APIClient.CheckPassword(password))
{
throw new Exception("Неправильный пароль");
}
Response.Redirect("Index");
}
/// <summary>
/// Получение списка магазинов
/// </summary>
/// <returns></returns>
public IActionResult Index()
{
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshops"));
}
/// <summary>
/// Получение магазина
/// </summary>
/// <param name="shopId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[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 plane = result.Item2[i].Item1;
var count = result.Item2[i].Item2;
table += "<tr>";
table += $"<td>{plane}</td>";
table += $"<td>{count}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
/// <summary>
/// Создать магазин
/// </summary>
/// <returns></returns>
public IActionResult Create()
{
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
return View();
}
/// <summary>
/// Создать магазин
/// </summary>
/// <param name="name"></param>
/// <param name="address"></param>
/// <param name="date"></param>
/// <param name="count"></param>
/// <exception cref="Exception"></exception>
[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,
MaxPlanes = count,
DateOpening = date
});
Response.Redirect("Index");
}
/// <summary>
/// Обновить магазин
/// </summary>
/// <returns></returns>
public IActionResult Update()
{
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
return View();
}
/// <summary>
/// Обновить магазин
/// </summary>
/// <param name="shop"></param>
/// <param name="name"></param>
/// <param name="address"></param>
/// <param name="date"></param>
/// <param name="count"></param>
/// <exception cref="Exception"></exception>
[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/updateshop", new ShopBindingModel
{
Id = shop,
ShopName = name,
Address = address,
DateOpening = date,
MaxPlanes = count
});
Response.Redirect("Index");
}
/// <summary>
/// Удалить магазин
/// </summary>
/// <returns></returns>
public IActionResult Delete()
{
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
return View();
}
/// <summary>
/// Удалить магазин
/// </summary>
/// <param name="shop"></param>
/// <exception cref="Exception"></exception>
[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");
}
/// <summary>
/// Добавить изделие в магазин
/// </summary>
/// <returns></returns>
public IActionResult AddPlane()
{
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshops");
ViewBag.Planes = APIClient.GetRequest<List<PlaneViewModel>>("api/main/getplanelist");
return View();
}
/// <summary>
/// Добавить изделие в магазин
/// </summary>
/// <param name="shop"></param>
/// <param name="plane"></param>
/// <param name="count"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void AddPlane(int shop, int plane, int count)
{
if (APIClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
APIClient.PostRequest("api/shop/AddPlaneInShop", Tuple.Create(
new ShopSearchModel() { Id = shop },
new PlaneViewModel() { Id = plane },
count
));
Response.Redirect("Index");
}
/// <summary>
/// Ошибка
/// </summary>
/// <returns></returns>
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}