using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.SearchModels; using HospitalContracts.ViewModels; using HospitalDataModels.Models; using Microsoft.AspNetCore.Mvc; namespace HospitalWebApp.Controllers { /// /// Контроллер для сущности "Процедура" /// public class ProcedureController : Controller { /// /// Логгер /// private readonly ILogger _logger; /// /// Бизнес-логика для сущности "Процедура" /// private readonly IProcedureLogic _procedureLogic; /// /// Бизнес-логика для сущности "Лекарство" /// private readonly IMedicineLogic _medicineLogic; /// /// Конструктор /// /// /// /// public ProcedureController(ILogger logger, IProcedureLogic procedureLogic, IMedicineLogic medicineLogic) { _logger = logger; _procedureLogic = procedureLogic; _medicineLogic = medicineLogic; } /// /// Вывести список процедур /// /// [HttpGet] public IActionResult Procedures() { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } return View(_procedureLogic.ReadList(null)); } /// /// Создать процедуру /// /// [HttpGet] public IActionResult CreateProcedure() { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } ViewBag.Medicines = _medicineLogic.ReadList(null); 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, _medicineLogic.ReadElement(new MedicineSearchModel { Id = medicineId })!); } _procedureLogic.Create(new ProcedureBindingModel { Name = name, Description = description, ProcedureMedicines = procedureMedicines }); Response.Redirect("/Procedure/Procedures"); } /// /// Редактировать процедуру /// /// [HttpGet] public IActionResult UpdateProcedure(int id) { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } ViewBag.Medicines = _medicineLogic.ReadList(null); return View(_procedureLogic.ReadElement(new ProcedureSearchModel { 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, _medicineLogic.ReadElement(new MedicineSearchModel { Id = medicineId })!); } _procedureLogic.Update(new ProcedureBindingModel { Id = id, Name = name, Description = description, ProcedureMedicines = procedureMedicines }); Response.Redirect("/Procedure/Procedures"); } /// /// Удалить процедуру /// /// [HttpPost] public void DeleteProcedure(int id) { if (APIClient.Doctor == null) { throw new Exception("Необходимо авторизоваться!"); } _procedureLogic.Delete(new ProcedureBindingModel { Id = id }); Response.Redirect("/Procedure/Procedures"); } } }