PIbd-21_MasenkinMS_Coursewo.../Hospital/HospitalWebApp/Controllers/MedicineController.cs

161 lines
4.3 KiB
C#

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