using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using VeterinaryClinicContracts.BindingModels; using VeterinaryClinicContracts.BusinessLogicsContracts; using VeterinaryClinicContracts.SearchModels; using VeterinaryClinicDataModels.Enums; using VeterinaryClinicWebApp.Models; namespace VeterinaryClinicWebApp.Controllers; public class HomeController : Controller { private readonly ILogger _logger; private readonly IUserLogic _userLogic; public HomeController(ILogger logger, IUserLogic userLogic) { _logger = logger; _userLogic = userLogic; } /// /// Домашняя страница /// [HttpGet] public IActionResult Index() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(APIClient.User); } /// /// Личные данные пользователя /// [HttpGet] public IActionResult Privacy() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(APIClient.User); } [HttpPost] public void Privacy(string fullname, UserRole role, string phone, string email, string password) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(fullname) || string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception("Введены не все данные!"); } _userLogic.Update(new UserBindingModel { Id = APIClient.User.Id, FullName = fullname, Role = role, Phone = phone, Email = email, Password = password }); APIClient.User.FullName = fullname; APIClient.User.Role = role; APIClient.User.Phone = phone; APIClient.User.Email = email; APIClient.User.Password = password; Response.Redirect("Privacy"); } /// /// Аутентификация /// [HttpGet] public IActionResult Enter() { if (APIClient.User != null) { throw new Exception("Вы уже авторизовались!"); } return View(); } [HttpPost] public void Enter(string email, string password) { if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception("Введены не все данные!"); } APIClient.User = _userLogic.ReadElement(new UserSearchModel { Email = email, Password = password }); if (APIClient.User == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Index"); } /// /// Регистрация /// [HttpGet] public IActionResult Register() { if (APIClient.User != null) { throw new Exception("Вы уже зарегистрировались!"); } return View(); } [HttpPost] public void Register(string fullname, UserRole role, string phone, string email, string password) { if (string.IsNullOrEmpty(fullname) || string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception("Введены не все данные!"); } _userLogic.Create(new UserBindingModel { FullName = fullname, Role = role, Phone = phone, Email = email, Password = password }); Response.Redirect("Enter"); } /// /// Выйти из аккаунта /// /// /// Получить отчет /// /// /// Создать отчёт в формате Word /// /// /// Создать отчёт в формате Excel /// /// /// Создать отчёт в формате Pdf /// /// /// Отправить по почте отчёт /// /// /// Ошибка /// [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } }