Работает круд для симптомов

This commit is contained in:
Никита Потапов 2024-05-28 13:55:03 +04:00
parent f6c52a08ad
commit 7ed2d70455
9 changed files with 84 additions and 70 deletions

View File

@ -73,7 +73,7 @@ namespace PolyclinicBusinessLogic.BusinessLogics
public bool Update(SymptomBindingModel model) public bool Update(SymptomBindingModel model)
{ {
CheckModel(model); CheckModel(model, false);
if (_symptomStorage.Update(model) == null) if (_symptomStorage.Update(model) == null)
{ {
_logger.LogWarning("Update operation failed"); _logger.LogWarning("Update operation failed");

View File

@ -31,12 +31,9 @@ namespace PolyclinicDatabaseImplement.Implements
public List<CourseViewModel> GetFilteredList(CourseSearchModel model) public List<CourseViewModel> GetFilteredList(CourseSearchModel model)
{ {
var elements = GetFullList(); var elements = GetFullList();
foreach (var prop in model.GetType().GetProperties()) if (model.Id != null)
{ {
if (model.GetType().GetProperty(prop.Name)?.GetValue(model, null) != null) elements = elements.Where(x => x.Id == model.Id).ToList();
{
elements = elements.Where(x => x.GetType().GetProperty(prop.Name)?.GetValue(x, null) == model.GetType().GetProperty(prop.Name)?.GetValue(model, null)).ToList();
}
} }
return elements; return elements;
} }

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using PolyclinicContracts.BindingModels; using PolyclinicContracts.BindingModels;
using PolyclinicContracts.SearchModels; using PolyclinicContracts.SearchModels;
using PolyclinicContracts.StoragesContracts; using PolyclinicContracts.StoragesContracts;
@ -31,12 +32,13 @@ namespace PolyclinicDatabaseImplement.Implements
public List<SymptomViewModel> GetFilteredList(SymptomSearchModel model) public List<SymptomViewModel> GetFilteredList(SymptomSearchModel model)
{ {
var elements = GetFullList(); var elements = GetFullList();
foreach (var prop in model.GetType().GetProperties()) if (model.Id != null)
{ {
if (model.GetType().GetProperty(prop.Name)?.GetValue(model, null) != null) elements = elements.Where(x => x.Id == model.Id).ToList();
{
elements = elements.Where(x => x.GetType().GetProperty(prop.Name)?.GetValue(x, null) == model.GetType().GetProperty(prop.Name)?.GetValue(model, null)).ToList();
} }
if (!model.Name.IsNullOrEmpty())
{
elements = elements.Where(x => x.Name == model.Name).ToList();
} }
return elements; return elements;
} }
@ -67,6 +69,9 @@ namespace PolyclinicDatabaseImplement.Implements
public SymptomViewModel? Update(SymptomBindingModel model) public SymptomViewModel? Update(SymptomBindingModel model)
{ {
using var context = new PolyclinicDatabase(); using var context = new PolyclinicDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var element = context.Symptomes.FirstOrDefault(x => x.Id == model.Id); var element = context.Symptomes.FirstOrDefault(x => x.Id == model.Id);
if (element == null) if (element == null)
{ {
@ -74,7 +79,16 @@ namespace PolyclinicDatabaseImplement.Implements
} }
element.Update(model); element.Update(model);
context.SaveChanges(); context.SaveChanges();
element.UpdateDiagnoses(context, model);
transaction.Commit();
return element.GetViewModel; return element.GetViewModel;
} }
catch (Exception ex)
{
transaction.Rollback();
throw;
}
}
} }
} }

View File

@ -63,19 +63,18 @@ namespace PolyclinicDatabaseImplement.Models
public void UpdateDiagnoses(PolyclinicDatabase context, SymptomBindingModel model) public void UpdateDiagnoses(PolyclinicDatabase context, SymptomBindingModel model)
{ {
var symptomDiagnoses = context.SymptomDiagnoses.Where(rec => rec.SymptomId == model.Id).ToList(); var symptomDiagnoses = context.SymptomDiagnoses.Where(rec => rec.SymptomId == model.Id).ToList();
if (symptomDiagnoses != null && symptomDiagnoses.Count > 0) if (symptomDiagnoses != null && symptomDiagnoses.Count > 0)
{ {
// удалили те, которых нет в модели context.SymptomDiagnoses.RemoveRange(symptomDiagnoses);
context.SymptomDiagnoses.RemoveRange(symptomDiagnoses
.Where(rec => !model.SymptomDiagnoses.ContainsKey(rec.DiagnoseId)));
context.SaveChanges(); context.SaveChanges();
} }
var course = context.Symptomes.First(x => x.Id == Id); var symptom = context.Symptomes.First(x => x.Id == model.Id);
foreach (var pc in model.SymptomDiagnoses) foreach (var pc in model.SymptomDiagnoses)
{ {
context.SymptomDiagnoses.Add(new SymptomDiagnose context.SymptomDiagnoses.Add(new SymptomDiagnose
{ {
Symptom = course, Symptom = symptom,
Diagnose = context.Diagnoses.First(x => x.Id == pc.Key), Diagnose = context.Diagnoses.First(x => x.Id == pc.Key),
}); });
context.SaveChanges(); context.SaveChanges();

View File

@ -3,6 +3,7 @@ using PolyclinicContracts.BindingModels;
using PolyclinicContracts.BusinessLogicsContracts; using PolyclinicContracts.BusinessLogicsContracts;
using PolyclinicContracts.SearchModels; using PolyclinicContracts.SearchModels;
using PolyclinicContracts.ViewModels; using PolyclinicContracts.ViewModels;
using PolyclinicDataModels.Models;
using PolyclinicWebAppImplementer.Models; using PolyclinicWebAppImplementer.Models;
namespace PolyclinicWebAppImplementer.Controllers namespace PolyclinicWebAppImplementer.Controllers
@ -25,21 +26,29 @@ namespace PolyclinicWebAppImplementer.Controllers
} }
[HttpGet] [HttpGet]
[HttpPost] [HttpPost]
public IActionResult Add(SymptomFormModel model) public IActionResult Add(SymptomFormModel model, int[] selectedDiagnoses)
{ {
if (HttpContext.Request.Method == "GET") if (HttpContext.Request.Method == "GET")
{ {
ViewData["Title"] = "Новый симптом"; ViewData["Title"] = "Новый симптом";
model = new(); model = new()
model.AvailableDiagnoses = _diagnoseLogic.ReadList(); {
Diagnoses = _diagnoseLogic.ReadList().Select(x => (x, false)).ToList()
};
return View("SymptomForm", model); return View("SymptomForm", model);
} }
else else
{ {
var allDiagnoses = _diagnoseLogic.ReadList();
SymptomBindingModel symptom = new SymptomBindingModel SymptomBindingModel symptom = new SymptomBindingModel
{ {
Name = model.SymptomViewModel.Name, Name = model.SymptomViewModel.Name,
Comment = model.SymptomViewModel.Comment, Comment = model.SymptomViewModel.Comment,
SymptomDiagnoses = selectedDiagnoses
.ToDictionary(
x => x,
x => allDiagnoses.Where(y => y.Id == x) as IDiagnoseModel
)
}; };
_symptomLogic.Create(symptom); _symptomLogic.Create(symptom);
return RedirectToAction("Index"); return RedirectToAction("Index");
@ -47,21 +56,32 @@ namespace PolyclinicWebAppImplementer.Controllers
} }
[HttpGet] [HttpGet]
[HttpPost] [HttpPost]
public IActionResult Edit(int id, SymptomViewModel model) public IActionResult Edit(int id, SymptomFormModel model, int[] selectedDiagnoses)
{ {
if (HttpContext.Request.Method == "GET") if (HttpContext.Request.Method == "GET")
{ {
var obj = _symptomLogic.ReadElement(new SymptomSearchModel { Id = id }); var obj = _symptomLogic.ReadElement(new SymptomSearchModel { Id = id });
model = new()
{
SymptomViewModel = obj,
Diagnoses = _diagnoseLogic.ReadList().Select(x => (x, obj.SymptomDiagnoses.ContainsKey(x.Id))).ToList()
};
ViewData["Title"] = "Редактировать симптом"; ViewData["Title"] = "Редактировать симптом";
return View("SymptomForm", obj); return View("SymptomForm", model);
} }
else else
{ {
var allDiagnoses = _diagnoseLogic.ReadList();
SymptomBindingModel symptom = new SymptomBindingModel SymptomBindingModel symptom = new SymptomBindingModel
{ {
Id = model.Id, Id = id,
Name = model.Name, Name = model.SymptomViewModel.Name,
Comment = model.Comment, Comment = model.SymptomViewModel.Comment,
SymptomDiagnoses = selectedDiagnoses
.ToDictionary(
x => x,
x => allDiagnoses.Where(y => y.Id == x) as IDiagnoseModel
)
}; };
_symptomLogic.Update(symptom); _symptomLogic.Update(symptom);
return RedirectToAction("Index"); return RedirectToAction("Index");

View File

@ -5,7 +5,6 @@ namespace PolyclinicWebAppImplementer.Models
public class SymptomFormModel public class SymptomFormModel
{ {
public SymptomViewModel? SymptomViewModel { get; set; } public SymptomViewModel? SymptomViewModel { get; set; }
public List<DiagnoseViewModel> SelectecDiagnoses { get; set; } = new(); public List<(DiagnoseViewModel Diagnose, bool IsChecked)> Diagnoses { get; set; } = new();
public List<DiagnoseViewModel> AvailableDiagnoses { get; set; } = new();
} }
} }

View File

@ -4,20 +4,20 @@
<form class="d-flex flex-column" method="post"> <form class="d-flex flex-column" method="post">
<input type="hidden" readonly asp-for="Id" /> <input type="hidden" readonly asp-for="Id" />
<div class="row mb-5"> <div class="row mb-5">
<div class="col-3">Название:</div> <div class="col">Название:</div>
<div class="col-8"><input required asp-for="Name" /></div> <div class="col"><input required asp-for="Name" /></div>
</div> </div>
<div class="row mb-5"> <div class="row mb-5">
<div class="col-3">Коментарий:</div> <div class="col">Коментарий:</div>
<div class="col-8"><textarea asp-for="Comment"></textarea></div> <div class="col"><textarea asp-for="Comment"></textarea></div>
</div> </div>
<div class="row mb-5"> <div class="row mb-5">
<div class="col-3">Начало:</div> <div class="col">Начало:</div>
<div class="col-8"><input required type="date" asp-for="DateStartDiagnose" /></div> <div class="col"><input required type="date" asp-for="DateStartDiagnose" /></div>
</div> </div>
<div class="row mb-5"> <div class="row mb-5">
<div class="col-3">Конец:</div> <div class="col">Конец:</div>
<div class="col-8"><input type="date" asp-for="DateStopDiagnose" /></div> <div class="col"><input type="date" asp-for="DateStopDiagnose" /></div>
</div> </div>
<div class="row mb-5"> <div class="row mb-5">
<div class="col-4"> <div class="col-4">

View File

@ -4,42 +4,27 @@
<form class="d-flex flex-column" method="post"> <form class="d-flex flex-column" method="post">
<div class="row mb-5"> <div class="row mb-5">
<div class="col-3">Название:</div> <div class="col-3">Название:</div>
<div class="col-8"><input type="text" asp-for="SymptomViewModel.Name" /></div> <div class="col-8"><input required type="text" asp-for="SymptomViewModel.Name" /></div>
</div> </div>
<div class="row mb-5"> <div class="row mb-5">
<div class="col-3">Коментарий:</div> <div class="col-3">Коментарий:</div>
<div class="col-8"><textarea asp-for="SymptomViewModel.Comment"></textarea></div> <div class="col-8"><textarea asp-for="SymptomViewModel.Comment"></textarea></div>
</div> </div>
<div class="row mb-5">
<div class="col-3 d-flex align-content-center">
<h5 class="me-2">Болезни</h5>
<select id="diagnoseId" name="diagnoseId" class="me-2">
<option value="">Выберите болезнь</option>
@foreach (var availableDiagnose in Model.AvailableDiagnoses)
{
<option value="@availableDiagnose.Id">@availableDiagnose.Name</option>
}
</select>
<button class="btn btn-success" type="button">
Добавить
</button>
</div>
</div>
<div class="row mb-5 overflow-auto" style="max-height: 100px; max-width: 500px;"> <div class="row mb-5 overflow-auto" style="max-height: 100px; max-width: 500px;">
<ol> <ol>
@{ @foreach (var item in Model.Diagnoses)
int count = 7;
for (int i = 0; i < count; i++)
{ {
<li class="mb-2 ps-1 ms-1"> <li class="mb-2 ps-1 ms-1">
<a asp-action="Symptom" class="text-decoration-none"> @if (item.IsChecked)
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash-fill text-danger " viewBox="0 0 16 16"> {
<path d="M2.5 1a1 1 0 0 0-1 1v1a1 1 0 0 0 1 1H3v9a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V4h.5a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H10a1 1 0 0 0-1-1H7a1 1 0 0 0-1 1zm3 4a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 .5-.5M8 5a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7A.5.5 0 0 1 8 5m3 .5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 1 0" /> <input type="checkbox" id="diagnose-@item.Diagnose.Id" name="selectedDiagnoses" value="@item.Diagnose.Id" checked />
</svg>
</a>
<span>Выбранная болезнь</span>
</li>
} }
else
{
<input type="checkbox" id="diagnose-@item.Diagnose.Id" name="selectedDiagnoses" value="@item.Diagnose.Id" />
}
<label for="diagnose-@item.Diagnose.Id">@item.Diagnose.Name</label>
</li>
} }
</ol> </ol>
</div> </div>