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

234 lines
6.6 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
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>
/// <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,
DoctorId = APIClient.Doctor.Id,
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,
DoctorId = APIClient.Doctor.Id,
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");
}
/// <summary>
/// Выписать рецепт пациенту
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[HttpGet]
public IActionResult CreatePatientRecipe()
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
ViewBag.Patients = APIClient.GetRequest<List<PatientViewModel>>($"api/patient/getpatients?doctorId={APIClient.Doctor.Id}");
ViewBag.Recipes = APIClient.GetRequest<List<RecipeViewModel>>($"api/recipe/getrecipes?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, APIClient.GetRequest<RecipeViewModel>($"api/recipe/getrecipe?id={recipeId}"));
}
var patient = APIClient.GetRequest<PatientViewModel>($"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");
}
}
}