PIbd-23_Ivanov_V.N._Pizzeria/Pizzeria/PizzeriaClientApp/Controllers/HomeController.cs

160 lines
5.1 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
using PizzeriaClientApp.Models;
2024-03-21 13:21:25 +04:00
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using System.Diagnostics;
2024-03-21 13:21:25 +04:00
namespace PizzeriaClientApp.Controllers
2024-03-21 13:21:25 +04:00
{
public class HomeController : Controller
2024-03-21 21:18:16 +04:00
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
2024-03-21 21:00:28 +04:00
{
_logger = logger;
}
2024-03-21 13:21:25 +04:00
public IActionResult Index()
2024-03-21 21:00:28 +04:00
{
if (APIClient.Client == null)
2024-03-21 21:00:28 +04:00
{
return Redirect("~/Home/Enter");
2024-03-21 21:00:28 +04:00
}
return View(APIClient.GetRequest<List<OrderViewModel>>($"api/main/getorders?clientId={APIClient.Client.Id}"));
2024-03-21 21:00:28 +04:00
}
2024-03-21 13:21:25 +04:00
[HttpGet]
public IActionResult Privacy()
2024-03-21 21:00:28 +04:00
{
if (APIClient.Client == null)
2024-03-21 21:00:28 +04:00
{
return Redirect("~/Home/Enter");
2024-03-21 21:00:28 +04:00
}
return View(APIClient.Client);
2024-03-21 21:00:28 +04:00
}
2024-03-21 13:21:25 +04:00
[HttpPost]
public void Privacy(string login, string password, string fio)
2024-03-21 21:00:28 +04:00
{
if (APIClient.Client == null)
2024-03-21 21:00:28 +04:00
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
2024-03-21 21:00:28 +04:00
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
2024-04-02 21:27:34 +04:00
{
throw new Exception("Введите логин, пароль и ФИО");
2024-04-02 21:27:34 +04:00
}
APIClient.PostRequest("api/client/updatedata", new ClientBindingModel
{
Id = APIClient.Client.Id,
ClientFIO = fio,
Email = login,
Password = password
});
APIClient.Client.ClientFIO = fio;
APIClient.Client.Email = login;
APIClient.Client.Password = password;
Response.Redirect("Index");
2024-03-21 21:00:28 +04:00
}
2024-03-21 13:21:25 +04:00
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
2024-03-21 21:00:28 +04:00
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
2024-03-21 21:00:28 +04:00
}
2024-03-21 13:21:25 +04:00
[HttpGet]
public IActionResult Enter()
2024-03-21 21:00:28 +04:00
{
return View();
2024-03-21 21:00:28 +04:00
}
2024-03-21 13:21:25 +04:00
[HttpPost]
public void Enter(string login, string password)
2024-03-21 21:00:28 +04:00
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
2024-03-21 21:00:28 +04:00
{
throw new Exception("Введите логин и пароль");
2024-03-21 21:00:28 +04:00
}
APIClient.Client = APIClient.GetRequest<ClientViewModel>($"api/client/login?login={login}&password={password}");
if (APIClient.Client == null)
2024-03-21 21:00:28 +04:00
{
throw new Exception("Неверный логин/пароль");
2024-03-21 21:00:28 +04:00
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(string login, string password, string fio)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
2024-03-21 21:00:28 +04:00
{
throw new Exception("Введите логин, пароль и ФИО");
2024-04-02 21:27:34 +04:00
}
APIClient.PostRequest("api/client/register", new ClientBindingModel
2024-04-02 21:27:34 +04:00
{
ClientFIO = fio,
Email = login,
Password = password
});
Response.Redirect("Enter");
return;
2024-03-21 21:00:28 +04:00
}
2024-03-21 13:21:25 +04:00
[HttpGet]
public IActionResult Create()
2024-03-21 21:00:28 +04:00
{
ViewBag.Pizzas = APIClient.GetRequest<List<PizzaViewModel>>("api/main/getpizzalist");
return View();
}
[HttpPost]
public void Create(int pizza, int count)
{
if (APIClient.Client == null)
2024-04-02 21:27:34 +04:00
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
2024-03-21 21:00:28 +04:00
}
if (count <= 0)
2024-03-21 21:00:28 +04:00
{
throw new Exception("Количество и сумма должны быть больше 0");
2024-03-21 21:00:28 +04:00
}
APIClient.PostRequest("api/main/createorder", new OrderBindingModel
2024-03-21 21:00:28 +04:00
{
ClientId = APIClient.Client.Id,
PizzaId = pizza,
Count = count,
Sum = Calc(count, pizza)
});
Response.Redirect("Index");
}
[HttpPost]
public double Calc(int count, int pizza)
{
var piz = APIClient.GetRequest<PizzaViewModel>($"api/main/getpizza?pizzaId={pizza}");
return count * (piz?.Price ?? 1);
2024-03-21 21:00:28 +04:00
}
2024-04-02 22:17:03 +04:00
[HttpGet]
2024-04-03 09:19:16 +04:00
public IActionResult Mails(int page = 1)
2024-04-02 22:17:03 +04:00
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
2024-04-03 09:19:16 +04:00
page = Math.Max(page, 1);
return View(APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}&page={page}"));
2024-04-02 22:17:03 +04:00
}
2024-04-02 21:27:34 +04:00
}
2024-03-21 13:21:25 +04:00
}