using HospitalContracts.BindingModels; using HospitalContracts.ViewModels; using HospitalDatabaseImplement; using HospitalWeb.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; namespace HospitalWeb.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { return View(); } [HttpGet] public IActionResult Privacy() { if (APIClient.Apothecary == null) { return Redirect("~/Home/Enter"); } return View(APIClient.Apothecary); } [HttpPost] public void Privacy(string login, string password) { if (APIClient.Apothecary == null) { throw new Exception("Доступно только авторизованным пользователям"); } if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) { throw new Exception("Введите логин и пароль"); } APIClient.PostRequest("api/apothecary/updatedata", new ApothecaryBindingModel { Id = APIClient.Apothecary.Id, Login = login, Password = password }); APIClient.Apothecary.Login = login; APIClient.Apothecary.Password = password; Response.Redirect("Index"); } [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.Apothecary = APIClient.GetRequest($"api/apothecary/login?login={login}&password={password}"); if (APIClient.Apothecary == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Register() { return View(); } [HttpPost] public void Register(string login, string password) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) { throw new Exception("Введите логин и пароль"); } APIClient.PostRequest("api/apothecary/register", new ApothecaryBindingModel { Login = login, Password = password }); Response.Redirect("Enter"); return; } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } }