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

188 lines
5.6 KiB
C#
Raw Permalink 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 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)
2024-05-22 00:58:17 +04:00
{
_logger = logger;
_procedureLogic = procedureLogic;
_medicineLogic = medicineLogic;
2024-05-22 00:58:17 +04:00
}
/// <summary>
/// Вывести список процедур
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Procedures()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(_procedureLogic.ReadList(null));
2024-05-22 00:58:17 +04:00
}
/// <summary>
/// Создать процедуру
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateProcedure()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = _medicineLogic.ReadList(null);
2024-05-22 00:58:17 +04:00
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 })!);
2024-05-22 00:58:17 +04:00
}
_procedureLogic.Create(new ProcedureBindingModel
2024-05-22 00:58:17 +04:00
{
Name = name,
Description = description,
ProcedureMedicines = procedureMedicines
});
Response.Redirect("/Procedure/Procedures");
2024-05-22 00:58:17 +04:00
}
/// <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
}));
2024-05-22 00:58:17 +04:00
}
/// <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 })!);
}
2024-05-22 00:58:17 +04:00
_procedureLogic.Update(new ProcedureBindingModel
{
2024-05-22 00:58:17 +04:00
Id = id,
Name = name,
Description = description,
ProcedureMedicines = procedureMedicines
});
2024-05-22 00:58:17 +04:00
Response.Redirect("/Procedure/Procedures");
}
2024-05-22 00:58:17 +04:00
/// <summary>
/// Удалить процедуру
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteProcedure(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_procedureLogic.Delete(new ProcedureBindingModel
2024-05-22 00:58:17 +04:00
{
Id = id
});
Response.Redirect("/Procedure/Procedures");
}
2024-05-22 00:58:17 +04:00
}
}