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

192 lines
4.6 KiB
C#
Raw Normal View History

2024-05-23 01:38:16 +04:00
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
2024-05-23 01:38:16 +04:00
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;
2024-05-23 01:38:16 +04:00
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="recipeLogic"></param>
/// <param name="medicineLogic"></param>
public RecipeController(ILogger<RecipeController> logger, IRecipeLogic recipeLogic, IMedicineLogic medicineLogic)
2024-05-23 01:38:16 +04:00
{
_logger = logger;
_recipeLogic = recipeLogic;
_medicineLogic = medicineLogic;
2024-05-23 01:38:16 +04:00
}
/// <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,
}));
2024-05-23 01:38:16 +04:00
}
/// <summary>
/// Создать рецепт
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateRecipe()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = _medicineLogic.ReadList(null);
2024-05-23 01:38:16 +04:00
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 })!);
2024-05-23 01:38:16 +04:00
}
_recipeLogic.Create(new RecipeBindingModel
2024-05-23 01:38:16 +04:00
{
IssueDate = issuedate,
DoctorId = APIClient.Doctor.Id,
RecipeMedicines = recipeMedicines
});
Response.Redirect("/Recipe/Recipes");
2024-05-23 01:38:16 +04:00
}
/// <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
}));
2024-05-23 01:38:16 +04:00
}
/// <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 })!);
2024-05-23 01:38:16 +04:00
}
_recipeLogic.Update(new RecipeBindingModel
2024-05-23 01:38:16 +04:00
{
Id = id,
IssueDate = issuedate,
DoctorId = APIClient.Doctor.Id,
RecipeMedicines = recipeMedicines
});
Response.Redirect("/Recipe/Recipes");
2024-05-23 01:38:16 +04:00
}
/// <summary>
/// Удалить рецепт
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteRecipe(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_recipeLogic.Delete(new RecipeBindingModel
2024-05-23 01:38:16 +04:00
{
Id = id
});
Response.Redirect("/Recipe/Recipes");
2024-05-23 01:38:16 +04:00
}
}
}