using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace HospitalWebApp.Controllers
{
///
/// Контроллер для сущности "Болезнь"
///
public class DiseaseController : Controller
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Бизнес-логика для сущности "Болезнь"
///
private readonly IDiseaseLogic _diseaseLogic;
///
/// Бизнес-логика для сущности "Рецепт"
///
private readonly IRecipeLogic _recipeLogic;
///
/// Конструктор
///
///
///
///
public DiseaseController(ILogger logger, IDiseaseLogic diseaseLogic, IRecipeLogic recipeLogic)
{
_logger = logger;
_diseaseLogic = diseaseLogic;
_recipeLogic = recipeLogic;
}
///
/// Вывести список болезней
///
///
[HttpGet]
public IActionResult Diseases()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(_diseaseLogic.ReadList(null));
}
///
/// Создать болезнь
///
///
[HttpGet]
public IActionResult CreateDisease()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Recipes = _recipeLogic.ReadList(new RecipeSearchModel
{
DoctorId = APIClient.Doctor.Id,
});
return View();
}
///
/// Создать болезнь
///
///
///
///
///
[HttpPost]
public void CreateDisease(string name, string? symptoms, int recipe)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || recipe <= 0)
{
throw new Exception("Введены не все данные!");
}
_diseaseLogic.Create(new DiseaseBindingModel
{
Name = name,
Symptoms = symptoms,
RecipeId = recipe
});
Response.Redirect("/Disease/Diseases");
}
///
/// Редактировать болезнь
///
///
[HttpGet]
public IActionResult UpdateDisease(int id)
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Recipes = _recipeLogic.ReadList(new RecipeSearchModel
{
DoctorId = APIClient.Doctor.Id,
});
return View(_diseaseLogic.ReadElement(new DiseaseSearchModel
{
Id = id
}));
}
///
/// Редактировать болезнь
///
/// ///
///
///
///
///
[HttpPost]
public void UpdateDisease(int id, string name, string? symptoms, int recipe)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || recipe <= 0)
{
throw new Exception("Введены не все данные!");
}
_diseaseLogic.Update(new DiseaseBindingModel
{
Id = id,
Name = name,
Symptoms = symptoms,
RecipeId = recipe
});
Response.Redirect("/Disease/Diseases");
}
///
/// Удалить болезнь
///
///
[HttpPost]
public void DeleteDisease(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_diseaseLogic.Delete(new DiseaseBindingModel
{
Id = id
});
Response.Redirect("/Disease/Diseases");
}
}
}