79 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
namespace HospitalWeb.Controllers
{
public class RecipeController : Controller
{
private readonly ILogger<RecipeController> _logger;
public RecipeController(ILogger<RecipeController> logger)
{
_logger = logger;
}
[HttpGet("/recipe")]
public IActionResult Recipes()
{
if (APIClient.Apothecary == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<RecipeViewModel>>($"api/recipe/getrecipes?apothecaryId={APIClient.Apothecary.Id}"));
}
[HttpGet("/recipe/save/{id?}")]
public IActionResult Create(int? id)
{
if (APIClient.Apothecary == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = APIClient.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicines");
if (!id.HasValue)
{
return View(new RecipeViewModel());
}
var model = APIClient.GetRequest<RecipeViewModel?>($"api/recipe/getrecipe?id={id}");
return View(model);
}
[HttpPost("/recipe/save/{id?}")]
public void Create(RecipeBindingModel model)
{
if (APIClient.Apothecary == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
model.ApothecaryId = APIClient.Apothecary.Id;
if (model.Id != 0)
{
// тут надо вытащить ключи, потом по ним сделать массовый запрос сущностей лекарств, а потом вставить как значения в словарь
//model.RecipeMedicines =
APIClient.PostRequest("api/recipe/update", model);
}
else
{
APIClient.PostRequest("api/recipe/create", model);
}
Response.Redirect("/recipe");
}
[HttpPost("/recipe/delete")]
public void Delete(int id)
{
if (APIClient.Apothecary == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
APIClient.PostRequest($"api/recipe/delete", new RecipeBindingModel { Id = id });
Response.Redirect("/recipe");
}
}
}