PIbd-21_CourseWork_Polyclin.../Polyclinic/PolyclinicWebAppImplementer/Controllers/SymptomesController.cs

122 lines
4.5 KiB
C#

using Microsoft.AspNetCore.Mvc;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.BusinessLogicsContracts;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.ViewModels;
using PolyclinicDataModels.Models;
using PolyclinicWebAppImplementer.Models;
namespace PolyclinicWebAppImplementer.Controllers
{
public class SymptomesController : Controller
{
private readonly ISymptomLogic _symptomLogic;
private readonly IDiagnoseLogic _diagnoseLogic;
public SymptomesController(ISymptomLogic symptomLogic, IDiagnoseLogic diagnoseLogic)
{
_symptomLogic = symptomLogic;
_diagnoseLogic = diagnoseLogic;
}
[HttpGet]
public IActionResult Index()
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
List<SymptomViewModel> symptomes = _symptomLogic.ReadList();
ViewData["Title"] = "Список симптомов";
return View("SymptomesList", symptomes);
}
[HttpGet]
[HttpPost]
public IActionResult Add(SymptomFormModel model, int[] selectedDiagnoses)
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
if (HttpContext.Request.Method == "GET")
{
ViewData["Title"] = "Новый симптом";
model = new()
{
Diagnoses = _diagnoseLogic.ReadList().Select(x => (x, false)).ToList()
};
return View("SymptomForm", model);
}
else
{
var allDiagnoses = _diagnoseLogic.ReadList();
SymptomBindingModel symptom = new SymptomBindingModel
{
Name = model.SymptomViewModel.Name,
Comment = model.SymptomViewModel.Comment,
SymptomDiagnoses = selectedDiagnoses
.ToDictionary(
x => x,
x => allDiagnoses.Where(y => y.Id == x) as IDiagnoseModel
)
};
_symptomLogic.Create(symptom);
return RedirectToAction("Index");
}
}
[HttpGet]
[HttpPost]
public IActionResult Edit(int id, SymptomFormModel model, int[] selectedDiagnoses)
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
if (HttpContext.Request.Method == "GET")
{
var obj = _symptomLogic.ReadElement(new SymptomSearchModel { Id = id });
model = new()
{
SymptomViewModel = obj,
Diagnoses = _diagnoseLogic.ReadList().Select(x => (x, obj.SymptomDiagnoses.ContainsKey(x.Id))).ToList()
};
ViewData["Title"] = "Редактировать симптом";
return View("SymptomForm", model);
}
else
{
var allDiagnoses = _diagnoseLogic.ReadList();
SymptomBindingModel symptom = new SymptomBindingModel
{
Id = id,
Name = model.SymptomViewModel.Name,
Comment = model.SymptomViewModel.Comment,
SymptomDiagnoses = selectedDiagnoses
.ToDictionary(
x => x,
x => allDiagnoses.Where(y => y.Id == x) as IDiagnoseModel
)
};
_symptomLogic.Update(symptom);
return RedirectToAction("Index");
}
}
[HttpPost]
public IActionResult Delete(int id)
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
var obj = _symptomLogic.ReadElement(new SymptomSearchModel { Id = id });
if (obj != null)
{
_symptomLogic.Delete(new SymptomBindingModel { Id = obj.Id });
}
return RedirectToAction("Index");
}
}
}