diff --git a/Polyclinic/PolyclinicDatabaseImplement/Implements/CourseStorage.cs b/Polyclinic/PolyclinicDatabaseImplement/Implements/CourseStorage.cs index e49cdd8..acd0dea 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Implements/CourseStorage.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Implements/CourseStorage.cs @@ -64,14 +64,25 @@ namespace PolyclinicDatabaseImplement.Implements public CourseViewModel? Update(CourseBindingModel model) { using var context = new PolyclinicDatabase(); - var element = context.Courses.FirstOrDefault(x => x.Id == model.Id); - if (element == null) + using var transaction = context.Database.BeginTransaction(); + try { - return null; + var element = context.Courses.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + element.UpdateDiagnoses(context, model); + transaction.Commit(); + return element.GetViewModel; + } + catch (Exception ex) + { + transaction.Rollback(); + throw; } - element.Update(model); - context.SaveChanges(); - return element.GetViewModel; } } } diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs index d709fcf..1ba4d2d 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs @@ -71,8 +71,7 @@ namespace PolyclinicDatabaseImplement.Models if (courseDiagnoses != null && courseDiagnoses.Count > 0) { // удалили те, которых нет в модели - context.CourseDiagnoses.RemoveRange(courseDiagnoses - .Where(rec => !model.CourseDiagnoses.ContainsKey(rec.DiagnoseId))); + context.CourseDiagnoses.RemoveRange(courseDiagnoses); context.SaveChanges(); } var course = context.Courses.First(x => x.Id == Id); diff --git a/Polyclinic/PolyclinicWebAppImplementer/Controllers/CoursesController.cs b/Polyclinic/PolyclinicWebAppImplementer/Controllers/CoursesController.cs new file mode 100644 index 0000000..4ed72b6 --- /dev/null +++ b/Polyclinic/PolyclinicWebAppImplementer/Controllers/CoursesController.cs @@ -0,0 +1,105 @@ +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() + { + List courses = _courseLogic.ReadList(); + ViewData["Title"] = "Список курсов"; + return View("CoursesList", courses); + } + [HttpGet] + [HttpPost] + public IActionResult Add(CourseFormModel model, int[] selectedDiagnoses) + { + 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) + { + 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 obj = _courseLogic.ReadElement(new CourseSearchModel { Id = id }); + if (obj != null) + { + _courseLogic.Delete(new CourseBindingModel { Id = obj.Id }); + } + return RedirectToAction("Index"); + } + } +} diff --git a/Polyclinic/PolyclinicWebAppImplementer/Models/CourseFormModel.cs b/Polyclinic/PolyclinicWebAppImplementer/Models/CourseFormModel.cs new file mode 100644 index 0000000..6938690 --- /dev/null +++ b/Polyclinic/PolyclinicWebAppImplementer/Models/CourseFormModel.cs @@ -0,0 +1,10 @@ +using PolyclinicContracts.ViewModels; + +namespace PolyclinicWebAppImplementer.Models +{ + public class CourseFormModel + { + public CourseViewModel? CourseViewModel { get; set; } + public List<(DiagnoseViewModel Diagnose, bool IsChecked)> Diagnoses { get; set; } = new(); + } +} diff --git a/Polyclinic/PolyclinicWebAppImplementer/PolyclinicWebAppImplementer.csproj b/Polyclinic/PolyclinicWebAppImplementer/PolyclinicWebAppImplementer.csproj index 9a5a213..f097bc6 100644 --- a/Polyclinic/PolyclinicWebAppImplementer/PolyclinicWebAppImplementer.csproj +++ b/Polyclinic/PolyclinicWebAppImplementer/PolyclinicWebAppImplementer.csproj @@ -10,7 +10,7 @@ Always - + true PreserveNewest diff --git a/Polyclinic/PolyclinicWebAppImplementer/SiteMenuItems.cs b/Polyclinic/PolyclinicWebAppImplementer/SiteMenuItems.cs index c022948..393e79d 100644 --- a/Polyclinic/PolyclinicWebAppImplementer/SiteMenuItems.cs +++ b/Polyclinic/PolyclinicWebAppImplementer/SiteMenuItems.cs @@ -3,12 +3,9 @@ public static class SiteMenuItems { public static (string Controller, string Action, string Title) Index = ("Home", "", "Главная"); - public static (string Controller, string Action, string Title) Courses = ("Home", "Courses", "Курсы"); + public static (string Controller, string Action, string Title) Courses = ("Courses", "", "Курсы"); public static (string Controller, string Action, string Title) Diagnoses = ("Diagnoses", "", "Болезни"); public static (string Controller, string Action, string Title) Symptomes = ("Symptomes", "", "Симптомы"); - public static (string Controller, string Action, string Title) Symptom = ("Home", "Symptom", "Симптом"); - public static (string Controller, string Action, string Title) Diagnose = ("Home", "Diagnose", "Болезнь"); - public static (string Controller, string Action, string Title) Course = ("Home", "Course", "Курс"); public static (string Controller, string Action, string Title) Login = ("Home", "Login", "Вход"); public static (string Controller, string Action, string Title) Register = ("Home", "Register", "Регистрация"); public static (string Controller, string Action, string Title) Privacy = ("Home", "Privacy", "Политика приватности"); diff --git a/Polyclinic/PolyclinicWebAppImplementer/Views/Courses/CourseForm.cshtml b/Polyclinic/PolyclinicWebAppImplementer/Views/Courses/CourseForm.cshtml new file mode 100644 index 0000000..413be66 --- /dev/null +++ b/Polyclinic/PolyclinicWebAppImplementer/Views/Courses/CourseForm.cshtml @@ -0,0 +1,42 @@ +@model CourseFormModel +

@ViewData["Title"]

+
+
+
Количество дней:
+
+
+
+
Количество препарата в день:
+
+
+
+
Коментарий:
+
+
+
+
    + @foreach (var item in Model.Diagnoses) + { +
  1. + @if (item.IsChecked) + { + + } + else + { + + } + +
  2. + } +
+
+
+
+ + @Html.ActionLink("Отмена", "", "Courses", null, new { @class = "btn btn-danger" }) +
+
+
diff --git a/Polyclinic/PolyclinicWebAppImplementer/Views/Courses/CoursesList.cshtml b/Polyclinic/PolyclinicWebAppImplementer/Views/Courses/CoursesList.cshtml new file mode 100644 index 0000000..068a5c4 --- /dev/null +++ b/Polyclinic/PolyclinicWebAppImplementer/Views/Courses/CoursesList.cshtml @@ -0,0 +1,48 @@ +@using PolyclinicContracts.ViewModels +@model List +@{ + ViewBag.SelectedSiteMenuItem = SiteMenuItems.Courses; +} +
+ + + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + + + } + +
НомерКоличество днейКоличество препаратов в деньКомментарий
@item.Id@item.DaysCount@item.PillsPerDay@item.Comment +
+ +
+ + + + + +
+
\ No newline at end of file diff --git a/Polyclinic/PolyclinicWebAppImplementer/Views/Home/Course.cshtml b/Polyclinic/PolyclinicWebAppImplementer/Views/Home/Course.cshtml deleted file mode 100644 index 2e3efe0..0000000 --- a/Polyclinic/PolyclinicWebAppImplementer/Views/Home/Course.cshtml +++ /dev/null @@ -1,60 +0,0 @@ -@{ - ViewBag.SelectedSiteMenuItem = SiteMenuItems.Course; -} -

Новый курс лечения

-
-
-
Количество дней:
-
-
-
-
Количество препарата в день:
-
-
-
-
Коментарий:
-
-
-
-
-
Болезни
- - -
-
-
-
    - @{ - for (int i = 0; i < 7; i++) - { -
  1. - - - - - - Выбранная болезнь -
  2. - } - } -
-
-
-
- - @Html.ActionLink("Отмена", "Courses", "Home", null, new { @class = "btn btn-danger" }) -
-
-
diff --git a/Polyclinic/PolyclinicWebAppImplementer/Views/Home/Courses.cshtml b/Polyclinic/PolyclinicWebAppImplementer/Views/Home/Courses.cshtml deleted file mode 100644 index f2656b6..0000000 --- a/Polyclinic/PolyclinicWebAppImplementer/Views/Home/Courses.cshtml +++ /dev/null @@ -1,47 +0,0 @@ -@{ - ViewBag.SelectedSiteMenuItem = SiteMenuItems.Courses; -} -
- - - - - - - - - - - - - @{ - int count = 20; - for (int i = 0; i < count; i++) - { - - - - - - - - } - } - -
НомерКоличество днейКоличество препаратов в деньКомментарий
@i123Очень хороший курс приема - - - - - - - - - - -
-
\ No newline at end of file