CourseWork_Borschevskaya_A..../Hospital/HospitalWeb/Controllers/RecipeController.cs

78 lines
2.5 KiB
C#

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");
ViewBag.Treatments = APIClient.GetRequest<List<TreatmentViewModel>>($"api/treatment/gettreatments");
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)
{
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");
}
}
}