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

119 lines
4.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.BusinessLogicsContracts;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.ViewModels;
using PolyclinicWebAppImplementer.Models;
using System.Net;
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()
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
List<DiagnoseViewModel> diagnoses = _diagnoseLogic.ReadList(new DiagnoseSearchModel { UserId = currentUser.Id });
ViewData["Title"] = "Список диагнозов";
return View("DiagnosesList", diagnoses);
}
[HttpGet]
[HttpPost]
public IActionResult Add(DiagnoseViewModel model)
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
if (HttpContext.Request.Method == "GET")
{
ViewData["Title"] = "Новый диагноз";
return View("DiagnoseForm");
}
else
{
DiagnoseBindingModel diagnose = new DiagnoseBindingModel
{
UserId = currentUser.Id,
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)
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
var obj = _diagnoseLogic.ReadElement(new DiagnoseSearchModel { Id = id });
if (obj.UserId != currentUser.Id)
{
return StatusCode(403, "Нельзя редактировать чужой диагноз");
}
if (HttpContext.Request.Method == "GET")
{
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 currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
var obj = _diagnoseLogic.ReadElement(new DiagnoseSearchModel { Id = id });
if (obj.UserId != currentUser.Id)
{
return StatusCode(403, "Нельзя удалить чужой диагноз");
}
if (obj != null)
{
_diagnoseLogic.Delete(new DiagnoseBindingModel { Id = obj.Id });
}
return RedirectToAction("Index");
}
}
}