using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
using System.Numerics;
namespace HospitalWebApp.Controllers
{
///
/// Контроллер для сущности "Рецепт"
///
public class RecipeController : Controller
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Бизнес-логика для сущности "Рецепт"
///
private readonly IRecipeLogic _recipeLogic;
///
/// Бизнес-логика для сущности "Лекарство"
///
private readonly IMedicineLogic _medicineLogic;
///
/// Конструктор
///
///
///
///
public RecipeController(ILogger logger, IRecipeLogic recipeLogic, IMedicineLogic medicineLogic)
{
_logger = logger;
_recipeLogic = recipeLogic;
_medicineLogic = medicineLogic;
}
///
/// Вывести список рецептов
///
///
[HttpGet]
public IActionResult Recipes()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(_recipeLogic.ReadList(new RecipeSearchModel
{
DoctorId = APIClient.Doctor.Id,
}));
}
///
/// Создать рецепт
///
///
[HttpGet]
public IActionResult CreateRecipe()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = _medicineLogic.ReadList(null);
return View();
}
///
/// Создать рецепт
///
///
///
///
[HttpPost]
public void CreateRecipe(DateTime issuedate, List medicines)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (issuedate == DateTime.MinValue || medicines == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary recipeMedicines = new Dictionary();
foreach (var medicineId in medicines)
{
recipeMedicines.Add(medicineId, _medicineLogic.ReadElement(new MedicineSearchModel { Id = medicineId })!);
}
_recipeLogic.Create(new RecipeBindingModel
{
IssueDate = issuedate,
DoctorId = APIClient.Doctor.Id,
RecipeMedicines = recipeMedicines
});
Response.Redirect("/Recipe/Recipes");
}
///
/// Редактировать рецепт
///
///
[HttpGet]
public IActionResult UpdateRecipe(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = _medicineLogic.ReadList(null);
return View(_recipeLogic.ReadElement(new RecipeSearchModel
{
Id = id
}));
}
///
/// Редактировать рецепт
///
///
///
///
///
[HttpPost]
public void UpdateRecipe(int id, DateTime issuedate, List medicines)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (issuedate == DateTime.MinValue || medicines == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary recipeMedicines = new Dictionary();
foreach (var medicineId in medicines)
{
recipeMedicines.Add(medicineId, _medicineLogic.ReadElement(new MedicineSearchModel { Id = medicineId })!);
}
_recipeLogic.Update(new RecipeBindingModel
{
Id = id,
IssueDate = issuedate,
DoctorId = APIClient.Doctor.Id,
RecipeMedicines = recipeMedicines
});
Response.Redirect("/Recipe/Recipes");
}
///
/// Удалить рецепт
///
///
[HttpPost]
public void DeleteRecipe(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_recipeLogic.Delete(new RecipeBindingModel
{
Id = id
});
Response.Redirect("/Recipe/Recipes");
}
}
}