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