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

126 lines
4.6 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 CoursesController : Controller
{
private readonly IDiagnoseLogic _diagnoseLogic;
private readonly ICourseLogic _courseLogic;
public CoursesController(IDiagnoseLogic diagnoseLogic, ICourseLogic courseLogic)
{
_diagnoseLogic = diagnoseLogic;
_courseLogic = courseLogic;
}
[HttpGet]
public IActionResult Index()
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
List<CourseViewModel> courses = _courseLogic.ReadList();
ViewData["Title"] = "Список курсов";
return View("CoursesList", courses);
}
[HttpGet]
[HttpPost]
public IActionResult Add(CourseFormModel 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("CourseForm", model);
}
else
{
var allDiagnoses = _diagnoseLogic.ReadList();
CourseBindingModel course = new CourseBindingModel
{
Comment = model.CourseViewModel.Comment,
DaysCount = model.CourseViewModel.DaysCount,
PillsPerDay = model.CourseViewModel.PillsPerDay,
CourseDiagnoses = selectedDiagnoses
.ToDictionary(
x => x,
x => allDiagnoses.Where(y => y.Id == x) as IDiagnoseModel
)
};
_courseLogic.Create(course);
return RedirectToAction("Index");
}
}
[HttpGet]
[HttpPost]
public IActionResult Edit(int id, CourseFormModel model, int[] selectedDiagnoses)
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
if (HttpContext.Request.Method == "GET")
{
var obj = _courseLogic.ReadElement(new CourseSearchModel { Id = id });
model = new()
{
CourseViewModel = obj,
Diagnoses = _diagnoseLogic.ReadList().Select(x => (x, obj.CourseDiagnoses.ContainsKey(x.Id))).ToList()
};
ViewData["Title"] = "Редактировать симптом";
return View("CourseForm", model);
}
else
{
var allDiagnoses = _diagnoseLogic.ReadList();
CourseBindingModel course = new CourseBindingModel
{
Id = id,
Comment = model.CourseViewModel.Comment,
DaysCount = model.CourseViewModel.DaysCount,
PillsPerDay = model.CourseViewModel.PillsPerDay,
CourseDiagnoses = selectedDiagnoses
.ToDictionary(
x => x,
x => allDiagnoses.Where(y => y.Id == x) as IDiagnoseModel
)
};
_courseLogic.Update(course);
return RedirectToAction("Index");
}
}
[HttpPost]
public IActionResult Delete(int id)
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
var obj = _courseLogic.ReadElement(new CourseSearchModel { Id = id });
if (obj != null)
{
_courseLogic.Delete(new CourseBindingModel { Id = obj.Id });
}
return RedirectToAction("Index");
}
}
}