CourseWork_Borschevskaya_A..../Hospital/HospitalWeb/Controllers/PrescriptionController.cs

86 lines
2.9 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace HospitalWeb.Controllers
{
public class PrescriptionController : Controller
{
private readonly ILogger<PrescriptionController> _logger;
public PrescriptionController(ILogger<PrescriptionController> logger)
{
_logger = logger;
}
[HttpGet("/prescription")]
public IActionResult Prescriptions()
{
if (APIClient.Apothecary == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<PrescriptionViewModel>>($"api/prescription/getprescriptions?apothecaryId={APIClient.Apothecary.Id}"));
}
[HttpGet("/prescription/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();
}
var model = APIClient.GetRequest<PrescriptionViewModel?>($"api/prescription/getprescription?id={id}");
return View(model);
}
[HttpPost("/prescription/save/{id?}")]
public void Create(int? id, int medicine, int number)
{
if (APIClient.Apothecary == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
if (number <= 0)
{
throw new Exception("Количество лекарств должно быть больше 0");
}
if (id.HasValue)
{
APIClient.PostRequest("api/prescription/update", new PrescriptionBindingModel
{
Id = id.Value,
Number = number
});
}
else {
APIClient.PostRequest("api/prescription/create", new PrescriptionBindingModel
{
ApothecaryId = APIClient.Apothecary.Id,
MedicineId = medicine,
Number = number
});
}
Response.Redirect("/prescription");
}
[HttpPost("/prescription/delete")]
public void Delete(int id)
{
if (APIClient.Apothecary == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
APIClient.PostRequest($"api/prescription/delete", new PrescriptionBindingModel { Id = id });
Response.Redirect("/prescription");
}
}
}