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

150 lines
4.1 KiB
C#

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