163 lines
5.2 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
2024-04-09 17:15:42 +04:00
using SushiBar.BindingModels;
using SushiBar.ViewModels;
using SushiBarClientApp.Models;
2024-04-09 17:15:42 +04:00
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using System.Diagnostics;
namespace SushiBarClientApp.Controllers
{
public class HomeController : Controller
{
2024-05-08 08:59:50 +04:00
private readonly ILogger<HomeController> _logger;
2024-05-08 14:05:44 +04:00
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
2024-05-08 08:59:50 +04:00
public IActionResult Index()
{
if (APIClient.Client == null)
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
return Redirect("~/Home/Enter");
2024-04-09 17:15:42 +04:00
}
2024-06-20 13:33:14 +04:00
return View(APIClient.GetRequest<List<OrderViewModel>>($"api/main/getorders?clientId={APIClient.Client.Id}"));
2024-05-08 08:59:50 +04:00
}
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Client == null)
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
return Redirect("~/Home/Enter");
2024-04-09 17:15:42 +04:00
}
2024-05-08 08:59:50 +04:00
return View(APIClient.Client);
}
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
[HttpPost]
public void Privacy(string login, string password, string fio)
{
if (APIClient.Client == null)
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
2024-04-09 17:15:42 +04:00
}
2024-06-20 13:33:14 +04:00
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
throw new Exception("Введите логин, пароль и ФИО");
2024-04-09 17:15:42 +04:00
}
2024-06-20 13:33:14 +04:00
APIClient.PostRequest("api/client/updatedata", new ClientBindingModel
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
Id = APIClient.Client.Id,
ClientFIO = fio,
Email = login,
Password = password
});
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
APIClient.Client.ClientFIO = fio;
APIClient.Client.Email = login;
APIClient.Client.Password = password;
Response.Redirect("Index");
}
2024-06-20 13:33:14 +04:00
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
2024-05-08 08:59:50 +04:00
public IActionResult Error()
{
2024-06-20 13:33:14 +04:00
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
2024-05-08 08:59:50 +04:00
}
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
[HttpGet]
public IActionResult Enter()
{
return View();
}
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
[HttpPost]
public void Enter(string login, string password)
{
2024-06-20 13:33:14 +04:00
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
throw new Exception("Введите логин и пароль");
2024-04-09 17:15:42 +04:00
}
2024-06-20 13:33:14 +04:00
APIClient.Client = APIClient.GetRequest<ClientViewModel>($"api/client/login?login={login}&password={password}");
2024-05-08 08:59:50 +04:00
if (APIClient.Client == null)
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
throw new Exception("Неверный логин/пароль");
2024-04-09 17:15:42 +04:00
}
2024-05-08 08:59:50 +04:00
Response.Redirect("Index");
}
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
[HttpGet]
public IActionResult Register()
{
return View();
}
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
[HttpPost]
public void Register(string login, string password, string fio)
{
2024-06-20 13:33:14 +04:00
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
throw new Exception("Введите логин, пароль и ФИО");
2024-04-09 17:15:42 +04:00
}
2024-06-20 13:33:14 +04:00
APIClient.PostRequest("api/client/register", new ClientBindingModel
2024-05-08 08:59:50 +04:00
{
ClientFIO = fio,
Email = login,
Password = password
});
Response.Redirect("Enter");
return;
}
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
[HttpGet]
public IActionResult Create()
{
2024-06-20 13:33:14 +04:00
ViewBag.Sushis = APIClient.GetRequest<List<SushiViewModel>>("api/main/getSushilist");
2024-05-08 08:59:50 +04:00
return View();
}
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
[HttpPost]
2024-06-20 13:33:14 +04:00
public void Create(int Sushi, int count)
2024-05-08 08:59:50 +04:00
{
if (APIClient.Client == null)
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
2024-04-09 17:15:42 +04:00
}
2024-05-08 08:59:50 +04:00
if (count <= 0)
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
throw new Exception("Количество и сумма должны быть больше 0");
2024-04-09 17:15:42 +04:00
}
2024-05-08 08:59:50 +04:00
APIClient.PostRequest("api/main/createorder", new OrderBindingModel
{
ClientId = APIClient.Client.Id,
2024-06-20 13:33:14 +04:00
SushiId = Sushi,
2024-05-08 08:59:50 +04:00
Count = count,
2024-06-20 13:33:14 +04:00
Sum = Calc(count, Sushi)
2024-05-08 08:59:50 +04:00
});
Response.Redirect("Index");
}
2024-06-20 13:33:14 +04:00
2024-05-08 08:59:50 +04:00
[HttpPost]
2024-06-20 13:33:14 +04:00
public double Calc(int count, int Sushi)
2024-05-08 08:59:50 +04:00
{
2024-06-20 13:33:14 +04:00
var sus = APIClient.GetRequest<SushiViewModel>($"api/main/getSushi?SushiId={Sushi}");
return count * (sus?.Price ?? 1);
2024-05-08 08:59:50 +04:00
}
[HttpGet]
2024-06-20 13:33:14 +04:00
public IActionResult Mails(int page = 1)
2024-05-08 08:59:50 +04:00
{
if (APIClient.Client == null)
2024-04-09 17:15:42 +04:00
{
2024-05-08 08:59:50 +04:00
return Redirect("~/Home/Enter");
2024-04-09 17:15:42 +04:00
}
2024-06-20 13:33:14 +04:00
page = Math.Max(page, 1);
return View(APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}&page={page}"));
}
}
2024-05-08 08:59:50 +04:00
}
2024-04-09 17:15:42 +04:00