PIbd-21_MasenkinMS_Coursewo.../Hospital/HospitalWebApp/Controllers/PatientController.cs
2024-05-27 22:25:54 +04:00

305 lines
8.2 KiB
C#

using DocumentFormat.OpenXml.Office2010.Excel;
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
using System.Numerics;
namespace HospitalWebApp.Controllers
{
/// <summary>
/// Контроллер для сущности "Пациент"
/// </summary>
public class PatientController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger<PatientController> _logger;
/// <summary>
/// Бизнес-логика для сущности "Пациент"
/// </summary>
private readonly IPatientLogic _patientLogic;
/// <summary>
/// Бизнес-логика для сущности "Рецепт"
/// </summary>
private readonly IRecipeLogic _recipeLogic;
/// <summary>
/// Бизнес-логика для сущности "Процедура"
/// </summary>
private readonly IProcedureLogic _procedureLogic;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="patientLogic"></param>
/// <param name="recipeLogic"></param>
/// <param name="procedureLogic"></param>
public PatientController(ILogger<PatientController> logger, IPatientLogic patientLogic, IRecipeLogic recipeLogic, IProcedureLogic procedureLogic)
{
_logger = logger;
_patientLogic = patientLogic;
_recipeLogic = recipeLogic;
_procedureLogic = procedureLogic;
}
/// <summary>
/// Вывести список пациентов
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Patients()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(_patientLogic.ReadList(new PatientSearchModel
{
DoctorId = APIClient.Doctor.Id,
}));
}
/// <summary>
/// Создать пациента
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreatePatient()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedures = _procedureLogic.ReadList(null);
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, _procedureLogic.ReadElement(new ProcedureSearchModel { Id = procedureId })!);
}
_patientLogic.Create(new PatientBindingModel
{
FullName = fullname,
BirthDate = birthdate,
Phone = phone,
DoctorId = APIClient.Doctor.Id,
PatientProcedures = patientProcedures
});
Response.Redirect("/Patient/Patients");
}
/// <summary>
/// Редактировать пациента
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult UpdatePatient(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedures = _procedureLogic.ReadList(null);
return View(_patientLogic.ReadElement(new PatientSearchModel
{
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, _procedureLogic.ReadElement(new ProcedureSearchModel { Id = procedureId })!);
}
var patient = _patientLogic.ReadElement(new PatientSearchModel { Id = id });
_patientLogic.Update(new PatientBindingModel
{
Id = id,
FullName = fullname,
BirthDate = birthdate,
Phone = phone,
DoctorId = APIClient.Doctor.Id,
PatientRecipes = patient!.PatientRecipes,
PatientProcedures = patientProcedures
});
Response.Redirect("/Patient/Patients");
}
/// <summary>
/// Удалить пациента
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeletePatient(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_patientLogic.Delete(new PatientBindingModel
{
Id = id
});
Response.Redirect("/Patient/Patients");
}
/// <summary>
/// Выписать рецепт пациенту
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[HttpGet]
public IActionResult CreatePatientRecipe()
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
ViewBag.Patients = _patientLogic.ReadList(new PatientSearchModel
{
DoctorId = APIClient.Doctor.Id
});
ViewBag.Recipes = _recipeLogic.ReadList(new RecipeSearchModel
{
DoctorId = APIClient.Doctor.Id
});
return View();
}
/// <summary>
/// Выписать рецепт пациенту
/// </summary>
/// <param name="patientId"></param>
/// <param name="recipes"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void CreatePatientRecipe(int patientId, List<int> recipes)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (patientId <= 0 || recipes == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, IRecipeModel> patientRecipes = new Dictionary<int, IRecipeModel>();
foreach (var recipeId in recipes)
{
patientRecipes.Add(recipeId, _recipeLogic.ReadElement(new RecipeSearchModel { Id = recipeId })!);
}
var patient = _patientLogic.ReadElement(new PatientSearchModel { Id = patientId });
_patientLogic.Update(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("/Patient/Patients");
}
/// <summary>
/// Получить список рецептов пациента
/// </summary>
/// <param name="patientId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[HttpGet]
public JsonResult GetPatientRecipes(int patientId)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
var allRecipes = _recipeLogic.ReadList(new RecipeSearchModel
{
DoctorId = APIClient.Doctor.Id
});
var patient = _patientLogic.ReadElement(new PatientSearchModel { Id = patientId });
var patientRecipeIds = patient?.PatientRecipes?.Select(x => x.Key).ToList() ?? new List<int>();
var result = new
{
allRecipes = allRecipes.Select(r => new { id = r.Id, issueDate = r.IssueDate.ToShortDateString() }),
patientRecipeIds = patientRecipeIds
};
return Json(result);
}
}
}