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

161 lines
4.3 KiB
C#
Raw Normal View History

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