PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenManagerApp/Controllers/HomeController.cs
2023-05-17 16:02:26 +03:00

368 lines
11 KiB
C#

using CanteenContracts.BindingModels;
using CanteenContracts.SearchModel;
using CanteenContracts.View;
using CanteenDatabaseImplement.Models;
using CanteenDataModels.Models;
using CanteenManagerApp.Models;
using FluentNHibernate.Conventions;
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Diagnostics;
namespace CanteenManagerApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Index()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpGet]
public IActionResult Enter()
{
return View();
}
[HttpPost]
public void Enter(string login, string password)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
{
throw new Exception("Введите логин и пароль");
}
APIClient.Manager = APIClient.GetRequest<ManagerViewModel>($"api/manager/login?login={login}&password={password}");
if (APIClient.Manager == null)
{
throw new Exception("Неверный логин/пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(string login, string password, string fio, string phoneNumber)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
{
throw new Exception("Введите логин, пароль и ФИО");
}
APIClient.PostRequest("api/manager/register", new ManagerBindingModel
{
FIO = fio,
Login = login,
Password = password,
PhoneNumber = phoneNumber
});
Response.Redirect("Enter");
return;
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[HttpGet]
public IActionResult Cooks()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Cooks = APIClient.GetRequest<List<CookViewModel>>($"api/main/getcooklist? ={APIClient.Manager.Id}");
return View();
}
[HttpGet]
public IActionResult CreateCook()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void CreateCook(string FIO, string position)
{
if (APIClient.Manager == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
if (string.IsNullOrEmpty(FIO))
{
throw new Exception("ФИО не должно быть пустым");
}
APIClient.PostRequest("api/main/createcook", new CookBindingModel
{
ManagerId = APIClient.Manager.Id,
FIO = FIO,
Position = position
});
Response.Redirect("Cooks");
}
[HttpGet]
public IActionResult DeleteCook(int CookId)
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
APIClient.PostRequest("api/main/deletecook", new CookBindingModel { Id = CookId }); ;
return Redirect("~/Home/Cooks");
}
[HttpGet]
public IActionResult UpdateCook()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void UpdateCook(string FIO, string postition)
{
if (APIClient.Manager == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
APIClient.PostRequest("api/main/updatecook", new CookBindingModel
{
Id = APIClient.Manager.Id,
FIO = FIO,
Position = postition
});
Response.Redirect("Cooks");
}
[HttpGet]
public IActionResult Products()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Products = APIClient.GetRequest<List<ProductViewModel>>($"api/main/getproductlist?managerId={APIClient.Manager.Id}");
return View();
}
[HttpGet]
public IActionResult CreateProduct()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Cooks = APIClient.GetRequest<List<CookViewModel>>($"api/main/getcooklist?managerId={APIClient.Manager.Id}");
return View();
}
[HttpPost]
public void CreateProduct(string name, double price)
{
if (APIClient.Manager == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
if (string.IsNullOrEmpty(name))
{
throw new Exception("Наименование продукта не должно быть пустым");
}
APIClient.PostRequest("api/main/createproduct", new ProductBindingModel
{
ManagerId = APIClient.Manager.Id,
ProductName = name,
Price = price
});
Response.Redirect("Products");
}
[HttpGet]
public IActionResult AddCooksToProduct()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Products = APIClient.GetRequest<List<ProductViewModel>>($"api/main/getproductlist?managerId={APIClient.Manager.Id}");
ViewBag.Cooks = APIClient.GetRequest<List<CookViewModel>>($"api/main/getcooklist?managerId={APIClient.Manager.Id}");
return View();
}
[HttpPost]
public void AddCooksToProduct(int selectedProduct, int selectedCook)
{
if (APIClient.Manager == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
if (selectedProduct <= 0)
{
throw new Exception("Должен быть выбран продукт");
}
if (selectedCook <= 0)
{
throw new Exception("Должен быть выбран повар");
}
APIClient.PostRequest("api/main/addcookstoproduct", Tuple.Create
(
new ProductSearchModel { Id = selectedProduct },
new CookViewModel { Id = selectedCook }
));
Response.Redirect("Products");
}
[HttpGet]
public IActionResult DeleteProduct(int productId)
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
APIClient.PostRequest("api/main/deleteproduct", new ProductBindingModel { Id = productId });
return Redirect("~/Home/Products");
}
[HttpGet]
public IActionResult UpdateProduct()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void UpdateProduct(string productName, double price)
{
if (APIClient.Manager == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
APIClient.PostRequest("api/main/updateproduct", new ProductBindingModel
{
Id = APIClient.Manager.Id,
ProductName = productName,
Price = price
});
Response.Redirect("Products");
}
[HttpGet]
public IActionResult Dishes()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Dishes = APIClient.GetRequest<List<DishViewModel>>($"api/main/getdishlist?managerId={APIClient.Manager.Id}");
return View();
}
[HttpGet]
public IActionResult CreateDish()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void CreateDish(string dishName, double price)
{
if (APIClient.Manager == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
if (string.IsNullOrEmpty(dishName))
{
throw new Exception("Наименование блюда не должно быть пустым");
}
APIClient.PostRequest("api/main/createdish", new DishBindingModel
{
ManagerId = APIClient.Manager.Id,
DishName = dishName,
Price = price
});
Response.Redirect("Dishes");
}
[HttpGet]
public IActionResult DeleteDish(int dishId)
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
APIClient.PostRequest("api/main/deletedish", new DishBindingModel { Id = dishId });
return Redirect("~/Home/Dishes");
}
[HttpGet]
public IActionResult UpdateDish()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void UpdateDish(string dishName, double price)
{
if (APIClient.Manager == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
APIClient.PostRequest("api/main/updatedish", new DishBindingModel
{
Id = APIClient.Manager.Id,
DishName = dishName,
Price = price
});
Response.Redirect("Dishes");
}
}
}