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

184 lines
5.0 KiB
C#
Raw Normal View History

2024-05-22 00:58:17 +04:00
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
2024-05-22 00:58:17 +04:00
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)
2024-05-22 00:58:17 +04:00
{
_logger = logger;
_diseaseLogic = diseaseLogic;
_recipeLogic = recipeLogic;
2024-05-22 00:58:17 +04:00
}
/// <summary>
/// Вывести список болезней
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Diseases()
{
if (APIClient.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(_diseaseLogic.ReadList(null));
2024-05-22 00:58:17 +04:00
}
/// <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,
});
2024-05-22 00:58:17 +04:00
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)
2024-05-22 00:58:17 +04:00
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || recipe <= 0)
2024-05-22 00:58:17 +04:00
{
throw new Exception("Введены не все данные!");
}
_diseaseLogic.Create(new DiseaseBindingModel
2024-05-22 00:58:17 +04:00
{
Name = name,
Symptoms = symptoms,
RecipeId = recipe
2024-05-22 00:58:17 +04:00
});
Response.Redirect("/Disease/Diseases");
}
2024-05-22 00:58:17 +04:00
/// <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
}));
2024-05-22 00:58:17 +04:00
}
/// <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)
2024-05-22 00:58:17 +04:00
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || recipe <= 0)
2024-05-22 00:58:17 +04:00
{
throw new Exception("Введены не все данные!");
}
_diseaseLogic.Update(new DiseaseBindingModel
2024-05-22 00:58:17 +04:00
{
Id = id,
Name = name,
Symptoms = symptoms,
RecipeId = recipe
2024-05-22 00:58:17 +04:00
});
Response.Redirect("/Disease/Diseases");
}
2024-05-22 00:58:17 +04:00
/// <summary>
/// Удалить болезнь
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteDisease(int id)
{
if (APIClient.Doctor == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_diseaseLogic.Delete(new DiseaseBindingModel
2024-05-22 00:58:17 +04:00
{
Id = id
});
Response.Redirect("/Disease/Diseases");
}
2024-05-22 00:58:17 +04:00
}
}