PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenVisitorApp/Controllers/HomeController.cs
2023-05-19 03:39:58 +04:00

273 lines
8.4 KiB
C#

using CanteenContracts.BindingModels;
using CanteenContracts.View;
using CanteenVisitorApp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Diagnostics;
namespace CanteenVisitorApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Index()
{
if (APIClient.Visitor == 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.Visitor = APIClient.GetRequest<VisitorViewModel>($"api/visitor/login?login={login}&password={password}");
if (APIClient.Visitor == 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/visitor/register", new VisitorBindingModel
{
FIO = fio,
Login = login,
Password = password,
PhoneNumber = phoneNumber
});
Response.Redirect("Enter");
}
public IActionResult Privacy()
{
return View();
}
[HttpGet]
public IActionResult Tablewares()
{
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Tablewares = APIClient.GetRequest<List<TablewareViewModel>>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}");
return View();
}
[HttpGet]
public IActionResult CreateTableware()
{
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void CreateTableware(string tablewarename)
{
if (APIClient.Visitor == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
APIClient.PostRequest("api/main/createtableware", new TablewareBindingModel
{
VisitorId = APIClient.Visitor.Id,
TablewareName = tablewarename
});
Response.Redirect("Tablewares");
}
[HttpGet]
public IActionResult DeleteTableware()
{
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Tablewares = APIClient.GetRequest<List<TablewareViewModel>>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}");
return View();
}
[HttpPost]
public void DeleteTableware(int id)
{
if (APIClient.Visitor == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
if (id <= 0)
{
throw new Exception("Выберите пhb,jh");
}
APIClient.PostRequest("api/main/DeleteTableware", new TablewareBindingModel
{
Id = id
});
Response.Redirect("Tablewares");
}
[HttpGet]
public IActionResult UpdateTableware()
{
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Tablewares = APIClient.GetRequest<List<TablewareViewModel>>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}");
return View();
}
[HttpPost]
public void UpdateTableware(int id, string tablewarename)
{
if (APIClient.Visitor == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
APIClient.PostRequest("api/main/UpdateTableware", new TablewareBindingModel
{
Id = id,
VisitorId = APIClient.Visitor.Id,
TablewareName = tablewarename
});
Response.Redirect("Tablewares");
}
[HttpGet]
public IActionResult Orders()
{
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Orders = APIClient.GetRequest<List<OrderViewModel>>($"api/main/getorderlist?visitorId={APIClient.Visitor.Id}");
return View();
}
[HttpGet]
public IActionResult CreateOrder()
{
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void CreateOrder(string ordername)
{
if (APIClient.Visitor == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
APIClient.PostRequest("api/main/createorder", new OrderBindingModel
{
VisitorId = APIClient.Visitor.Id,
OrderName = ordername
});
Response.Redirect("Orders");
}
[HttpGet]
public IActionResult DeleteOrder()
{
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Orders = APIClient.GetRequest<List<OrderViewModel>>($"api/main/getorderlist?visitorId={APIClient.Visitor.Id}");
return View();
}
[HttpPost]
public void DeleteOrder(int id)
{
if (APIClient.Visitor == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
if (id <= 0)
{
throw new Exception("Выберите пhb,jh");
}
APIClient.PostRequest("api/main/DeleteOrder", new OrderBindingModel
{
Id = id
});
Response.Redirect("Orders");
}
[HttpGet]
public IActionResult UpdateOrder()
{
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Orders = APIClient.GetRequest<List<OrderViewModel>>($"api/main/getorderlist?visitorId={APIClient.Visitor.Id}");
return View();
}
[HttpPost]
public void UpdateOrder(int id, string ordername)
{
if (APIClient.Visitor == null)
{
throw new Exception("Доступ возможен только авторизованным пользователям");
}
APIClient.PostRequest("api/main/UpdateOrder", new OrderBindingModel
{
Id = id,
VisitorId = APIClient.Visitor.Id,
OrderName = ordername
});
Response.Redirect("Orders");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}