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

This commit is contained in:
Никита Потапов 2024-05-29 00:35:18 +04:00
parent 9d840a764e
commit 3b067247ad
10 changed files with 225 additions and 120 deletions

View File

@ -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;
}
}
}

View File

@ -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);

View File

@ -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<CourseViewModel> 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");
}
}
}

View File

@ -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();
}
}

View File

@ -10,7 +10,7 @@
<Content Update="log4net.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Views\Home\Course.cshtml">
<Content Update="Views\Courses\CourseForm.cshtml">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>

View File

@ -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", "Политика приватности");

View File

@ -0,0 +1,42 @@
@model CourseFormModel
<h4>@ViewData["Title"]</h4>
<form class="d-flex flex-column" method="post">
<div class="row mb-5">
<div class="col-3">Количество дней:</div>
<div class="col-8"><input required asp-for="CourseViewModel.DaysCount" /></div>
</div>
<div class="row mb-5">
<div class="col-3">Количество препарата в день:</div>
<div class="col-8"><input required asp-for="CourseViewModel.PillsPerDay" /></div>
</div>
<div class="row mb-5">
<div class="col-3">Коментарий:</div>
<div class="col-8"><textarea asp-for="CourseViewModel.Comment"></textarea></div>
</div>
<div class="row mb-5 overflow-auto" style="max-height: 100px; max-width: 500px;">
<ol>
@foreach (var item in Model.Diagnoses)
{
<li class="mb-2 ps-1 ms-1">
@if (item.IsChecked)
{
<input type="checkbox" id="diagnose-@item.Diagnose.Id" name="selectedDiagnoses" value="@item.Diagnose.Id" checked />
}
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>
</div>
<div class="row mb-5">
<div class="col-4">
<button class="btn btn-success" type="submit">
Сохранить
</button>
@Html.ActionLink("Отмена", "", "Courses", null, new { @class = "btn btn-danger" })
</div>
</div>
</form>

View File

@ -0,0 +1,48 @@
@using PolyclinicContracts.ViewModels
@model List<CourseViewModel>
@{
ViewBag.SelectedSiteMenuItem = SiteMenuItems.Courses;
}
<div>
<div class="d-flex flex-row">
<a class="btn btn-primary" asp-action="Add" title="Добавить">
Добавить курс
</a>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">Номер</th>
<th scope="col">Количество дней</th>
<th scope="col">Количество препаратов в день</th>
<th scope="col">Комментарий</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<th scope="row">@item.Id</th>
<td>@item.DaysCount</td>
<td>@item.PillsPerDay</td>
<td>@item.Comment</td>
<td class="d-flex">
<form method="post" asp-action="Delete" asp-route-id="@item.Id">
<button class="btn btn-danger me-1" title="Удалить" type="submit">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash-fill" 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" />
</svg>
</button>
</form>
<a class="btn btn-warning text-light" title="Редактировать" asp-action="Edit" asp-route-id="@item.Id">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-fill" viewBox="0 0 16 16">
<path d="M12.854.146a.5.5 0 0 0-.707 0L10.5 1.793 14.207 5.5l1.647-1.646a.5.5 0 0 0 0-.708zm.646 6.061L9.793 2.5 3.293 9H3.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.207zm-7.468 7.468A.5.5 0 0 1 6 13.5V13h-.5a.5.5 0 0 1-.5-.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.5-.5V10h-.5a.5.5 0 0 1-.175-.032l-.179.178a.5.5 0 0 0-.11.168l-2 5a.5.5 0 0 0 .65.65l5-2a.5.5 0 0 0 .168-.11z" />
</svg>
</a>
</td>
</tr>
}
</tbody>
</table>
</div>

View File

@ -1,60 +0,0 @@
@{
ViewBag.SelectedSiteMenuItem = SiteMenuItems.Course;
}
<h4>Новый курс лечения</h4>
<form class="d-flex flex-column" method="post">
<div class="row mb-5">
<div class="col-3">Количество дней:</div>
<div class="col-8"><input type="number" id="daysCount" name="daysCount" /></div>
</div>
<div class="row mb-5">
<div class="col-3">Количество препарата в день:</div>
<div class="col-8"><input type="number" id="pillsPerDay" name="pillsPerDay" /></div>
</div>
<div class="row mb-5">
<div class="col-3">Коментарий:</div>
<div class="col-8"><textarea id="comment" name="comment"></textarea></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>
@{
for (int i = 0; i < 10; i++)
{
<option value="@i">Какая-то противная болезнь @i</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;">
<ol>
@{
for (int i = 0; i < 7; i++)
{
<li class="mb-2 ps-1 ms-1">
<a asp-action="Course" class="text-decoration-none">
<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" />
</svg>
</a>
<span>Выбранная болезнь</span>
</li>
}
}
</ol>
</div>
<div class="row mb-5">
<div class="col-4">
<button class="btn btn-success" type="submit">
Сохранить
</button>
@Html.ActionLink("Отмена", "Courses", "Home", null, new { @class = "btn btn-danger" })
</div>
</div>
</form>

View File

@ -1,47 +0,0 @@
@{
ViewBag.SelectedSiteMenuItem = SiteMenuItems.Courses;
}
<div>
<div class="d-flex flex-row">
<a class="btn btn-primary" asp-action="Course" asp-controller="Home" title="Добавить">
Добавить курс
</a>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">Номер</th>
<th scope="col">Количество дней</th>
<th scope="col">Количество препаратов в день</th>
<th scope="col">Комментарий</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@{
int count = 20;
for (int i = 0; i < count; i++)
{
<tr>
<th scope="row">@i</th>
<td>12</td>
<td>3</td>
<td>Очень хороший курс приема</td>
<td class="d-flex">
<a class="btn btn-danger me-1" title="Удалить">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash-fill" 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" />
</svg>
</a>
<a class="btn btn-warning text-light" title="Редактировать" asp-action="Course" asp-controller="Home">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-fill" viewBox="0 0 16 16">
<path d="M12.854.146a.5.5 0 0 0-.707 0L10.5 1.793 14.207 5.5l1.647-1.646a.5.5 0 0 0 0-.708zm.646 6.061L9.793 2.5 3.293 9H3.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.207zm-7.468 7.468A.5.5 0 0 1 6 13.5V13h-.5a.5.5 0 0 1-.5-.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.5-.5V10h-.5a.5.5 0 0 1-.175-.032l-.179.178a.5.5 0 0 0-.11.168l-2 5a.5.5 0 0 0 .65.65l5-2a.5.5 0 0 0 .168-.11z" />
</svg>
</a>
</td>
</tr>
}
}
</tbody>
</table>
</div>