using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
using System.Numerics;
namespace HospitalWebApp.Controllers
{
///
/// Контроллер для сущности "Рецепт"
///
public class RecipeController : Controller
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Конструктор
///
///
public RecipeController(ILogger logger)
{
_logger = logger;
}
///
/// Вывести список рецептов
///
///
[HttpGet]
public IActionResult Recipes()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest>($"api/recipe/getrecipes?doctorId={APIClient.Doctor.Id}"));
}
///
/// Создать рецепт
///
///
[HttpGet]
public IActionResult CreateRecipe()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = APIClient.GetRequest>("api/medicine/getmedicines");
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, APIClient.GetRequest($"api/medicine/getmedicine?id={medicineId}"));
}
APIClient.PostRequest("api/recipe/createrecipe", new RecipeBindingModel
{
IssueDate = issuedate,
DoctorId = APIClient.Doctor.Id,
RecipeMedicines = recipeMedicines
});
Response.Redirect("Recipes");
}
///
/// Редактировать рецепт
///
///
[HttpGet]
public IActionResult UpdateRecipe(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = APIClient.GetRequest>("api/medicine/getmedicines");
return View(APIClient.GetRequest($"api/patient/getpatient?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, APIClient.GetRequest($"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");
}
///
/// Удалить рецепт
///
///
[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");
}
}
}