using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using VeterinaryClinicContracts.BindingModels; using VeterinaryClinicContracts.BusinessLogicsContracts; using VeterinaryClinicContracts.SearchModels; using VeterinaryClinicWebApp.Models; namespace VeterinaryClinicWebApp.Controllers { public class MedicationController : Controller { private readonly ILogger _logger; private readonly IMedicationLogic _medicationLogic; public MedicationController(ILogger logger, IMedicationLogic medicationLogic) { _logger = logger; _medicationLogic = medicationLogic; } /// /// Вывести список лекарств /// public IActionResult Medications() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(_medicationLogic.ReadList(null)); } /// /// Создать лекарство /// /// [HttpGet] public IActionResult CreateMedication() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void CreateMedication(string name, string description) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(description)) { throw new Exception("Введены не все данные!"); } _medicationLogic.Create(new MedicationBindingModel { Name = name, Description = description }); Response.Redirect("/Medication/Medications"); } /// /// Редактировать лекарство /// [HttpGet] public IActionResult UpdateMedication(int id) { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(_medicationLogic.ReadElement(new MedicationSearchModel { Id = id })); } [HttpPost] public void UpdateMedication(int id, string name, string description) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(description)) { throw new Exception("Введены не все данные!"); } _medicationLogic.Update(new MedicationBindingModel { Id = id, Name = name, Description = description }); Response.Redirect("/Medication/Medications"); } /// /// Удалить лекарство /// [HttpPost] public void DeleteMedication(int id) { if (APIClient.User == null) { throw new Exception("Необходимо авторизоваться!"); } _medicationLogic.Delete(new MedicationBindingModel { Id = id }); Response.Redirect("/Medication/Medications"); } } }