PIbd-21_MasenkinMS_Coursewo.../Hospital/HospitalWebApp/Controllers/DiseaseController.cs

157 lines
4.6 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace HospitalWebApp.Controllers
{
/// <summary>
/// Контроллер для сущности "Болезнь"
/// </summary>
public class DiseaseController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger<DiseaseController> _logger;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
public DiseaseController(ILogger<DiseaseController> logger)
{
_logger = logger;
}
/// <summary>
/// Вывести список болезней
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Diseases()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<DiseaseViewModel>>($"api/disease/getdiseases"));
}
/// <summary>
/// Создать болезнь
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateDisease()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Recipes = APIClient.GetRequest<List<RecipeViewModel>>($"api/recipe/getrecipes?doctorId={APIClient.Doctor.Id}");
return View();
}
/// <summary>
/// Создать болезнь
/// </summary>
/// <param name="name"></param>
/// <param name="symptoms"></param>
/// <param name="recipeId"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void CreateDisease(string name, string? symptoms, int recipeId)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || recipeId <= 0)
{
throw new Exception("Введены не все данные!");
}
APIClient.PostRequest("api/disease/createdisease", new DiseaseBindingModel
{
Name = name,
Symptoms = symptoms,
RecipeId = recipeId
});
Response.Redirect("Diseases");
}
/// <summary>
/// Редактировать болезнь
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult UpdateDisease(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Recipes = APIClient.GetRequest<List<RecipeViewModel>>($"api/recipe/getrecipes?doctorId={APIClient.Doctor.Id}");
return View(APIClient.GetRequest<DiseaseViewModel>($"api/disease/getdisease?id={id}"));
}
/// <summary>
/// Редактировать болезнь
/// </summary>
/// /// <param name="id"></param>
/// <param name="name"></param>
/// <param name="symptoms"></param>
/// <param name="recipeId"></param>
/// <exception cref="Exception"></exception>
[HttpPost]
public void UpdateDisease(int id, string name, string? symptoms, int recipeId)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || recipeId <= 0)
{
throw new Exception("Введены не все данные!");
}
APIClient.PostRequest("api/disease/updatedisease", new DiseaseBindingModel
{
Id = id,
Name = name,
Symptoms = symptoms,
RecipeId = recipeId
});
Response.Redirect("Diseases");
}
/// <summary>
/// Удалить болезнь
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteDisease(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
APIClient.PostRequest($"api/disease/deletedisease", new DiseaseBindingModel
{
Id = id
});
Response.Redirect("Diseases");
}
}
}