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

168 lines
4.2 KiB
C#

using HospitalContracts.BindingModels;
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>
/// <param name="logger"></param>
public RecipeController(ILogger<RecipeController> logger)
{
_logger = logger;
}
/// <summary>
/// Вывести список рецептов
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Recipes()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<RecipeViewModel>>($"api/recipe/getrecipes?doctorId={APIClient.Doctor.Id}"));
}
/// <summary>
/// Создать рецепт
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateRecipe()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = APIClient.GetRequest<List<MedicineViewModel>>("api/medicine/getmedicines");
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, APIClient.GetRequest<MedicineViewModel>($"api/medicine/getmedicine?id={medicineId}"));
}
APIClient.PostRequest("api/recipe/createrecipe", new RecipeBindingModel
{
IssueDate = issuedate,
DoctorId = APIClient.Doctor.Id,
RecipeMedicines = recipeMedicines
});
Response.Redirect("Recipes");
}
/// <summary>
/// Редактировать рецепт
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult UpdateRecipe(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = APIClient.GetRequest<List<MedicineViewModel>>("api/medicine/getmedicines");
return View(APIClient.GetRequest<PatientViewModel>($"api/patient/getpatient?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, APIClient.GetRequest<MedicineViewModel>($"api/medicine/getmedicine?id={medicineId}"));
}
APIClient.PostRequest("api/recipe/updaterecipe", new RecipeBindingModel
{
Id = id,
IssueDate = issuedate,
DoctorId = APIClient.Doctor.Id,
RecipeMedicines = recipeMedicines
});
Response.Redirect("Recipes");
}
/// <summary>
/// Удалить рецепт
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteRecipe(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
APIClient.PostRequest($"api/recipe/deleterecipe", new RecipeBindingModel
{
Id = id
});
Response.Redirect("Recipes");
}
}
}