using HospitalContracts.BindingModels; using HospitalContracts.ViewModels; using HospitalDataModels.Models; using Microsoft.AspNetCore.Mvc; namespace HospitalWebApp.Controllers { /// /// Контроллер для сущности "Лекарство" /// public class MedicineController : Controller { /// /// Логгер /// private readonly ILogger _logger; /// /// Конструктор /// /// public MedicineController(ILogger logger) { _logger = logger; } /// /// Вывести список лекарств /// /// [HttpGet] public IActionResult Medicines() { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/medicine/getmedicines")); } /// /// Создать лекарство /// /// [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("Введены не все данные!"); } APIClient.PostRequest("api/medicine/createmedicine", new MedicineBindingModel { Name = name, Description = description }); Response.Redirect("Medicines"); } /// /// Редактировать лекарство /// /// [HttpGet] public IActionResult UpdateMedicine(int id) { if (APIClient.Doctor == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest($"api/medicine/getmedicine?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("Введены не все данные!"); } APIClient.PostRequest("api/medicine/updatemedicine", new MedicineBindingModel { Id = id, Name = name, Description = description }); Response.Redirect("Medicines"); } /// /// Удалить лекарство /// /// [HttpPost] public void DeleteMedicine(int id) { if (APIClient.Doctor == null) { throw new Exception("Необходимо авторизоваться!"); } APIClient.PostRequest($"api/medicine/deletemedicine", new MedicineBindingModel { Id = id }); Response.Redirect("Medicines"); } } }