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

168 lines
5.3 KiB
C#
Raw Normal View History

2024-05-22 00:58:17 +04:00
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace HospitalWebApp.Controllers
{
/// <summary>
/// Контроллер для сущности "Процедура"
/// </summary>
public class ProcedureController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger<ProcedureController> _logger;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
public ProcedureController(ILogger<ProcedureController> logger)
{
_logger = logger;
}
/// <summary>
/// Вывести список процедур
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Procedures()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ProcedureViewModel>>($"api/procedure/getprocedures"));
}
/// <summary>
/// Создать процедуру
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateProcedure()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = APIClient.GetRequest<List<MedicineViewModel>>("api/medicine/getmedicines");
return View();
}
/// <summary>
/// Создать процедуру
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
/// <param name="medicines"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void CreateProcedure(string name, string? description, List<int> medicines)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || medicines == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, IMedicineModel> procedureMedicines = new Dictionary<int, IMedicineModel>();
foreach (var medicineId in medicines)
{
procedureMedicines.Add(medicineId, APIClient.GetRequest<MedicineViewModel>($"api/medicine/getmedicine?id={medicineId}"));
}
APIClient.PostRequest("api/procedure/createprocedure", new ProcedureBindingModel
{
Name = name,
Description = description,
ProcedureMedicines = procedureMedicines
});
Response.Redirect("Procedures");
}
/// <summary>
/// Редактировать процедуру
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult UpdateProcedure(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = APIClient.GetRequest<List<MedicineViewModel>>("api/medicine/getmedicines");
return View(APIClient.GetRequest<ProcedureViewModel>($"api/procedure/getprocedure?id={id}"));
}
/// <summary>
/// Редактировать процедуру
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
/// <param name="medicines"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void UpdateProcedure(int id, string name, string? description, List<int> medicines)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || medicines == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, IMedicineModel> procedureMedicines = new Dictionary<int, IMedicineModel>();
foreach (var medicineId in medicines)
{
procedureMedicines.Add(medicineId, APIClient.GetRequest<MedicineViewModel>($"api/medicine/getmedicine?id={medicineId}"));
}
APIClient.PostRequest("api/procedure/updateprocedure", new ProcedureBindingModel
{
Id = id,
Name = name,
Description = description,
ProcedureMedicines = procedureMedicines
});
Response.Redirect("Procedures");
}
/// <summary>
/// Удалить процедуру
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteProcedure(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
APIClient.PostRequest($"api/procedure/deleteprocedure", new ProcedureBindingModel
{
Id = id
});
Response.Redirect("Prodecures");
}
}
}