diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/ClientLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/ClientLogic.cs index e446450..dbf8b48 100644 --- a/Hospital/HospitalBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/ClientLogic.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace HospitalBusinessLogic.BusinessLogics @@ -107,6 +108,15 @@ namespace HospitalBusinessLogic.BusinessLogics { throw new ArgumentNullException("Нет пароля клиента", nameof(model.Password)); } + if (!Regex.IsMatch(model.Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase)) + { + throw new ArgumentException("Неправильно введенный email", nameof(model.Email)); + } + + if (!Regex.IsMatch(model.Password, @"^^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w+))[\w\d\W]*$", RegexOptions.IgnoreCase)) + { + throw new ArgumentException("Неправильно введенный пароль", nameof(model.Password)); + } _logger.LogInformation("Client. ClientFIO: {ClientFIO}. Email: {Email}. Id: {Id}", model.ClientFIO, model.Email, model.Id); var element = _clientStorage.GetElement(new ClientSearchModel { diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/ProceduresLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/ProceduresLogic.cs index 2557789..75b0cf6 100644 --- a/Hospital/HospitalBusinessLogic/BusinessLogics/ProceduresLogic.cs +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/ProceduresLogic.cs @@ -3,6 +3,7 @@ using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.SearchModels; using HospitalContracts.StoragesContracts; using HospitalContracts.ViewModels; +using HospitalDataModels.Models; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -21,6 +22,38 @@ namespace HospitalBusinessLogic.BusinessLogics _logger = logger; _proceduresStorage = proceduresStorage; } + + public bool AddMedicines(ProceduresSearchModel model, IMedicinesModel medicine) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("AddMedicine.Id:{ Id}", model.Id); + var element = _proceduresStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("AddMedicine element not found"); + return false; + } + + _logger.LogInformation("AddMedicine find. Id:{Id}", element.Id); + + element.ProcedureMedicine[medicine.Id] = medicine; + + _proceduresStorage.Update(new() + { + Id = element.Id, + ProceduresName = element.ProceduresName, + Type = element.Type, + ClientId = element.ClientId, + ProcedureMedicine = element.ProcedureMedicine + }); + + return true; + } public bool Create(ProceduresBindingModel model) { CheckModel(model); diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/RecipesLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/RecipesLogic.cs index 8d256f2..3a46201 100644 --- a/Hospital/HospitalBusinessLogic/BusinessLogics/RecipesLogic.cs +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/RecipesLogic.cs @@ -130,6 +130,10 @@ namespace HospitalBusinessLogic.BusinessLogics { return; } + if (model.SymptomsId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор симптома", nameof(model.SymptomsId)); + } if (string.IsNullOrEmpty(model.Dose)) { throw new ArgumentNullException("В рецепте нет дозировки", nameof(model.Dose)); @@ -138,7 +142,7 @@ namespace HospitalBusinessLogic.BusinessLogics { throw new ArgumentNullException("Нет способа приема", nameof(model.ModeOfApplication)); } - _logger.LogInformation("Recipes.RecipesId:{Id}.Dose:{ Dose}.ModeOfApplication:{ ModeOfApplication}", model.Id, model.Dose, model.ModeOfApplication); + _logger.LogInformation("Recipes.RecipesId:{Id}.Dose:{ Dose}.ModeOfApplication:{ ModeOfApplication}. MedicineId: { MedicineId}", model.Id, model.Dose, model.ModeOfApplication, model.SymptomsId); } } } diff --git a/Hospital/HospitalClientApp/Controllers/HomeController.cs b/Hospital/HospitalClientApp/Controllers/HomeController.cs index 9115940..c8ee491 100644 --- a/Hospital/HospitalClientApp/Controllers/HomeController.cs +++ b/Hospital/HospitalClientApp/Controllers/HomeController.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; using System.Diagnostics; using System.Diagnostics.Metrics; +using System.Reflection; using System.Xml.Linq; namespace HospitalClientApp.Controllers @@ -123,23 +124,23 @@ namespace HospitalClientApp.Controllers { return Redirect("~/Home/Enter"); } - + ViewBag.Medicines = APIClient.GetRequest>("api/main/getmedicineslist"); if (!id.HasValue) { - return View(); + return View(new ProceduresViewModel()); } var model = APIClient.GetRequest($"api/main/getprocedure?id={id}"); return View(model); } [HttpPost] - public void CreateProcedure(int? id, string procedurename, string proceduretype ) + public void CreateProcedure(ProceduresBindingModel model, int? id, string procedurename, string proceduretype ) { if (APIClient.Client == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } - if (id.HasValue) + if (model.Id != 0) { APIClient.PostRequest("api/main/updateprocedure", new ProceduresBindingModel { @@ -199,6 +200,29 @@ namespace HospitalClientApp.Controllers ViewBag.Procedures = APIClient.GetRequest>($"api/main/getprocedurelist?clientId={APIClient.Client.Id}"); return View(); } + + //[HttpGet] + //public IActionResult AddDrugCourse() + //{ + // if (APIClient.Client == null) + // { + // return Redirect("~/Home/Enter"); + // } + // ViewBag.Medicines = APIClient.GetRequest>($"api/medicine/getmedicines"); + // ViewBag.DrugCourses = APIClient.GetRequest>($"api/drugcourse/getdrugcourses"); + // return View(); + //} + + //[HttpPost] + //public void AddMedicineDrugCourse(int medicine, int drugcourse) + //{ + // if (APIClient.Client == null) + // { + // throw new Exception("Доступно только авторизованным пользователям"); + // } + // APIClient.PostRequest($"api/medicine/addmedicinedrugcourse", (new DrugCourseBindingModel { Id = drugcourse }, new MedicineBindingModel { Id = medicine })); + // Response.Redirect("ListDrugCourses"); + //} /// /// ЛЕКАРСТВА /// @@ -310,19 +334,20 @@ namespace HospitalClientApp.Controllers { return Redirect("~/Home/Enter"); } - ViewBag.Medicines = APIClient.GetRequest>("api/main/getmedicineslist"); + ViewBag.Symptomses = APIClient.GetRequest>("api/main/getsymptoms"); ViewBag.Procedures = APIClient.GetRequest>("api/main/getprocedurelist"); if (!id.HasValue) { return View(new RecipesViewModel()); } - var model = APIClient.GetRequest($"api/main/getrecipe?id={id}"); + + var model = APIClient.GetRequest($"api/main/getprocedure?id={id}"); return View(model); } [HttpPost] - public void CreateRecipe(RecipesBindingModel model) + public void CreateRecipe(RecipesBindingModel model, int? id, int symptoms, string dose, string modeofapplication) { if (APIClient.Client == null) { @@ -335,7 +360,13 @@ namespace HospitalClientApp.Controllers } else { - APIClient.PostRequest("api/main/createrecipe", model); + APIClient.PostRequest("api/main/createrecipe", new RecipesBindingModel + { + ClientId = APIClient.Client.Id, + Dose = dose, + SymptomsId = symptoms, + ModeOfApplication = modeofapplication + }); } Response.Redirect("ListRecipes"); } @@ -364,6 +395,27 @@ namespace HospitalClientApp.Controllers } return Tuple.Create(result.Item1, table); } + [HttpPost] + public void DeleteRecipe(int recipe) + { + if (APIClient.Client == null) + { + throw new Exception("Доступно только авторизованным пользователям"); + } + + APIClient.PostRequest($"api/main/DeleteRecipe", new RecipesBindingModel { Id = recipe }); + Response.Redirect("ListRecipes"); + } + + public IActionResult DeleteRecipe() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Recipes = APIClient.GetRequest>($"api/main/GetRecipesList?clientId={APIClient.Client.Id}"); + return View(); + } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() diff --git a/Hospital/HospitalClientApp/Views/Home/CreateProcedure.cshtml b/Hospital/HospitalClientApp/Views/Home/CreateProcedure.cshtml index fa7051f..67d87a1 100644 --- a/Hospital/HospitalClientApp/Views/Home/CreateProcedure.cshtml +++ b/Hospital/HospitalClientApp/Views/Home/CreateProcedure.cshtml @@ -18,14 +18,80 @@ if (Model != null)
Название процедуры:
-
+
Тип:
+
+
Добавление лекарств
+
+
+
+ +
+
+ +
+
+
+
+
+

Лекарства

+ + + + + + + + + @foreach (var medicine in Model.ProcedureMedicine) + { + + + + + } + +
Название
@medicine.Value.MedicinesName + +
+
+ +@section scripts { + +} \ No newline at end of file diff --git a/Hospital/HospitalClientApp/Views/Home/CreateRecipe.cshtml b/Hospital/HospitalClientApp/Views/Home/CreateRecipe.cshtml index f87313a..98db028 100644 --- a/Hospital/HospitalClientApp/Views/Home/CreateRecipe.cshtml +++ b/Hospital/HospitalClientApp/Views/Home/CreateRecipe.cshtml @@ -17,26 +17,28 @@
-
Лекарство:
+
Привязка симптома:
- +
-
- -
-
- -
-
- +
Добавление процедур
+
+
+
+ +
+
+ +
+

Процедуры

- +
@@ -56,7 +58,6 @@
Название
-
@@ -65,16 +66,16 @@ @section scripts { } diff --git a/Hospital/HospitalClientApp/Views/Home/DeleteRecipe.cshtml b/Hospital/HospitalClientApp/Views/Home/DeleteRecipe.cshtml new file mode 100644 index 0000000..094983a --- /dev/null +++ b/Hospital/HospitalClientApp/Views/Home/DeleteRecipe.cshtml @@ -0,0 +1,16 @@ +@{ + ViewData["Title"] = "DeleteRecipe"; +} + +
+
+ +
+ +
+
+
+
+
+
+
diff --git a/Hospital/HospitalClientApp/Views/Home/ListRecipes.cshtml b/Hospital/HospitalClientApp/Views/Home/ListRecipes.cshtml index 25bed75..06a0a00 100644 --- a/Hospital/HospitalClientApp/Views/Home/ListRecipes.cshtml +++ b/Hospital/HospitalClientApp/Views/Home/ListRecipes.cshtml @@ -21,8 +21,6 @@

Создать рецепт Удалить - Добавить процедуры - Добавить симптомы

@@ -58,7 +56,7 @@ @Html.DisplayFor(modelItem => item.Dose)
- @Html.DisplayFor(modelItem => item.MedicinesName) + @Html.DisplayFor(modelItem => item.SymptomsId) Редактировать diff --git a/Hospital/HospitalRestApi/Controllers/MainController.cs b/Hospital/HospitalRestApi/Controllers/MainController.cs index 398e75c..10ef123 100644 --- a/Hospital/HospitalRestApi/Controllers/MainController.cs +++ b/Hospital/HospitalRestApi/Controllers/MainController.cs @@ -105,7 +105,7 @@ namespace HospitalRestApi.Controllers { try { - _procedure.Update(model); + _procedure.Update(GetModelWithMedicines(model)); } catch (Exception ex) { @@ -114,6 +114,17 @@ namespace HospitalRestApi.Controllers } } + private ProceduresBindingModel GetModelWithMedicines(ProceduresBindingModel model) + { + var medicines = _medicine.ReadList(new MedicinesSearchModel { Ids = model.ProcedureMedicine.Keys.ToArray() }); + if (medicines != null) + { + model.ProcedureMedicine = medicines.Where(m => model.ProcedureMedicine.Keys.Contains(m.Id)) + .ToDictionary(m => m.Id, m => m as IMedicinesModel); + } + return model; + } + [HttpPost] public void DeleteProcedure(ProceduresBindingModel model) { @@ -248,7 +259,7 @@ namespace HospitalRestApi.Controllers { try { - _recipe.Create(GetModelWithProcedures(model)); + _recipe.Create(model); } catch (Exception ex) { @@ -309,11 +320,23 @@ namespace HospitalRestApi.Controllers } catch (Exception ex) { - _logger.LogError(ex, "Ошибка добавления участника в план питания."); + _logger.LogError(ex, "Ошибка добавления процедуры в рецепт."); + throw; + } + } + [HttpPost] + public void AddMedicines(Tuple model) + { + try + { + _procedure.AddMedicines(model.Item1, model.Item2); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка добавления лекарства в процедуру."); throw; } } - private RecipesBindingModel GetModelWithProcedures( RecipesBindingModel model) { var medicines = _procedure.ReadList(new ProceduresSearchModel { Ids = model.RecipeProcedures.Keys.ToArray() }); @@ -324,6 +347,32 @@ namespace HospitalRestApi.Controllers } return model; } + + [HttpPost] + public void DeleteRecipe(RecipesBindingModel model) + { + try + { + _recipe.Delete(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления рецепта"); + throw; + } + } + [HttpGet] + public List? GetSymptoms() + { + try + { + return _symptom.ReadList(null); + } + catch (Exception ex) + { + throw; + } + } } } diff --git a/Hospital/HospitalRestApi/Program.cs b/Hospital/HospitalRestApi/Program.cs index d7b8eda..31d2001 100644 --- a/Hospital/HospitalRestApi/Program.cs +++ b/Hospital/HospitalRestApi/Program.cs @@ -1,6 +1,7 @@ using HospitalBusinessLogic.BusinessLogics; using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.StoragesContracts; +using HospitalDataBaseImplements; using HospitalDataBaseImplements.Implements; using Microsoft.OpenApi.Models; @@ -42,6 +43,7 @@ builder.Services.AddSwaggerGen(c => var app = builder.Build(); +LoaderFromXML.LoadSymptoms(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) diff --git a/HospitalContracts/BindingModels/ProceduresBindingModel.cs b/HospitalContracts/BindingModels/ProceduresBindingModel.cs index ea09603..1a34a4c 100644 --- a/HospitalContracts/BindingModels/ProceduresBindingModel.cs +++ b/HospitalContracts/BindingModels/ProceduresBindingModel.cs @@ -1,8 +1,10 @@ -using HospitalDataModels.Models; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace HospitalContracts.BindingModels @@ -13,5 +15,13 @@ namespace HospitalContracts.BindingModels public string ProceduresName { get; set; } = string.Empty; public string Type { get; set; } = string.Empty; public int ClientId { get; set; } + + public Dictionary ProcedureMedicine + { + get; + set; + } = new(); + + } } diff --git a/HospitalContracts/BindingModels/RecipesBindingModel.cs b/HospitalContracts/BindingModels/RecipesBindingModel.cs index 27055b5..5a15fa8 100644 --- a/HospitalContracts/BindingModels/RecipesBindingModel.cs +++ b/HospitalContracts/BindingModels/RecipesBindingModel.cs @@ -14,18 +14,12 @@ namespace HospitalContracts.BindingModels public DateTime Date { get; set; } = DateTime.Now; public string ModeOfApplication { get; set; } = string.Empty; public int ClientId { get; set; } - public int MedicinesId { get; set; } - //public Dictionary RecipeSymptoms - //{ - // get; - // set; - //} = new(); - public Dictionary RecipeProcedures { get; set; } = new(); + public int SymptomsId { get; set; } } } diff --git a/HospitalContracts/BusinessLogicsContracts/IProceduresLogic.cs b/HospitalContracts/BusinessLogicsContracts/IProceduresLogic.cs index 7a0b346..e3ce5e0 100644 --- a/HospitalContracts/BusinessLogicsContracts/IProceduresLogic.cs +++ b/HospitalContracts/BusinessLogicsContracts/IProceduresLogic.cs @@ -1,6 +1,7 @@ using HospitalContracts.BindingModels; using HospitalContracts.SearchModels; using HospitalContracts.ViewModels; +using HospitalDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -13,6 +14,7 @@ namespace HospitalContracts.BusinessLogicsContracts { List? ReadList(ProceduresSearchModel? model); ProceduresViewModel? ReadElement(ProceduresSearchModel model); + bool AddMedicines(ProceduresSearchModel model, IMedicinesModel member); bool Create(ProceduresBindingModel model); bool Update(ProceduresBindingModel model); bool Delete(ProceduresBindingModel model); diff --git a/HospitalContracts/BusinessLogicsContracts/IRecipesLogic.cs b/HospitalContracts/BusinessLogicsContracts/IRecipesLogic.cs index 8d468f8..b6b65ba 100644 --- a/HospitalContracts/BusinessLogicsContracts/IRecipesLogic.cs +++ b/HospitalContracts/BusinessLogicsContracts/IRecipesLogic.cs @@ -14,7 +14,7 @@ namespace HospitalContracts.BusinessLogicsContracts { List? ReadList(RecipesSearchModel? model); RecipesViewModel? ReadElement(RecipesSearchModel model); - bool AddProcedures(RecipesSearchModel model, IProceduresModel member); + bool AddProcedures(RecipesSearchModel model, IProceduresModel procedure); bool Create(RecipesBindingModel model); bool Update(RecipesBindingModel model); bool Delete(RecipesBindingModel model); diff --git a/HospitalContracts/SearchModels/MedicinesSearchModel.cs b/HospitalContracts/SearchModels/MedicinesSearchModel.cs index 9d4f405..f29d7c0 100644 --- a/HospitalContracts/SearchModels/MedicinesSearchModel.cs +++ b/HospitalContracts/SearchModels/MedicinesSearchModel.cs @@ -11,5 +11,7 @@ namespace HospitalContracts.SearchModels public int? Id { get; set; } public string? MedicinesName { get; set; } public int? ClientId { get; set; } + + public int[]? Ids { get; set; } } } diff --git a/HospitalContracts/ViewModels/MedicinesViewModel.cs b/HospitalContracts/ViewModels/MedicinesViewModel.cs index ca6bc5d..e819277 100644 --- a/HospitalContracts/ViewModels/MedicinesViewModel.cs +++ b/HospitalContracts/ViewModels/MedicinesViewModel.cs @@ -12,7 +12,7 @@ namespace HospitalContracts.ViewModels { public int Id { get; set; } [DisplayName("Название лекарства")] - public string MedicinesName { get; set; } = string.Empty; + public string MedicinesName { get; set; } [DisplayName("Группа")] public string Group { get; set; } = string.Empty; public int ClientId { get; set; } diff --git a/HospitalContracts/ViewModels/ProceduresViewModel.cs b/HospitalContracts/ViewModels/ProceduresViewModel.cs index 09580aa..12e6cfd 100644 --- a/HospitalContracts/ViewModels/ProceduresViewModel.cs +++ b/HospitalContracts/ViewModels/ProceduresViewModel.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace HospitalContracts.ViewModels @@ -16,5 +17,17 @@ namespace HospitalContracts.ViewModels [DisplayName("Тип процедуры")] public string Type { get; set; } = string.Empty; public int ClientId { get; set; } + public Dictionary ProcedureMedicine + { + get; + set; + } = new(); + + public ProceduresViewModel() { } + [JsonConstructor] + public ProceduresViewModel(Dictionary ProcedureMedicine) + { + this.ProcedureMedicine = ProcedureMedicine.ToDictionary(x => x.Key, x => x.Value as IMedicinesModel); + } } } diff --git a/HospitalContracts/ViewModels/RecipesViewModel.cs b/HospitalContracts/ViewModels/RecipesViewModel.cs index 77f79ef..c7e2245 100644 --- a/HospitalContracts/ViewModels/RecipesViewModel.cs +++ b/HospitalContracts/ViewModels/RecipesViewModel.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace HospitalContracts.ViewModels @@ -20,20 +21,22 @@ namespace HospitalContracts.ViewModels public string ClientFIO { get; set; } = string.Empty; [DisplayName("Способ приготовления")] public string ModeOfApplication { get; set; } = string.Empty; - [DisplayName("Лекарство")] - public string MedicinesName { get; set; } = string.Empty; - public int MedicinesId { get; set; } - //[DisplayName("Симптом")] - //public Dictionary RecipeSymptoms - //{ - // get; - // set; - //} = new(); + [DisplayName("Процедура")] public Dictionary RecipeProcedures { get; set; } = new(); + [DisplayName("Симптом")] + public int SymptomsId { get; set; } + + public RecipesViewModel() { } + + [JsonConstructor] + public RecipesViewModel(Dictionary RecipeProcedures) + { + this.RecipeProcedures = RecipeProcedures.ToDictionary(x => x.Key, x => x.Value as IProceduresModel); + } } } diff --git a/HospitalContracts/ViewModels/SymptomsViewModel.cs b/HospitalContracts/ViewModels/SymptomsViewModel.cs index 78b71b8..430e7d4 100644 --- a/HospitalContracts/ViewModels/SymptomsViewModel.cs +++ b/HospitalContracts/ViewModels/SymptomsViewModel.cs @@ -14,6 +14,6 @@ namespace HospitalContracts.ViewModels [DisplayName("Симптом")] public string SymptomName { get; set; } = string.Empty; [DisplayName("Описание")] - public string Description { get; set; } = string.Empty; + public string? Description { get; set; } = string.Empty; } } diff --git a/HospitalDataBaseImplements/HospitalDatabase.cs b/HospitalDataBaseImplements/HospitalDatabase.cs index fa1502d..b1778a6 100644 --- a/HospitalDataBaseImplements/HospitalDatabase.cs +++ b/HospitalDataBaseImplements/HospitalDatabase.cs @@ -22,8 +22,8 @@ namespace HospitalDataBaseImplements public virtual DbSet Illnesses { set; get; } public virtual DbSet IllnessKurse { set; get; } public virtual DbSet IllnessSymptomses { set; get; } - public virtual DbSet RecipesSymptoms { set; get; } public virtual DbSet RecipesProcedures { set; get; } + public virtual DbSet ProcedureMedicine { set; get; } public virtual DbSet Symptomses { set; get; } public virtual DbSet Recipes { set; get; } public virtual DbSet Medicines { set; get; } diff --git a/HospitalDataBaseImplements/Implements/MedicinesStorage.cs b/HospitalDataBaseImplements/Implements/MedicinesStorage.cs index 7bf702b..09e55f7 100644 --- a/HospitalDataBaseImplements/Implements/MedicinesStorage.cs +++ b/HospitalDataBaseImplements/Implements/MedicinesStorage.cs @@ -42,7 +42,7 @@ namespace HospitalDataBaseImplements.Implements return null; } using var context = new HospitalDatabase(); - return context.Medicines + return context.Medicines.Include(x => x.Client) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.MedicinesName) && x.MedicinesName == model.MedicinesName) || @@ -51,20 +51,24 @@ namespace HospitalDataBaseImplements.Implements } public MedicinesViewModel? Insert(MedicinesBindingModel model) { + using var context = new HospitalDatabase(); var newMedicine = Medicines.Create(model); if (newMedicine == null) { return null; } - using var context = new HospitalDatabase(); context.Medicines.Add(newMedicine); context.SaveChanges(); - return newMedicine.GetViewModel; + return context.Medicines.Include(x => x.Client).FirstOrDefault(x => x.Id == newMedicine.Id) + ?.GetViewModel; } public MedicinesViewModel? Update(MedicinesBindingModel model) { using var context = new HospitalDatabase(); - var medicine = context.Medicines.FirstOrDefault(x => x.Id == model.Id); + using var transaction = context.Database.BeginTransaction(); + try + { + var medicine = context.Medicines.FirstOrDefault(x => x.Id == model.Id); if (medicine == null) { return null; @@ -72,6 +76,12 @@ namespace HospitalDataBaseImplements.Implements medicine.Update(model); context.SaveChanges(); return medicine.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } } public MedicinesViewModel? Delete(MedicinesBindingModel model) { diff --git a/HospitalDataBaseImplements/Implements/ProceduresStorage.cs b/HospitalDataBaseImplements/Implements/ProceduresStorage.cs index 0396fde..140f918 100644 --- a/HospitalDataBaseImplements/Implements/ProceduresStorage.cs +++ b/HospitalDataBaseImplements/Implements/ProceduresStorage.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace HospitalDataBaseImplements.Implements { @@ -17,7 +18,8 @@ namespace HospitalDataBaseImplements.Implements public List GetFullList() { using var context = new HospitalDatabase(); - return context.Procedures.Include(x => x.Client) + return context.Procedures.Include(x => x.Client).Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) .Select(x => x.GetViewModel) .ToList(); } @@ -41,7 +43,7 @@ namespace HospitalDataBaseImplements.Implements return null; } using var context = new HospitalDatabase(); - return context.Procedures + return context.Procedures.Include(x => x.Client) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProceduresName) && x.ProceduresName == model.ProceduresName) || @@ -58,24 +60,40 @@ namespace HospitalDataBaseImplements.Implements using var context = new HospitalDatabase(); context.Procedures.Add(newProcedures); context.SaveChanges(); - return newProcedures.GetViewModel; + return context.Procedures.Include(x => x.Client) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine).FirstOrDefault(x => x.Id == newProcedures.Id) + ?.GetViewModel; } public ProceduresViewModel? Update(ProceduresBindingModel model) { using var context = new HospitalDatabase(); - var procedure = context.Procedures.FirstOrDefault(x => x.Id == model.Id); + using var transaction = context.Database.BeginTransaction(); + try + { + var procedure = context.Procedures.FirstOrDefault(x => x.Id == model.Id); if (procedure == null) { return null; } procedure.Update(model); context.SaveChanges(); - return procedure.GetViewModel; + procedure.UpdateMedicines(context, model); + transaction.Commit(); + return procedure.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } } public ProceduresViewModel? Delete(ProceduresBindingModel model) { using var context = new HospitalDatabase(); - var element = context.Procedures.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Procedures.Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Procedures.Remove(element); diff --git a/HospitalDataBaseImplements/Implements/RecipesStorage.cs b/HospitalDataBaseImplements/Implements/RecipesStorage.cs index b8708fb..b257586 100644 --- a/HospitalDataBaseImplements/Implements/RecipesStorage.cs +++ b/HospitalDataBaseImplements/Implements/RecipesStorage.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace HospitalDataBaseImplements.Implements { @@ -17,7 +18,8 @@ namespace HospitalDataBaseImplements.Implements public RecipesViewModel? Delete(RecipesBindingModel model) { using var context = new HospitalDatabase(); - var element = context.Recipes.Include(x => x.Procedures).FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Recipes.Include(x => x.Procedures).Include(x => x.Symptoms) + .Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Recipes.Remove(element); @@ -38,11 +40,11 @@ namespace HospitalDataBaseImplements.Implements Include(x => x.Client). Include(x => x.Procedures). ThenInclude(x => x.Procedure). - Include(x => x.Medicines). + Include(x => x.Symptoms). FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))? .GetViewModel; } - + //НУЖЕН ТУТ ЕЩЕ ОДИН ТУ ЛИСТ??? public List GetFilteredList(RecipesSearchModel model) { if (model is null) @@ -54,9 +56,9 @@ namespace HospitalDataBaseImplements.Implements return context.Recipes. Include(x => x.Procedures). ThenInclude(x => x.Procedure) - .Include(x => x.Medicines) + .Include(x => x.Symptoms) .Include(x => x.Client) - .Where(x => x.ClientId == model.ClientId) + .Where(x => x.ClientId == model.ClientId).ToList() .Select(x => x.GetViewModel) .ToList(); } @@ -66,8 +68,9 @@ namespace HospitalDataBaseImplements.Implements using var context = new HospitalDatabase(); return context.Recipes.Include(x => x.Procedures). ThenInclude(x => x.Procedure) - .Include(x => x.Medicines) .Include(x => x.Client) + .Include(x => x.Symptoms) + .ToList() .Select(x => x.GetViewModel) .ToList(); } @@ -75,33 +78,44 @@ namespace HospitalDataBaseImplements.Implements public RecipesViewModel? Insert(RecipesBindingModel model) { using var context = new HospitalDatabase(); - var newDrugCourse = Recipes.Create(model); - if (newDrugCourse == null) + var newRecipe = Recipes.Create(model); + if (newRecipe == null) { return null; } - context.Recipes.Add(newDrugCourse); + context.Recipes.Add(newRecipe); context.SaveChanges(); return context.Recipes .Include(x => x.Procedures) .ThenInclude(x => x.Procedure) - .Include(x => x.Medicines) + .Include(x => x.Symptoms) .Include(x => x.Client) - .FirstOrDefault(x => x.Id == newDrugCourse.Id) + .FirstOrDefault(x => x.Id == newRecipe.Id) ?.GetViewModel; } public RecipesViewModel? Update(RecipesBindingModel model) { using var context = new HospitalDatabase(); - var recipe = context.Recipes.FirstOrDefault(x => x.Id == model.Id); + using var transaction = context.Database.BeginTransaction(); + try + { + var recipe = context.Recipes.FirstOrDefault(x => x.Id == model.Id); if (recipe == null) { return null; } recipe.Update(model); context.SaveChanges(); - return recipe.GetViewModel; + recipe.UpdateProcedures(context, model); + transaction.Commit(); + return recipe.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } } } } diff --git a/HospitalDataBaseImplements/LoaderFromXML.cs b/HospitalDataBaseImplements/LoaderFromXML.cs index 25fa72b..25aec0e 100644 --- a/HospitalDataBaseImplements/LoaderFromXML.cs +++ b/HospitalDataBaseImplements/LoaderFromXML.cs @@ -10,9 +10,9 @@ namespace HospitalDataBaseImplements { public class LoaderFromXML { - private static readonly string IllnessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Illness.xml"); + // private static readonly string IllnessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Illness.xml"); private static readonly string SymptomsFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Symptoms.xml"); - private static readonly string KursesFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Kurses.xml"); + // private static readonly string KursesFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Kurses.xml"); private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { @@ -26,35 +26,35 @@ namespace HospitalDataBaseImplements /// Чтение пациентов из XML-файла /// /// - public static void LoadIllness() // (запуск после загрузки курсов) - { - using var context = new HospitalDatabase(); - if (context.Illnesses.ToList().Count > 0) - return; - var list = LoadData(IllnessFileName, "Illness", x => Illness.Create(x)!)!; - list.ForEach(x => - { - context.Illnesses.Add(x); - }); - context.SaveChanges(); + //public static void LoadIllness() // (запуск после загрузки курсов) + //{ + // using var context = new HospitalDatabase(); + // if (context.Illnesses.ToList().Count > 0) + // return; + // var list = LoadData(IllnessFileName, "Illness", x => Illness.Create(x)!)!; + // list.ForEach(x => + // { + // context.Illnesses.Add(x); + // }); + // context.SaveChanges(); - } + //} /// /// Чтение лечений из XML-файла /// /// - public static void LoadKurses() // (запуск после загрузки симптомов) - { - using var context = new HospitalDatabase(); - if (context.Kurse.ToList().Count > 0) - return; - //var list = LoadData(KursesFileName, "Kurses", x => Kurses.Create(context, x)!)!; - //list.ForEach(x => - //{ - // context.Kurse.Add(x); - //}); - //context.SaveChanges(); - } + //public static void LoadKurses() // (запуск после загрузки симптомов) + //{ + // using var context = new HospitalDatabase(); + // if (context.Kurse.ToList().Count > 0) + // return; + // //var list = LoadData(KursesFileName, "Kurses", x => Kurses.Create(context, x)!)!; + // //list.ForEach(x => + // //{ + // // context.Kurse.Add(x); + // //}); + // //context.SaveChanges(); + //} /// /// Чтение поцедур из XML-файла /// @@ -64,12 +64,12 @@ namespace HospitalDataBaseImplements using var context = new HospitalDatabase(); if (context.Symptomses.ToList().Count > 0) return; - //var list = LoadData(SymptomsFileName, "Symptoms", x => Symptoms.Create(x)!)!; - //list.ForEach(x => - //{ - // context.Symptomses.Add(x); - //}); - //context.SaveChanges(); + var list = LoadData(SymptomsFileName, "Symptoms", x => Symptoms.Create(x)!)!; + list.ForEach(x => + { + context.Symptomses.Add(x); + }); + context.SaveChanges(); } } } diff --git a/HospitalDataBaseImplements/Migrations/20230520015950_InitialCreate.Designer.cs b/HospitalDataBaseImplements/Migrations/20230525103233_InitialCreate.Designer.cs similarity index 93% rename from HospitalDataBaseImplements/Migrations/20230520015950_InitialCreate.Designer.cs rename to HospitalDataBaseImplements/Migrations/20230525103233_InitialCreate.Designer.cs index f16806b..d6556ce 100644 --- a/HospitalDataBaseImplements/Migrations/20230520015950_InitialCreate.Designer.cs +++ b/HospitalDataBaseImplements/Migrations/20230525103233_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace HospitalDataBaseImplements.Migrations { [DbContext(typeof(HospitalDatabase))] - [Migration("20230520015950_InitialCreate")] + [Migration("20230525103233_InitialCreate")] partial class InitialCreate { /// @@ -172,6 +172,29 @@ namespace HospitalDataBaseImplements.Migrations b.ToTable("Medicines"); }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.ProcedureMedicine", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("MedicineId") + .HasColumnType("int"); + + b.Property("ProcedureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("MedicineId"); + + b.HasIndex("ProcedureId"); + + b.ToTable("ProcedureMedicine"); + }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.Procedures", b => { b.Property("Id") @@ -216,18 +239,18 @@ namespace HospitalDataBaseImplements.Migrations .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("MedicinesId") - .HasColumnType("int"); - b.Property("ModeOfApplication") .IsRequired() .HasColumnType("nvarchar(max)"); + b.Property("SymptomsId") + .HasColumnType("int"); + b.HasKey("Id"); b.HasIndex("ClientId"); - b.HasIndex("MedicinesId"); + b.HasIndex("SymptomsId"); b.ToTable("Recipes"); }); @@ -255,29 +278,6 @@ namespace HospitalDataBaseImplements.Migrations b.ToTable("RecipesProcedures"); }); - modelBuilder.Entity("HospitalDataBaseImplements.Models.RecipesSymptoms", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("RecipesId") - .HasColumnType("int"); - - b.Property("SymptomsId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("RecipesId"); - - b.HasIndex("SymptomsId"); - - b.ToTable("RecipesSymptoms"); - }); - modelBuilder.Entity("HospitalDataBaseImplements.Models.Symptoms", b => { b.Property("Id") @@ -358,6 +358,25 @@ namespace HospitalDataBaseImplements.Migrations b.Navigation("Client"); }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.ProcedureMedicine", b => + { + b.HasOne("HospitalDataBaseImplements.Models.Medicines", "Medicine") + .WithMany("Procedures") + .HasForeignKey("MedicineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HospitalDataBaseImplements.Models.Procedures", "Procedure") + .WithMany("Medicines") + .HasForeignKey("ProcedureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Medicine"); + + b.Navigation("Procedure"); + }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.Procedures", b => { b.HasOne("HospitalDataBaseImplements.Models.Client", "Client") @@ -377,15 +396,15 @@ namespace HospitalDataBaseImplements.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("HospitalDataBaseImplements.Models.Medicines", "Medicines") + b.HasOne("HospitalDataBaseImplements.Models.Symptoms", "Symptoms") .WithMany() - .HasForeignKey("MedicinesId") + .HasForeignKey("SymptomsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.Navigation("Client"); - b.Navigation("Medicines"); + b.Navigation("Symptoms"); }); modelBuilder.Entity("HospitalDataBaseImplements.Models.RecipesProcedures", b => @@ -407,25 +426,6 @@ namespace HospitalDataBaseImplements.Migrations b.Navigation("Recipe"); }); - modelBuilder.Entity("HospitalDataBaseImplements.Models.RecipesSymptoms", b => - { - b.HasOne("HospitalDataBaseImplements.Models.Recipes", "Recipe") - .WithMany() - .HasForeignKey("RecipesId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("HospitalDataBaseImplements.Models.Symptoms", "Symptoms") - .WithMany() - .HasForeignKey("SymptomsId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Recipe"); - - b.Navigation("Symptoms"); - }); - modelBuilder.Entity("HospitalDataBaseImplements.Models.Illness", b => { b.Navigation("Kurses"); @@ -438,6 +438,16 @@ namespace HospitalDataBaseImplements.Migrations b.Navigation("IllnessKurses"); }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b => + { + b.Navigation("Procedures"); + }); + + modelBuilder.Entity("HospitalDataBaseImplements.Models.Procedures", b => + { + b.Navigation("Medicines"); + }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.Recipes", b => { b.Navigation("Procedures"); diff --git a/HospitalDataBaseImplements/Migrations/20230520015950_InitialCreate.cs b/HospitalDataBaseImplements/Migrations/20230525103233_InitialCreate.cs similarity index 92% rename from HospitalDataBaseImplements/Migrations/20230520015950_InitialCreate.cs rename to HospitalDataBaseImplements/Migrations/20230525103233_InitialCreate.cs index 0eece3f..024549c 100644 --- a/HospitalDataBaseImplements/Migrations/20230520015950_InitialCreate.cs +++ b/HospitalDataBaseImplements/Migrations/20230525103233_InitialCreate.cs @@ -122,6 +122,35 @@ namespace HospitalDataBaseImplements.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "Recipes", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Dose = table.Column(type: "nvarchar(max)", nullable: false), + Date = table.Column(type: "datetime2", nullable: false), + ClientId = table.Column(type: "int", nullable: false), + ModeOfApplication = table.Column(type: "nvarchar(max)", nullable: false), + SymptomsId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Recipes", x => x.Id); + table.ForeignKey( + name: "FK_Recipes_Clients_ClientId", + column: x => x.ClientId, + principalTable: "Clients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Recipes_Symptomses_SymptomsId", + column: x => x.SymptomsId, + principalTable: "Symptomses", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "Kurse", columns: table => new @@ -145,60 +174,31 @@ namespace HospitalDataBaseImplements.Migrations }); migrationBuilder.CreateTable( - name: "Recipes", + name: "ProcedureMedicine", columns: table => new { Id = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), - Dose = table.Column(type: "nvarchar(max)", nullable: false), - Date = table.Column(type: "datetime2", nullable: false), - ClientId = table.Column(type: "int", nullable: false), - MedicinesId = table.Column(type: "int", nullable: false), - ModeOfApplication = table.Column(type: "nvarchar(max)", nullable: false) + MedicineId = table.Column(type: "int", nullable: false), + ProcedureId = table.Column(type: "int", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Recipes", x => x.Id); + table.PrimaryKey("PK_ProcedureMedicine", x => x.Id); table.ForeignKey( - name: "FK_Recipes_Clients_ClientId", - column: x => x.ClientId, - principalTable: "Clients", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_Recipes_Medicines_MedicinesId", - column: x => x.MedicinesId, + name: "FK_ProcedureMedicine_Medicines_MedicineId", + column: x => x.MedicineId, principalTable: "Medicines", principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ProcedureMedicine_Procedures_ProcedureId", + column: x => x.ProcedureId, + principalTable: "Procedures", + principalColumn: "Id", onDelete: ReferentialAction.Restrict); }); - migrationBuilder.CreateTable( - name: "IllnessKurse", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - IllnessId = table.Column(type: "int", nullable: false), - KurseId = table.Column(type: "int", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_IllnessKurse", x => x.Id); - table.ForeignKey( - name: "FK_IllnessKurse_Illnesses_IllnessId", - column: x => x.IllnessId, - principalTable: "Illnesses", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_IllnessKurse_Kurse_KurseId", - column: x => x.KurseId, - principalTable: "Kurse", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - migrationBuilder.CreateTable( name: "RecipesProcedures", columns: table => new @@ -226,27 +226,27 @@ namespace HospitalDataBaseImplements.Migrations }); migrationBuilder.CreateTable( - name: "RecipesSymptoms", + name: "IllnessKurse", columns: table => new { Id = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), - RecipesId = table.Column(type: "int", nullable: false), - SymptomsId = table.Column(type: "int", nullable: false) + IllnessId = table.Column(type: "int", nullable: false), + KurseId = table.Column(type: "int", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_RecipesSymptoms", x => x.Id); + table.PrimaryKey("PK_IllnessKurse", x => x.Id); table.ForeignKey( - name: "FK_RecipesSymptoms_Recipes_RecipesId", - column: x => x.RecipesId, - principalTable: "Recipes", + name: "FK_IllnessKurse_Illnesses_IllnessId", + column: x => x.IllnessId, + principalTable: "Illnesses", principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( - name: "FK_RecipesSymptoms_Symptomses_SymptomsId", - column: x => x.SymptomsId, - principalTable: "Symptomses", + name: "FK_IllnessKurse_Kurse_KurseId", + column: x => x.KurseId, + principalTable: "Kurse", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); @@ -281,6 +281,16 @@ namespace HospitalDataBaseImplements.Migrations table: "Medicines", column: "ClientId"); + migrationBuilder.CreateIndex( + name: "IX_ProcedureMedicine_MedicineId", + table: "ProcedureMedicine", + column: "MedicineId"); + + migrationBuilder.CreateIndex( + name: "IX_ProcedureMedicine_ProcedureId", + table: "ProcedureMedicine", + column: "ProcedureId"); + migrationBuilder.CreateIndex( name: "IX_Procedures_ClientId", table: "Procedures", @@ -292,9 +302,9 @@ namespace HospitalDataBaseImplements.Migrations column: "ClientId"); migrationBuilder.CreateIndex( - name: "IX_Recipes_MedicinesId", + name: "IX_Recipes_SymptomsId", table: "Recipes", - column: "MedicinesId"); + column: "SymptomsId"); migrationBuilder.CreateIndex( name: "IX_RecipesProcedures_ProcedureId", @@ -305,16 +315,6 @@ namespace HospitalDataBaseImplements.Migrations name: "IX_RecipesProcedures_RecipesId", table: "RecipesProcedures", column: "RecipesId"); - - migrationBuilder.CreateIndex( - name: "IX_RecipesSymptoms_RecipesId", - table: "RecipesSymptoms", - column: "RecipesId"); - - migrationBuilder.CreateIndex( - name: "IX_RecipesSymptoms_SymptomsId", - table: "RecipesSymptoms", - column: "SymptomsId"); } /// @@ -327,10 +327,10 @@ namespace HospitalDataBaseImplements.Migrations name: "IllnessSymptomses"); migrationBuilder.DropTable( - name: "RecipesProcedures"); + name: "ProcedureMedicine"); migrationBuilder.DropTable( - name: "RecipesSymptoms"); + name: "RecipesProcedures"); migrationBuilder.DropTable( name: "Kurse"); @@ -345,10 +345,10 @@ namespace HospitalDataBaseImplements.Migrations name: "Recipes"); migrationBuilder.DropTable( - name: "Symptomses"); + name: "Medicines"); migrationBuilder.DropTable( - name: "Medicines"); + name: "Symptomses"); migrationBuilder.DropTable( name: "Clients"); diff --git a/HospitalDataBaseImplements/Migrations/HospitalDatabaseModelSnapshot.cs b/HospitalDataBaseImplements/Migrations/HospitalDatabaseModelSnapshot.cs index ed128c9..cf76154 100644 --- a/HospitalDataBaseImplements/Migrations/HospitalDatabaseModelSnapshot.cs +++ b/HospitalDataBaseImplements/Migrations/HospitalDatabaseModelSnapshot.cs @@ -169,6 +169,29 @@ namespace HospitalDataBaseImplements.Migrations b.ToTable("Medicines"); }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.ProcedureMedicine", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("MedicineId") + .HasColumnType("int"); + + b.Property("ProcedureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("MedicineId"); + + b.HasIndex("ProcedureId"); + + b.ToTable("ProcedureMedicine"); + }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.Procedures", b => { b.Property("Id") @@ -213,18 +236,18 @@ namespace HospitalDataBaseImplements.Migrations .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("MedicinesId") - .HasColumnType("int"); - b.Property("ModeOfApplication") .IsRequired() .HasColumnType("nvarchar(max)"); + b.Property("SymptomsId") + .HasColumnType("int"); + b.HasKey("Id"); b.HasIndex("ClientId"); - b.HasIndex("MedicinesId"); + b.HasIndex("SymptomsId"); b.ToTable("Recipes"); }); @@ -252,29 +275,6 @@ namespace HospitalDataBaseImplements.Migrations b.ToTable("RecipesProcedures"); }); - modelBuilder.Entity("HospitalDataBaseImplements.Models.RecipesSymptoms", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("RecipesId") - .HasColumnType("int"); - - b.Property("SymptomsId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("RecipesId"); - - b.HasIndex("SymptomsId"); - - b.ToTable("RecipesSymptoms"); - }); - modelBuilder.Entity("HospitalDataBaseImplements.Models.Symptoms", b => { b.Property("Id") @@ -355,6 +355,25 @@ namespace HospitalDataBaseImplements.Migrations b.Navigation("Client"); }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.ProcedureMedicine", b => + { + b.HasOne("HospitalDataBaseImplements.Models.Medicines", "Medicine") + .WithMany("Procedures") + .HasForeignKey("MedicineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HospitalDataBaseImplements.Models.Procedures", "Procedure") + .WithMany("Medicines") + .HasForeignKey("ProcedureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Medicine"); + + b.Navigation("Procedure"); + }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.Procedures", b => { b.HasOne("HospitalDataBaseImplements.Models.Client", "Client") @@ -374,15 +393,15 @@ namespace HospitalDataBaseImplements.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("HospitalDataBaseImplements.Models.Medicines", "Medicines") + b.HasOne("HospitalDataBaseImplements.Models.Symptoms", "Symptoms") .WithMany() - .HasForeignKey("MedicinesId") + .HasForeignKey("SymptomsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.Navigation("Client"); - b.Navigation("Medicines"); + b.Navigation("Symptoms"); }); modelBuilder.Entity("HospitalDataBaseImplements.Models.RecipesProcedures", b => @@ -404,25 +423,6 @@ namespace HospitalDataBaseImplements.Migrations b.Navigation("Recipe"); }); - modelBuilder.Entity("HospitalDataBaseImplements.Models.RecipesSymptoms", b => - { - b.HasOne("HospitalDataBaseImplements.Models.Recipes", "Recipe") - .WithMany() - .HasForeignKey("RecipesId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("HospitalDataBaseImplements.Models.Symptoms", "Symptoms") - .WithMany() - .HasForeignKey("SymptomsId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Recipe"); - - b.Navigation("Symptoms"); - }); - modelBuilder.Entity("HospitalDataBaseImplements.Models.Illness", b => { b.Navigation("Kurses"); @@ -435,6 +435,16 @@ namespace HospitalDataBaseImplements.Migrations b.Navigation("IllnessKurses"); }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b => + { + b.Navigation("Procedures"); + }); + + modelBuilder.Entity("HospitalDataBaseImplements.Models.Procedures", b => + { + b.Navigation("Medicines"); + }); + modelBuilder.Entity("HospitalDataBaseImplements.Models.Recipes", b => { b.Navigation("Procedures"); diff --git a/HospitalDataBaseImplements/Models/Medicines.cs b/HospitalDataBaseImplements/Models/Medicines.cs index 796a196..ce85c27 100644 --- a/HospitalDataBaseImplements/Models/Medicines.cs +++ b/HospitalDataBaseImplements/Models/Medicines.cs @@ -14,12 +14,15 @@ namespace HospitalDataBaseImplements.Models public class Medicines : IMedicinesModel { public int Id { get; private set; } + [Required] public int ClientId { get; private set; } - public virtual Client Client { get; set; } [Required] public string MedicinesName { get; private set; } = string.Empty; [Required] public string Group { get; private set; } = string.Empty; + public virtual Client Client { get; set; } + [ForeignKey("MedicineId")] + public virtual List Procedures { get; set; } = new(); public static Medicines? Create(MedicinesBindingModel model) { if (model == null) diff --git a/HospitalDataBaseImplements/Models/RecipesSymptoms.cs b/HospitalDataBaseImplements/Models/ProcedureMedicine.cs similarity index 53% rename from HospitalDataBaseImplements/Models/RecipesSymptoms.cs rename to HospitalDataBaseImplements/Models/ProcedureMedicine.cs index 5724437..6907684 100644 --- a/HospitalDataBaseImplements/Models/RecipesSymptoms.cs +++ b/HospitalDataBaseImplements/Models/ProcedureMedicine.cs @@ -7,14 +7,14 @@ using System.Threading.Tasks; namespace HospitalDataBaseImplements.Models { - public class RecipesSymptoms + public class ProcedureMedicine { public int Id { get; set; } [Required] - public int RecipesId { get; set; } + public int MedicineId { get; set; } [Required] - public int SymptomsId { get; set; } - public virtual Recipes Recipe { get; set; } = new(); - public virtual Symptoms Symptoms { get; set; } = new(); + public int ProcedureId { get; set; } + public virtual Medicines Medicine { get; set; } = new(); + public virtual Procedures Procedure { get; set; } = new(); } } diff --git a/HospitalDataBaseImplements/Models/Procedures.cs b/HospitalDataBaseImplements/Models/Procedures.cs index 2efeedb..9c8c96e 100644 --- a/HospitalDataBaseImplements/Models/Procedures.cs +++ b/HospitalDataBaseImplements/Models/Procedures.cs @@ -4,6 +4,7 @@ using HospitalDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -15,11 +16,29 @@ namespace HospitalDataBaseImplements.Models public int Id { get; private set; } [Required] public int ClientId { get; private set; } - public virtual Client Client { get; set; } [Required] public string ProceduresName { get; private set; } = string.Empty; [Required] public string Type { get; private set; } = string.Empty; + public virtual Client Client { get; set; } + private Dictionary? _procedureMedicine = null; + + [NotMapped] + public Dictionary ProcedureMedicine + { + get + { + if (_procedureMedicine == null) + { + _procedureMedicine = Medicines + .ToDictionary(rec => rec.MedicineId, rec => + rec.Medicine as IMedicinesModel); + } + return _procedureMedicine; + } + } + [ForeignKey("ProcedureId")] + public virtual List Medicines { get; set; } = new(); public static Procedures? Create(ProceduresBindingModel model) { if (model == null) @@ -60,5 +79,32 @@ namespace HospitalDataBaseImplements.Models ProceduresName = ProceduresName, Type = Type }; + + public void UpdateMedicines(HospitalDatabase context, ProceduresBindingModel model) + { + var procedureMedicine = context.ProcedureMedicine.Where(rec => rec.ProcedureId == model.Id).ToList(); + if (procedureMedicine != null && procedureMedicine.Count > 0) + { + context.ProcedureMedicine.RemoveRange(procedureMedicine.Where(rec + => !model.ProcedureMedicine.ContainsKey(rec.MedicineId))); + context.SaveChanges(); + } + var procedure = context.Procedures.First(x => x.Id == Id); + var existingMedicineIds = procedureMedicine?.Select(x => x.MedicineId).ToList(); + foreach (var rec in model.ProcedureMedicine) + { + + if (existingMedicineIds != null && !existingMedicineIds.Contains(rec.Key)) + { + context.ProcedureMedicine.Add(new ProcedureMedicine + { + Procedure = procedure, + Medicine = context.Medicines.First(x => x.Id == rec.Key), + }); + } + } + context.SaveChanges(); + _procedureMedicine = null; + } } } diff --git a/HospitalDataBaseImplements/Models/Recipes.cs b/HospitalDataBaseImplements/Models/Recipes.cs index 52ff30c..e4e4e6c 100644 --- a/HospitalDataBaseImplements/Models/Recipes.cs +++ b/HospitalDataBaseImplements/Models/Recipes.cs @@ -21,12 +21,13 @@ namespace HospitalDataBaseImplements.Models public string Dose { get; private set; } = string.Empty; [Required] public DateTime Date { get; private set; } = DateTime.Now; + [Required] public int ClientId { get; set; } - public virtual Medicines Medicines { get; set; } = new(); - public int MedicinesId { get; private set; } [Required] public string ModeOfApplication { get; private set; } = string.Empty; - public Client Client { get; set; } + [Required] + public int SymptomsId { get; private set; } + private Dictionary? _recipeProcedures = null; [NotMapped] public Dictionary RecipeProcedures @@ -43,59 +44,49 @@ namespace HospitalDataBaseImplements.Models } [ForeignKey("RecipesId")] public virtual List Procedures { get; set; } = new(); - - //private Dictionary? _recipeSymptoms = null; - //[NotMapped] - //public Dictionary RecipeSymptoms - //{ - // get - // { - // if (_recipeSymptoms == null) - // { - // _recipeSymptoms = Symptoms.ToDictionary(recPC => recPC.SymptomsId, recPC => (recPC.Symptoms as ISymptomsModel)); - // } - // return _recipeSymptoms; - // } - //} - //public virtual List Symptoms { get; set; } = new(); - + public Client Client { get; set; } + public Symptoms Symptoms { get; set; } public static Recipes? Create(RecipesBindingModel model) { + if (model == null) + { + return null; + } return new Recipes() { Id = model.Id, + SymptomsId = model.SymptomsId, Dose = model.Dose, - Date = model.Date, - ModeOfApplication = model.ModeOfApplication, - ClientId = model.ClientId - //Symptoms = model.RecipeSymptoms.Select(x => new RecipesSymptoms - //{ - // Symptoms = context.Symptomses.First(y => y.Id == x.Key), - //}).ToList() + ClientId = model.ClientId, + ModeOfApplication = model.ModeOfApplication }; } + public static Recipes Create(RecipesViewModel model) + { + return new Recipes() + { + Id = model.Id, + SymptomsId = model.SymptomsId, + Dose = model.Dose, + ClientId = model.ClientId, + ModeOfApplication = model.ModeOfApplication + }; + } public void Update(RecipesBindingModel model) { Date = model.Date; - MedicinesId = model.MedicinesId; + Dose = model.Dose; + ModeOfApplication = model.ModeOfApplication; } - public RecipesViewModel GetViewModel + public RecipesViewModel GetViewModel => new() { - get - { - using var context = new HospitalDatabase(); - return new RecipesViewModel - { - Id = Id, - Date = Date, - ClientId = ClientId, - ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, - MedicinesId = MedicinesId, - RecipeProcedures = RecipeProcedures, - //RecipeSymptoms = RecipeSymptoms - }; - } - } + Id = Id, + Date = Date, + ClientId = ClientId, + ModeOfApplication = ModeOfApplication, + Dose = Dose, + SymptomsId = SymptomsId + }; public void UpdateProcedures(HospitalDatabase context, RecipesBindingModel model) { var recipeProcedures = context.RecipesProcedures.Where(rec => rec.RecipesId == model.Id).ToList(); @@ -117,26 +108,5 @@ namespace HospitalDataBaseImplements.Models } _recipeProcedures = null; } - //public void UpdateSymptomses(HospitalDatabase context, RecipesBindingModel model) - //{ - // var recipeSymptomses = context.RecipesSymptoms.Where(rec => rec.RecipesId == model.Id).ToList(); - // if (recipeSymptomses != null && recipeSymptomses.Count > 0) - // { // удалили те, которых нет в модели - // context.RecipesSymptoms.RemoveRange(recipeSymptomses.Where(rec - // => !model.RecipeSymptoms.ContainsKey(rec.SymptomsId))); - // context.SaveChanges(); - // } - // var recipe = context.Recipes.First(x => x.Id == Id); - // foreach (var pc in model.RecipeSymptoms) - // { - // context.RecipesSymptoms.Add(new RecipesSymptoms - // { - // Recipe = recipe, - // Symptoms = context.Symptomses.First(x => x.Id == pc.Key) - // }); - // context.SaveChanges(); - // } - // _recipeSymptoms = null; - //} } } diff --git a/HospitalDataBaseImplements/Models/Symptoms.cs b/HospitalDataBaseImplements/Models/Symptoms.cs index bcc305b..7b5c6a8 100644 --- a/HospitalDataBaseImplements/Models/Symptoms.cs +++ b/HospitalDataBaseImplements/Models/Symptoms.cs @@ -8,6 +8,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace HospitalDataBaseImplements.Models { @@ -40,6 +41,18 @@ namespace HospitalDataBaseImplements.Models Description = model.Description }; } + public static Symptoms? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Symptoms() + { + SymptomName = element.Element("SymptomName")!.Value, + Description = element.Element("Description")!.Value + }; + } public void Update(SymptomsBindingModel model) { if (model == null) diff --git a/HospitalDataBaseImplements/XMLData/Illness.xml b/HospitalDataBaseImplements/XMLData/Illness.xml deleted file mode 100644 index 99eb8cf..0000000 --- a/HospitalDataBaseImplements/XMLData/Illness.xml +++ /dev/null @@ -1,91 +0,0 @@ - - - - Ветрянка -
Тяжелая
- - 1 - 2 - 3 - - - 1 - 2 - -
- - Воспаление аппендицита -
Легкая
- - 1 - - - 1 - -
- - ОРВИ -
Средняя
- - 1 - 2 - - - 1 - 3 - -
- - Отравление -
Тяжелая
- - 1 - 2 - 3 - 4 - 5 - 6 - - - 1 - 2 - 3 - 4 - -
- - Перелом -
Средняя
- - 3 - - - 1 - 2 - -
- - Мигрень -
Тяжелая
- - 1 - 3 - 4 - - - 1 - 3 - 4 - -
- - Астма -
Легкая
- - 5 - - - 5 - -
-
\ No newline at end of file diff --git a/HospitalDataBaseImplements/XMLData/Kurses.xml b/HospitalDataBaseImplements/XMLData/Kurses.xml deleted file mode 100644 index 5265e5b..0000000 --- a/HospitalDataBaseImplements/XMLData/Kurses.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - 4 - 1 месяц - 1 - - - 3 - 1 месяц - 1 - - - 1 - 2 месяца - 1 - - - 7 - 1 неделя - 1 - - - 2 - 10 дней - 1 - - - 5 - 3 недели - 1 - - - 3 - 20 дней - 1 - - - 2 - 1,5 недели - 1 - - \ No newline at end of file diff --git a/HospitalDataBaseImplements/XMLData/Symptoms.xml b/HospitalDataBaseImplements/XMLData/Symptoms.xml deleted file mode 100644 index c50f516..0000000 --- a/HospitalDataBaseImplements/XMLData/Symptoms.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - Кашель - Сухой - - - Отечность - Ноги - - - Заложенность носа - 2 ноздри - - - Тошнота - Прозрачные выделения - - - Боль в сердце - Постоянная - - - Боль в животе - Умеренная - - - Боль - Тяжелая - - - Головокружение - При нагрузках - - diff --git a/HospitalDataModels/Models/IIllnessModel.cs b/HospitalDataModels/Models/IIllnessModel.cs index 01adadc..7bcb7c3 100644 --- a/HospitalDataModels/Models/IIllnessModel.cs +++ b/HospitalDataModels/Models/IIllnessModel.cs @@ -12,6 +12,5 @@ namespace HospitalDataModels.Models string Form { get; } Dictionary IllnessSymptoms { get; } Dictionary IllnessKurse { get; } - //int ClientId { get; } } } diff --git a/HospitalDataModels/Models/IKurseModel.cs b/HospitalDataModels/Models/IKurseModel.cs index 238987e..b5f6899 100644 --- a/HospitalDataModels/Models/IKurseModel.cs +++ b/HospitalDataModels/Models/IKurseModel.cs @@ -12,6 +12,5 @@ namespace HospitalDataModels.Models int CountInDay { get; } int MedicinesId { get; } string MedicinesName { get; } - //int ClientId { get; } } } diff --git a/HospitalDataModels/Models/IProceduresModel.cs b/HospitalDataModels/Models/IProceduresModel.cs index 38860ff..f4a8230 100644 --- a/HospitalDataModels/Models/IProceduresModel.cs +++ b/HospitalDataModels/Models/IProceduresModel.cs @@ -11,5 +11,6 @@ namespace HospitalDataModels.Models string ProceduresName { get; } string Type { get; } int ClientId { get; } + Dictionary ProcedureMedicine { get; } } } diff --git a/HospitalDataModels/Models/IRecipesModel.cs b/HospitalDataModels/Models/IRecipesModel.cs index 2075517..9666d1c 100644 --- a/HospitalDataModels/Models/IRecipesModel.cs +++ b/HospitalDataModels/Models/IRecipesModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace HospitalDataModels.Models @@ -11,9 +12,9 @@ namespace HospitalDataModels.Models string Dose { get; } DateTime Date { get; } string ModeOfApplication { get; } - int MedicinesId { get; } int ClientId { get; } + int SymptomsId { get; } Dictionary RecipeProcedures { get; } - //Dictionary RecipeSymptoms { get; } + } } diff --git a/HospitalDataModels/Models/ISymptomsModel.cs b/HospitalDataModels/Models/ISymptomsModel.cs index 86a7675..cc88570 100644 --- a/HospitalDataModels/Models/ISymptomsModel.cs +++ b/HospitalDataModels/Models/ISymptomsModel.cs @@ -9,7 +9,6 @@ namespace HospitalDataModels.Models public interface ISymptomsModel: IId { string SymptomName { get; } - string Description { get; } - //int ClientId { get; } + string? Description { get; } } }