using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
using System.Numerics;
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,
DoctorId = APIClient.Doctor.Id,
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,
DoctorId = APIClient.Doctor.Id,
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");
}
///
/// Выписать рецепт пациенту
///
///
///
[HttpGet]
public IActionResult CreatePatientRecipe()
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
ViewBag.Patients = APIClient.GetRequest>($"api/patient/getpatients?doctorId={APIClient.Doctor.Id}");
ViewBag.Recipes = APIClient.GetRequest>($"api/recipe/getrecipes?doctorId={APIClient.Doctor.Id}");
return View();
}
///
/// Выписать рецепт пациенту
///
///
///
///
[HttpPost]
public void CreatePatientRecipe(int patientId, List recipes)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (patientId <= 0 || recipes == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary patientRecipes = new Dictionary();
foreach (var recipeId in recipes)
{
patientRecipes.Add(recipeId, APIClient.GetRequest($"api/recipe/getrecipe?id={recipeId}"));
}
var patient = APIClient.GetRequest($"api/patient/getpatient?id={patientId}");
APIClient.PostRequest("api/patient/updatepatient", new PatientBindingModel
{
Id = patient!.Id,
FullName = patient!.FullName,
BirthDate = patient!.BirthDate,
Phone = patient!.Phone,
DoctorId = APIClient.Doctor.Id,
PatientProcedures = patient!.PatientProcedures,
PatientRecipes = patientRecipes
});
Response.Redirect("Patients");
}
}
}