PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenVisitorApp/Controllers/HomeController.cs

273 lines
8.4 KiB
C#
Raw Normal View History

2023-05-14 22:24:37 +04:00
using CanteenContracts.BindingModels;
using CanteenContracts.View;
2023-04-09 19:28:29 +04:00
using CanteenVisitorApp.Models;
using Microsoft.AspNetCore.Mvc;
2023-05-19 03:39:58 +04:00
using Microsoft.AspNetCore.Mvc.Rendering;
2023-04-09 19:28:29 +04:00
using System.Diagnostics;
namespace CanteenVisitorApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
2023-05-19 03:39:58 +04:00
[HttpGet]
2023-04-09 19:28:29 +04:00
public IActionResult Index()
{
2023-05-19 03:39:58 +04:00
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
2023-04-09 19:28:29 +04:00
return View();
}
2023-05-19 03:39:58 +04:00
[HttpGet]
public IActionResult Enter()
2023-04-09 19:28:29 +04:00
{
return View();
}
2023-05-19 03:39:58 +04:00
[HttpPost]
public void Enter(string login, string password)
2023-04-09 19:28:29 +04:00
{
2023-05-19 03:39:58 +04:00
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("Неверный логин/пароль");
}
2023-04-09 19:28:29 +04:00
2023-05-19 03:39:58 +04:00
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register()
{
2023-04-09 19:28:29 +04:00
return View();
}
2023-05-19 03:39:58 +04:00
[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();
}
2023-04-09 19:28:29 +04:00
[HttpGet]
public IActionResult Tablewares()
{
2023-05-19 03:39:58 +04:00
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Tablewares = APIClient.GetRequest<List<TablewareViewModel>>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}");
2023-04-09 19:28:29 +04:00
return View();
}
[HttpGet]
2023-05-19 03:39:58 +04:00
public IActionResult CreateTableware()
2023-04-09 19:28:29 +04:00
{
2023-05-19 03:39:58 +04:00
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
2023-04-09 19:28:29 +04:00
return View();
}
2023-05-19 03:39:58 +04:00
[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");
}
2023-04-09 19:28:29 +04:00
[HttpGet]
2023-05-19 03:39:58 +04:00
public IActionResult DeleteTableware()
2023-04-09 19:28:29 +04:00
{
2023-05-19 03:39:58 +04:00
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Tablewares = APIClient.GetRequest<List<TablewareViewModel>>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}");
2023-04-09 19:28:29 +04:00
return View();
}
2023-05-19 03:39:58 +04:00
[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
});
2023-04-09 19:28:29 +04:00
2023-05-19 03:39:58 +04:00
Response.Redirect("Tablewares");
}
2023-04-09 19:28:29 +04:00
[HttpGet]
2023-05-19 03:39:58 +04:00
public IActionResult UpdateTableware()
2023-04-09 19:28:29 +04:00
{
2023-05-19 03:39:58 +04:00
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Tablewares = APIClient.GetRequest<List<TablewareViewModel>>($"api/main/gettablewarelist?visitorId={APIClient.Visitor.Id}");
2023-04-09 19:28:29 +04:00
return View();
}
2023-05-14 22:24:37 +04:00
[HttpPost]
2023-05-19 03:39:58 +04:00
public void UpdateTableware(int id, string tablewarename)
2023-05-14 22:24:37 +04:00
{
2023-05-19 03:39:58 +04:00
if (APIClient.Visitor == null)
2023-05-14 22:24:37 +04:00
{
2023-05-19 03:39:58 +04:00
throw new Exception("Доступ возможен только авторизованным пользователям");
}
APIClient.PostRequest("api/main/UpdateTableware", new TablewareBindingModel
{
Id = id,
VisitorId = APIClient.Visitor.Id,
TablewareName = tablewarename
2023-05-14 22:24:37 +04:00
});
Response.Redirect("Tablewares");
}
2023-05-19 03:39:58 +04:00
[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();
}
2023-05-14 22:24:37 +04:00
2023-04-09 19:28:29 +04:00
[HttpGet]
2023-05-19 03:39:58 +04:00
public IActionResult CreateOrder()
2023-04-09 19:28:29 +04:00
{
2023-05-19 03:39:58 +04:00
if (APIClient.Visitor == null)
{
return Redirect("~/Home/Enter");
}
2023-04-09 19:28:29 +04:00
return View();
}
2023-05-19 03:39:58 +04:00
[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");
}
2023-04-09 19:28:29 +04:00
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}