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

92 lines
3.1 KiB
C#

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(int id, string name, string comment, DateTime dateStart, DateTime? dateStop)
{
if (HttpContext.Request.Method == "GET")
{
ViewData["Title"] = "Новый диагноз";
return View("DiagnoseForm");
}
else
{
// TODO прописать UserId
DiagnoseBindingModel diagnose = new DiagnoseBindingModel
{
Id = id,
Name = name,
Comment = comment,
DateStartDiagnose = dateStart,
DateStopDiagnose = dateStop,
};
_diagnoseLogic.Create(diagnose);
return RedirectToAction("Index");
}
}
[HttpGet]
[HttpPost]
public IActionResult Edit(int id, string name, string comment, DateTime dateStart, DateTime? dateStop)
{
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 = id,
Name = name,
Comment = comment,
DateStartDiagnose = dateStart,
DateStopDiagnose = dateStop,
};
_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");
}
}
}