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

173 lines
4.6 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace HospitalWebApp.Controllers
{
/// <summary>
/// Контроллер для сущности "Пациент"
/// </summary>
public class PatientController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger<PatientController> _logger;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
public PatientController(ILogger<PatientController> logger)
{
_logger = logger;
}
/// <summary>
/// Вывести список пациентов
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Patients()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<PatientViewModel>>($"api/patient/getpatients?doctorId={APIClient.Doctor.Id}"));
}
/// <summary>
/// Создать пациента
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreatePatient()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedures = APIClient.GetRequest<List<ProcedureViewModel>>("api/procedure/getprocedures");
return View();
}
/// <summary>
/// Создать пациента
/// </summary>
/// <param name="fullname"></param>
/// <param name="birthdate"></param>
/// <param name="phone"></param>
/// <param name="procedures"></param>
/// <returns></returns>
[HttpPost]
public void CreatePatient(string fullname, DateTime birthdate, string phone, List<int> procedures)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(fullname) || birthdate == DateTime.MinValue || string.IsNullOrEmpty(phone) || procedures == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, IProcedureModel> patientProcedures = new Dictionary<int, IProcedureModel>();
foreach (var procedureId in procedures)
{
patientProcedures.Add(procedureId, APIClient.GetRequest<ProcedureViewModel>($"api/procedure/getprocedure?id={procedureId}"));
}
APIClient.PostRequest("api/patient/createpatient", new PatientBindingModel
{
FullName = fullname,
BirthDate = birthdate,
Phone = phone,
PatientProcedures = patientProcedures
});
Response.Redirect("Patients");
}
/// <summary>
/// Редактировать пациента
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult UpdatePatient(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedures = APIClient.GetRequest<List<ProcedureViewModel>>("api/procedure/getprocedures");
return View(APIClient.GetRequest<PatientViewModel>($"api/patient/getpatient?id={id}"));
}
/// <summary>
/// Редактировать пациента
/// </summary>
/// <param name="fullname"></param>
/// <param name="birthdate"></param>
/// <param name="phone"></param>
/// <param name="procedures"></param>
/// <returns></returns>
[HttpPost]
public void UpdatePatient(int id, string fullname, DateTime birthdate, string phone, List<int> procedures)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(fullname) || birthdate == DateTime.MinValue || string.IsNullOrEmpty(phone) || procedures == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, IProcedureModel> patientProcedures = new Dictionary<int, IProcedureModel>();
foreach (var procedureId in procedures)
{
patientProcedures.Add(procedureId, APIClient.GetRequest<ProcedureViewModel>($"api/procedure/getprocedure?id={procedureId}"));
}
APIClient.PostRequest("api/patient/updatepatient", new PatientBindingModel
{
Id = id,
FullName = fullname,
BirthDate = birthdate,
Phone = phone,
PatientProcedures = patientProcedures
});
Response.Redirect("Patients");
}
/// <summary>
/// Удалить пациента
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeletePatient(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
APIClient.PostRequest($"api/patient/deletepatient", new PatientBindingModel
{
Id = id
});
Response.Redirect("Patients");
}
}
}