PIbd-21_MasenkinMS_Coursewo.../Hospital/HospitalWebApp/Controllers/HomeController.cs

217 lines
5.1 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Enums;
using HospitalWebApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace HospitalWebApp.Controllers
{
/// <summary>
/// Главный контроллер
/// </summary>
public class HomeController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger<HomeController> _logger;
/// <summary>
/// Бизнес-логика для сущности "Доктор"
/// </summary>
private readonly IDoctorLogic _doctorLogic;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="doctorLogic"></param>
public HomeController(ILogger<HomeController> logger, IDoctorLogic doctorLogic)
{
_logger = logger;
_doctorLogic = doctorLogic;
}
/// <summary>
/// Домашняя страница
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Index()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Doctor);
}
/// <summary>
/// Личные данные доктора
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Doctor);
}
/// <summary>
/// Личные данные доктора
/// </summary>
/// <param name="fullname"></param>
/// <param name="post"></param>
/// <param name="email"></param>
/// <param name="password"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void Privacy(string fullname, DoctorPost post, string email, string password)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(fullname) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{
throw new Exception("Введены не все данные!");
}
_doctorLogic.Update(new DoctorBindingModel
{
Id = APIClient.Doctor.Id,
FullName = fullname,
Post = post,
Email = email,
Password = password
});
APIClient.Doctor.FullName = fullname;
APIClient.Doctor.Post = post;
APIClient.Doctor.Email = email;
APIClient.Doctor.Password = password;
Response.Redirect("Privacy");
}
/// <summary>
/// Аутентификация
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Enter()
{
if (APIClient.Doctor != null)
{
throw new Exception("Вы уже авторизовались!");
}
return View();
}
/// <summary>
/// Аутентификация
/// </summary>
/// <param name="email"></param>
/// <param name="password"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void Enter(string email, string password)
{
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{
throw new Exception("Введены не все данные!");
}
APIClient.Doctor = _doctorLogic.ReadElement(new DoctorSearchModel
{
Email = email,
Password = password
});
if (APIClient.Doctor == null)
{
throw new Exception("Неверный логин/пароль");
}
Response.Redirect("Index");
}
/// <summary>
/// Регистрация
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Register()
{
if (APIClient.Doctor != null)
{
throw new Exception("Вы уже зарегистрировались!");
}
return View();
}
/// <summary>
/// Регистрация
/// </summary>
/// <param name="fullname"></param>
/// <param name="post"></param>
/// <param name="email"></param>
/// <param name="password"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void Register(string fullname, DoctorPost post, string email, string password)
{
if (string.IsNullOrEmpty(fullname) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{
throw new Exception("Введены не все данные!");
}
_doctorLogic.Create(new DoctorBindingModel
{
FullName = fullname,
Post = post,
Email = email,
Password = password
});
Response.Redirect("Enter");
}
/// <summary>
/// Выйти из аккаунта
/// </summary>
/// <exception cref="Exception"></exception>
[HttpGet]
public void Logout()
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
APIClient.Doctor = null;
Response.Redirect("Enter");
}
/// <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 });
}
}
}