using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.SearchModels; using HospitalContracts.ViewModels; using Microsoft.AspNetCore.Mvc; namespace HospitalWebApp.Controllers { /// /// Контроллер для сущности "Лекарство" /// public class MedicineController : Controller { /// /// Логгер /// private readonly ILogger _logger; /// /// Бизнес-логика для сущности "Лекарство" /// private readonly IMedicineLogic _medicineLogic; /// /// Конструктор /// /// /// public MedicineController(ILogger logger, IMedicineLogic medicineLogic) { _logger = logger; _medicineLogic = medicineLogic; } /// /// Вывести список лекарств /// /// [HttpGet] public IActionResult Medicines() { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } return View(_medicineLogic.ReadList(null)); } /// /// Создать лекарство /// /// [HttpGet] public IActionResult CreateMedicine() { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } return View(); } /// /// Создать лекарство /// /// /// /// [HttpPost] public void CreateMedicine(string name, string? description) { if (APIClient.Doctor == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name)) { throw new Exception("Введены не все данные!"); } _medicineLogic.Create(new MedicineBindingModel { Name = name, Description = description }); Response.Redirect("/Medicine/Medicines"); } /// /// Редактировать лекарство /// /// [HttpGet] public IActionResult UpdateMedicine(int id) { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } return View(_medicineLogic.ReadElement(new MedicineSearchModel { Id = id })); } /// /// Редактировать лекарство /// /// /// /// [HttpPost] public void UpdateMedicine(int id, string name, string? description) { if (APIClient.Doctor == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name)) { throw new Exception("Введены не все данные!"); } _medicineLogic.Update(new MedicineBindingModel { Id = id, Name = name, Description = description }); Response.Redirect("/Medicine/Medicines"); } /// /// Удалить лекарство /// /// [HttpPost] public void DeleteMedicine(int id) { if (APIClient.Doctor == null) { throw new Exception("Необходимо авторизоваться!"); } _medicineLogic.Delete(new MedicineBindingModel { Id = id }); Response.Redirect("/Medicine/Medicines"); } } }