From 3b067247ad0b8dd052e7d36b677f5fcd08215d5f Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Wed, 29 May 2024 00:35:18 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BA=D1=80=D1=83=D0=B4=20=D0=B4=D0=BB=D1=8F=20=D0=BA?= =?UTF-8?q?=D1=83=D1=80=D1=81=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/CourseStorage.cs | 23 +++- .../Models/Course.cs | 3 +- .../Controllers/CoursesController.cs | 105 ++++++++++++++++++ .../Models/CourseFormModel.cs | 10 ++ .../PolyclinicWebAppImplementer.csproj | 2 +- .../SiteMenuItems.cs | 5 +- .../Views/Courses/CourseForm.cshtml | 42 +++++++ .../Views/Courses/CoursesList.cshtml | 48 ++++++++ .../Views/Home/Course.cshtml | 60 ---------- .../Views/Home/Courses.cshtml | 47 -------- 10 files changed, 225 insertions(+), 120 deletions(-) create mode 100644 Polyclinic/PolyclinicWebAppImplementer/Controllers/CoursesController.cs create mode 100644 Polyclinic/PolyclinicWebAppImplementer/Models/CourseFormModel.cs create mode 100644 Polyclinic/PolyclinicWebAppImplementer/Views/Courses/CourseForm.cshtml create mode 100644 Polyclinic/PolyclinicWebAppImplementer/Views/Courses/CoursesList.cshtml delete mode 100644 Polyclinic/PolyclinicWebAppImplementer/Views/Home/Course.cshtml delete mode 100644 Polyclinic/PolyclinicWebAppImplementer/Views/Home/Courses.cshtml 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