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

188 lines
5.6 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
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>
private readonly IProcedureLogic _procedureLogic;
/// <summary>
/// Бизнес-логика для сущности "Лекарство"
/// </summary>
private readonly IMedicineLogic _medicineLogic;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="procedureLogic"></param>
/// <param name="medicineLogic"></param>
public ProcedureController(ILogger<ProcedureController> logger, IProcedureLogic procedureLogic, IMedicineLogic medicineLogic)
{
_logger = logger;
_procedureLogic = procedureLogic;
_medicineLogic = medicineLogic;
}
/// <summary>
/// Вывести список процедур
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Procedures()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(_procedureLogic.ReadList(null));
}
/// <summary>
/// Создать процедуру
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateProcedure()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = _medicineLogic.ReadList(null);
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, _medicineLogic.ReadElement(new MedicineSearchModel { Id = medicineId })!);
}
_procedureLogic.Create(new ProcedureBindingModel
{
Name = name,
Description = description,
ProcedureMedicines = procedureMedicines
});
Response.Redirect("/Procedure/Procedures");
}
/// <summary>
/// Редактировать процедуру
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult UpdateProcedure(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = _medicineLogic.ReadList(null);
return View(_procedureLogic.ReadElement(new ProcedureSearchModel
{
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, _medicineLogic.ReadElement(new MedicineSearchModel { Id = medicineId })!);
}
_procedureLogic.Update(new ProcedureBindingModel
{
Id = id,
Name = name,
Description = description,
ProcedureMedicines = procedureMedicines
});
Response.Redirect("/Procedure/Procedures");
}
/// <summary>
/// Удалить процедуру
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteProcedure(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_procedureLogic.Delete(new ProcedureBindingModel
{
Id = id
});
Response.Redirect("/Procedure/Procedures");
}
}
}