CourseWork_Borschevskaya_A..../Hospital/HospitalWeb/Controllers/MedicineController.cs

123 lines
4.0 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Globalization;
namespace HospitalWeb.Controllers
{
public class MedicineController : Controller
{
private readonly ILogger<MedicineController> _logger;
public MedicineController(ILogger<MedicineController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
[HttpGet("/medicine")]
public IActionResult Medicines()
{
if (APIClient.Apothecary == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicines?apothecaryId={APIClient.Apothecary.Id}"));
}
[HttpGet("/medicine/save/{id?}")]
public IActionResult Create(int? id)
{
if (APIClient.Apothecary == null)
{
return Redirect("~/Home/Enter");
}
if (!id.HasValue)
{
return View();
}
var model = APIClient.GetRequest<MedicineViewModel?>($"api/medicine/getmedicine?id={id}");
return View(model);
}
[HttpPost("/medicine/save/{id?}")]
public void Create(int? id, string name, string costString, string dose)
{
if (APIClient.Apothecary == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
if (!double.TryParse(costString, NumberStyles.Any, CultureInfo.InvariantCulture, out double cost) || cost <= 0)
{
throw new Exception("Цена лекарства должна быть больше 0");
}
if (id.HasValue)
{
APIClient.PostRequest("api/medicine/update", new MedicineBindingModel
{
Id = id.Value,
Name = name,
Cost = cost,
Dose = dose
});
}
else
{
APIClient.PostRequest("api/medicine/create", new MedicineBindingModel
{
ApothecaryId = APIClient.Apothecary.Id,
Name = name,
Cost = cost,
Dose = dose
});
}
Response.Redirect("/medicine");
}
[HttpPost("/medicine/delete")]
public void Delete(int id)
{
if (APIClient.Apothecary == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
APIClient.PostRequest($"api/medicine/delete", new MedicineBindingModel { Id = id });
Response.Redirect("/medicine");
}
[HttpGet("/medicine/addProcedure")]
public IActionResult AddProcedureMedicine()
{
if (APIClient.Apothecary == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedures = APIClient.GetRequest<List<ProcedureViewModel>>($"api/procedure/getprocedures");
ViewBag.Medicines = APIClient.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicines?apothecaryId={APIClient.Apothecary.Id}");
return View();
}
[HttpPost("/medicine/addProcedure")]
public void AddProcedureMedicine(int medicine, int procedure)
{
if (APIClient.Apothecary == null)
{
throw new Exception("Доступно только авторизованным пользователям");
}
APIClient.PostRequest($"api/medicine/addproceduremedicine", (new MedicineBindingModel { Id = medicine}, new ProcedureBindingModel { Id = procedure}));
Response.Redirect("/medicine");
}
}
}