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

91 lines
3.1 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.BusinessLogicsContracts;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.ViewModels;
using PolyclinicWebAppImplementer.Models;
namespace PolyclinicWebAppImplementer.Controllers
{
public class DiagnosesController : Controller
{
private readonly ILogger<DiagnosesController> _logger;
private readonly IDiagnoseLogic _diagnoseLogic;
public DiagnosesController(ILogger<DiagnosesController> logger, IDiagnoseLogic diagnoseLogic)
{
_logger = logger;
_diagnoseLogic = diagnoseLogic;
}
[HttpGet]
public IActionResult Index()
{
// TODO выводить только пользовательские диагнозы
List<DiagnoseViewModel> diagnoses = _diagnoseLogic.ReadList();
ViewData["Title"] = "Список диагнозов";
return View("DiagnosesList", diagnoses);
}
[HttpGet]
[HttpPost]
public IActionResult Add(DiagnoseViewModel model)
{
if (HttpContext.Request.Method == "GET")
{
ViewData["Title"] = "Новый диагноз";
return View("DiagnoseForm");
}
else
{
// TODO прописать UserId
DiagnoseBindingModel diagnose = new DiagnoseBindingModel
{
Name = model.Name,
Comment = model.Comment,
DateStartDiagnose = model.DateStartDiagnose,
DateStopDiagnose = model.DateStopDiagnose,
};
_diagnoseLogic.Create(diagnose);
return RedirectToAction("Index");
}
}
[HttpGet]
[HttpPost]
public IActionResult Edit(int id, DiagnoseViewModel model)
{
if (HttpContext.Request.Method == "GET")
{
var obj = _diagnoseLogic.ReadElement(new DiagnoseSearchModel { Id = id });
ViewData["Title"] = "Редактировать диагноз";
return View("DiagnoseForm", obj);
}
else
{
DiagnoseBindingModel diagnose = new DiagnoseBindingModel
{
Id = model.Id,
Name = model.Name,
Comment = model.Comment,
DateStartDiagnose = model.DateStartDiagnose,
DateStopDiagnose = model.DateStopDiagnose,
};
_diagnoseLogic.Update(diagnose);
return RedirectToAction("Index");
}
}
[HttpPost]
public IActionResult Delete(int id)
{
var obj = _diagnoseLogic.ReadElement(new DiagnoseSearchModel { Id = id });
if (obj != null)
{
_diagnoseLogic.Delete(new DiagnoseBindingModel { Id = obj.Id });
}
return RedirectToAction("Index");
}
}
}