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

192 lines
4.6 KiB
C#

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
{
/// <summary>
/// Контроллер для сущности "Рецепт"
/// </summary>
public class RecipeController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger<RecipeController> _logger;
/// <summary>
/// Бизнес-логика для сущности "Рецепт"
/// </summary>
private readonly IRecipeLogic _recipeLogic;
/// <summary>
/// Бизнес-логика для сущности "Лекарство"
/// </summary>
private readonly IMedicineLogic _medicineLogic;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="recipeLogic"></param>
/// <param name="medicineLogic"></param>
public RecipeController(ILogger<RecipeController> logger, IRecipeLogic recipeLogic, IMedicineLogic medicineLogic)
{
_logger = logger;
_recipeLogic = recipeLogic;
_medicineLogic = medicineLogic;
}
/// <summary>
/// Вывести список рецептов
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Recipes()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(_recipeLogic.ReadList(new RecipeSearchModel
{
DoctorId = APIClient.Doctor.Id,
}));
}
/// <summary>
/// Создать рецепт
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateRecipe()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = _medicineLogic.ReadList(null);
return View();
}
/// <summary>
/// Создать рецепт
/// </summary>
/// <param name="issuedate"></param>
/// <param name="medicines"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void CreateRecipe(DateTime issuedate, List<int> medicines)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (issuedate == DateTime.MinValue || medicines == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, IMedicineModel> recipeMedicines = new Dictionary<int, IMedicineModel>();
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");
}
/// <summary>
/// Редактировать рецепт
/// </summary>
/// <returns></returns>
[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
}));
}
/// <summary>
/// Редактировать рецепт
/// </summary>
/// <param name="id"></param>
/// <param name="issuedate"></param>
/// <param name="medicines"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void UpdateRecipe(int id, DateTime issuedate, List<int> medicines)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (issuedate == DateTime.MinValue || medicines == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, IMedicineModel> recipeMedicines = new Dictionary<int, IMedicineModel>();
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");
}
/// <summary>
/// Удалить рецепт
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteRecipe(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_recipeLogic.Delete(new RecipeBindingModel
{
Id = id
});
Response.Redirect("/Recipe/Recipes");
}
}
}