using AutomobilePlantClientApp.Models; using AutomobilePlantContracts.BindingModels; using AutomobilePlantContracts.ViewModels; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; namespace AutomobilePlantClientApp.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("Login only for authorized persons"); } if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) { throw new Exception("Enter your login, password and full name"); } 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("Enter login and password"); } APIClient.Client = APIClient.GetRequest($"api/client/login?login={login}&password={password}"); if (APIClient.Client == null) { throw new Exception("Invalid login/password"); } 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("Enter your login, password and full name"); } APIClient.PostRequest("api/client/register", new ClientBindingModel { ClientFIO = fio, Email = login, Password = password }); Response.Redirect("Enter"); return; } [HttpGet] public IActionResult Create() { ViewBag.Cars = APIClient.GetRequest>("api/main/getcarlist"); return View(); } [HttpPost] public void Create(int car, int count) { if (APIClient.Client == null) { throw new Exception("Login only for authorized persons"); } if (count <= 0) { throw new Exception("Quantity and amount must be greater than 0"); } APIClient.PostRequest("api/main/createorder", new OrderBindingModel { ClientId = APIClient.Client.Id, CarId = car, Count = count, Sum = Calc(count, car) }); Response.Redirect("Index"); } [HttpPost] public double Calc(int count, int car) { var c = APIClient.GetRequest($"api/main/getcar?carId={car}"); return count * (c?.Price ?? 1); } } }