PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantClientApp/Controllers/HomeController.cs

238 lines
7.6 KiB
C#
Raw Normal View History

2024-04-07 00:02:29 +04:00
using AircraftPlantClientApp.Models;
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace AircraftPlantClientApp.Controllers
{
/// <summary>
/// Контроллер
/// </summary>
public class HomeController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger<HomeController> _logger;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
/// <summary>
/// Вывод списка заказов
/// </summary>
/// <returns></returns>
public IActionResult Index()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<OrderViewModel>>($"api/main/getorders?clientId={APIClient.Client.Id}"));
}
/// <summary>
/// Вывод клиента
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Client);
}
/// <summary>
/// Изменение данных клиента
/// </summary>
/// <param name="login"></param>
/// <param name="password"></param>
/// <param name="fio"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void Privacy(string login, string password, string fio)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
{
throw new Exception("Введите логин, пароль и ФИО");
}
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");
}
/// <summary>
/// Вывод ошибки
/// </summary>
/// <returns></returns>
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
/// <summary>
/// Вход клиента
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Enter()
{
return View();
}
/// <summary>
/// Вход клиента
/// </summary>
/// <param name="login"></param>
/// <param name="password"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void Enter(string login, string password)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
{
throw new Exception("Введите логин и пароль");
}
APIClient.Client = APIClient.GetRequest<ClientViewModel>($"api/client/login?login={login}&password={password}");
if (APIClient.Client == null)
{
throw new Exception("Неверный логин/пароль");
}
Response.Redirect("Index");
}
/// <summary>
/// Регистрация клиента
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Register()
{
return View();
}
/// <summary>
/// Регистрация клиента
/// </summary>
/// <param name="login"></param>
/// <param name="password"></param>
/// <param name="fio"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void Register(string login, string password, string fio)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
{
throw new Exception("Введите логин, пароль и ФИО");
}
APIClient.PostRequest("api/client/register", new ClientBindingModel
{
ClientFIO = fio,
Email = login,
Password = password
});
Response.Redirect("Enter");
return;
}
/// <summary>
/// Создание заказа
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Create()
{
ViewBag.Planes = APIClient.GetRequest<List<PlaneViewModel>>("api/main/getplanelist");
return View();
}
/// <summary>
/// Создание заказа
/// </summary>
/// <param name="plane"></param>
/// <param name="count"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void Create(int plane, int count)
{
if (APIClient.Client == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (count <= 0)
{
throw new Exception("Количество и сумма должны быть больше 0");
}
APIClient.PostRequest("api/main/createorder", new OrderBindingModel
{
ClientId = APIClient.Client.Id,
PlaneId = plane,
Count = count,
Sum = Calc(count, plane)
});
Response.Redirect("Index");
}
/// <summary>
/// Подсчет суммы заказа
/// </summary>
/// <param name="count"></param>
/// <param name="plane"></param>
/// <returns></returns>
[HttpPost]
public double Calc(int count, int plane)
{
var prod = APIClient.GetRequest<PlaneViewModel>($"api/main/getplane?planeId={plane}");
return count * (prod?.Price ?? 1);
}
2024-05-04 22:30:27 +04:00
/// <summary>
/// Получение списка писем клиента
/// </summary>
/// <returns></returns>
[HttpGet]
2024-05-06 03:07:09 +04:00
public IActionResult Mails(int page = 1)
2024-05-04 22:30:27 +04:00
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
2024-05-06 03:07:09 +04:00
ViewBag.Page = page;
List<MessageInfoViewModel>? list = APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}&currentPage={page}");
if (list != null && list.Count == 0 && page != 1)
{
page--;
ViewBag.Page = page;
list = APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}&currentPage={page}");
}
return View(list);
2024-05-04 22:30:27 +04:00
}
}
2024-04-07 00:02:29 +04:00
}