2024-05-29 00:35:18 +04:00
|
|
|
|
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()
|
|
|
|
|
{
|
2024-05-29 02:22:44 +04:00
|
|
|
|
var currentUser = LoginManager.LogginedUser;
|
|
|
|
|
if (currentUser == null)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "User");
|
|
|
|
|
}
|
2024-05-29 00:35:18 +04:00
|
|
|
|
List<CourseViewModel> courses = _courseLogic.ReadList();
|
|
|
|
|
ViewData["Title"] = "Список курсов";
|
|
|
|
|
return View("CoursesList", courses);
|
|
|
|
|
}
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult Add(CourseFormModel model, int[] selectedDiagnoses)
|
|
|
|
|
{
|
2024-05-29 02:22:44 +04:00
|
|
|
|
var currentUser = LoginManager.LogginedUser;
|
|
|
|
|
if (currentUser == null)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "User");
|
|
|
|
|
}
|
2024-05-29 00:35:18 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-05-29 02:22:44 +04:00
|
|
|
|
var currentUser = LoginManager.LogginedUser;
|
|
|
|
|
if (currentUser == null)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "User");
|
|
|
|
|
}
|
2024-05-29 00:35:18 +04:00
|
|
|
|
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()
|
|
|
|
|
};
|
2024-05-30 09:36:16 +04:00
|
|
|
|
ViewData["Title"] = "Редактировать курс";
|
2024-05-29 00:35:18 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-05-29 02:22:44 +04:00
|
|
|
|
var currentUser = LoginManager.LogginedUser;
|
|
|
|
|
if (currentUser == null)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "User");
|
|
|
|
|
}
|
2024-05-29 00:35:18 +04:00
|
|
|
|
var obj = _courseLogic.ReadElement(new CourseSearchModel { Id = id });
|
|
|
|
|
if (obj != null)
|
|
|
|
|
{
|
|
|
|
|
_courseLogic.Delete(new CourseBindingModel { Id = obj.Id });
|
|
|
|
|
}
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|