using HospitalContracts.BindingModels; using HospitalContracts.ViewModels; using HospitalDataModels.Models; using Microsoft.AspNetCore.Mvc; namespace HospitalWebApp.Controllers { /// /// Контроллер для сущности "Процедура" /// public class ProcedureController : Controller { /// /// Логгер /// private readonly ILogger _logger; /// /// Конструктор /// /// public ProcedureController(ILogger logger) { _logger = logger; } /// /// Вывести список процедур /// /// [HttpGet] public IActionResult Procedures() { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/procedure/getprocedures")); } /// /// Создать процедуру /// /// [HttpGet] public IActionResult CreateProcedure() { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } ViewBag.Medicines = APIClient.GetRequest>("api/medicine/getmedicines"); return View(); } /// /// Создать процедуру /// /// /// /// /// [HttpPost] public void CreateProcedure(string name, string? description, List medicines) { if (APIClient.Doctor == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name) || medicines == null) { throw new Exception("Введены не все данные!"); } Dictionary procedureMedicines = new Dictionary(); foreach (var medicineId in medicines) { procedureMedicines.Add(medicineId, APIClient.GetRequest($"api/medicine/getmedicine?id={medicineId}")); } APIClient.PostRequest("api/procedure/createprocedure", new ProcedureBindingModel { Name = name, Description = description, ProcedureMedicines = procedureMedicines }); Response.Redirect("Procedures"); } /// /// Редактировать процедуру /// /// [HttpGet] public IActionResult UpdateProcedure(int id) { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } ViewBag.Medicines = APIClient.GetRequest>("api/medicine/getmedicines"); return View(APIClient.GetRequest($"api/procedure/getprocedure?id={id}")); } /// /// Редактировать процедуру /// /// /// /// /// [HttpPost] public void UpdateProcedure(int id, string name, string? description, List medicines) { if (APIClient.Doctor == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name) || medicines == null) { throw new Exception("Введены не все данные!"); } Dictionary procedureMedicines = new Dictionary(); foreach (var medicineId in medicines) { procedureMedicines.Add(medicineId, APIClient.GetRequest($"api/medicine/getmedicine?id={medicineId}")); } APIClient.PostRequest("api/procedure/updateprocedure", new ProcedureBindingModel { Id = id, Name = name, Description = description, ProcedureMedicines = procedureMedicines }); Response.Redirect("Procedures"); } /// /// Удалить процедуру /// /// [HttpPost] public void DeleteProcedure(int id) { if (APIClient.Doctor == null) { throw new Exception("Необходимо авторизоваться!"); } APIClient.PostRequest($"api/procedure/deleteprocedure", new ProcedureBindingModel { Id = id }); Response.Redirect("Prodecures"); } } }