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

184 lines
5.0 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
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>
private readonly IDiseaseLogic _diseaseLogic;
/// <summary>
/// Бизнес-логика для сущности "Рецепт"
/// </summary>
private readonly IRecipeLogic _recipeLogic;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="diseaseLogic"></param>
/// <param name="recipeLogic"></param>
public DiseaseController(ILogger<DiseaseController> logger, IDiseaseLogic diseaseLogic, IRecipeLogic recipeLogic)
{
_logger = logger;
_diseaseLogic = diseaseLogic;
_recipeLogic = recipeLogic;
}
/// <summary>
/// Вывести список болезней
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Diseases()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(_diseaseLogic.ReadList(null));
}
/// <summary>
/// Создать болезнь
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateDisease()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Recipes = _recipeLogic.ReadList(new RecipeSearchModel
{
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 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");
}
/// <summary>
/// Редактировать болезнь
/// </summary>
/// <returns></returns>
[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
}));
}
/// <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 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");
}
/// <summary>
/// Удалить болезнь
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteDisease(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_diseaseLogic.Delete(new DiseaseBindingModel
{
Id = id
});
Response.Redirect("/Disease/Diseases");
}
}
}