using AircraftPlantClientApp.Models;
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace AircraftPlantClientApp.Controllers
{
///
/// Контроллер
///
public class HomeController : Controller
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Конструктор
///
///
public HomeController(ILogger logger)
{
_logger = logger;
}
///
/// Вывод списка заказов
///
///
public IActionResult Index()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest>($"api/main/getorders?clientId={APIClient.Client.Id}"));
}
///
/// Вывод клиента
///
///
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Client);
}
///
/// Изменение данных клиента
///
///
///
///
///
[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");
}
///
/// Вывод ошибки
///
///
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
///
/// Вход клиента
///
///
[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.Client = APIClient.GetRequest($"api/client/login?login={login}&password={password}");
if (APIClient.Client == null)
{
throw new Exception("Неверный логин/пароль");
}
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))
{
throw new Exception("Введите логин, пароль и ФИО");
}
APIClient.PostRequest("api/client/register", new ClientBindingModel
{
ClientFIO = fio,
Email = login,
Password = password
});
Response.Redirect("Enter");
return;
}
///
/// Создание заказа
///
///
[HttpGet]
public IActionResult Create()
{
ViewBag.Planes = APIClient.GetRequest>("api/main/getplanelist");
return View();
}
///
/// Создание заказа
///
///
///
///
[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");
}
///
/// Подсчет суммы заказа
///
///
///
///
[HttpPost]
public double Calc(int count, int plane)
{
var prod = APIClient.GetRequest($"api/main/getplane?planeId={plane}");
return count * (prod?.Price ?? 1);
}
///
/// Получение списка писем клиента
///
///
[HttpGet]
public IActionResult Mails()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}"));
}
}
}