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

305 lines
8.2 KiB
C#
Raw Permalink Normal View History

using DocumentFormat.OpenXml.Office2010.Excel;
using HospitalContracts.BindingModels;
2024-05-22 00:58:17 +04:00
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
2024-05-22 00:58:17 +04:00
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
2024-05-23 01:38:16 +04:00
using System.Numerics;
2024-05-22 00:58:17 +04:00
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;
2024-05-22 00:58:17 +04:00
/// <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)
2024-05-22 00:58:17 +04:00
{
_logger = logger;
_patientLogic = patientLogic;
_recipeLogic = recipeLogic;
_procedureLogic = procedureLogic;
2024-05-22 00:58:17 +04:00
}
/// <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,
}));
2024-05-22 00:58:17 +04:00
}
/// <summary>
/// Создать пациента
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreatePatient()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedures = _procedureLogic.ReadList(null);
2024-05-22 00:58:17 +04:00
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 })!);
2024-05-22 00:58:17 +04:00
}
_patientLogic.Create(new PatientBindingModel
2024-05-22 00:58:17 +04:00
{
FullName = fullname,
BirthDate = birthdate,
Phone = phone,
2024-05-23 01:38:16 +04:00
DoctorId = APIClient.Doctor.Id,
2024-05-22 00:58:17 +04:00
PatientProcedures = patientProcedures
});
Response.Redirect("/Patient/Patients");
2024-05-22 00:58:17 +04:00
}
/// <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
}));
2024-05-22 00:58:17 +04:00
}
/// <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 })!);
2024-05-22 00:58:17 +04:00
}
var patient = _patientLogic.ReadElement(new PatientSearchModel { Id = id });
_patientLogic.Update(new PatientBindingModel
2024-05-22 00:58:17 +04:00
{
Id = id,
FullName = fullname,
BirthDate = birthdate,
Phone = phone,
2024-05-23 01:38:16 +04:00
DoctorId = APIClient.Doctor.Id,
PatientRecipes = patient!.PatientRecipes,
2024-05-22 00:58:17 +04:00
PatientProcedures = patientProcedures
});
Response.Redirect("/Patient/Patients");
2024-05-22 00:58:17 +04:00
}
/// <summary>
/// Удалить пациента
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeletePatient(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_patientLogic.Delete(new PatientBindingModel
2024-05-22 00:58:17 +04:00
{
Id = id
});
Response.Redirect("/Patient/Patients");
2024-05-22 00:58:17 +04:00
}
2024-05-23 01:38:16 +04:00
/// <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
});
2024-05-23 01:38:16 +04:00
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 })!);
2024-05-23 01:38:16 +04:00
}
var patient = _patientLogic.ReadElement(new PatientSearchModel { Id = patientId });
_patientLogic.Update(new PatientBindingModel
2024-05-23 01:38:16 +04:00
{
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
{
2024-05-27 22:25:54 +04:00
allRecipes = allRecipes.Select(r => new { id = r.Id, issueDate = r.IssueDate.ToShortDateString() }),
patientRecipeIds = patientRecipeIds
};
return Json(result);
2024-05-23 01:38:16 +04:00
}
2024-05-22 00:58:17 +04:00
}
}