Добавлено связывание рецептов и лечений

This commit is contained in:
prodigygirl 2023-05-19 10:00:13 +04:00
parent 675971d32f
commit a7b969b191
4 changed files with 105 additions and 3 deletions

View File

@ -1,4 +1,5 @@
using HospitalDataModels.Models;
using Newtonsoft.Json;
using System.ComponentModel;
namespace HospitalContracts.ViewModels
@ -10,5 +11,13 @@ namespace HospitalContracts.ViewModels
public string Name { get; set; } = string.Empty;
public Dictionary<int, IProcedureModel> TreatmentProcedures { get; set; } = new();
public TreatmentViewModel() { }
[JsonConstructor]
public TreatmentViewModel(Dictionary<int, ProcedureViewModel> TreatmentProcedures)
{
this.TreatmentProcedures = TreatmentProcedures.ToDictionary(x => x.Key, x => x.Value as IProcedureModel);
}
}
}

View File

@ -0,0 +1,34 @@
using HospitalContracts.BusinessLogicContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace HospitalRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class TreatmentController : ControllerBase
{
private readonly ITreatmentLogic _logic;
public TreatmentController(ITreatmentLogic logic)
{
_logic = logic;
}
[HttpGet]
public List<TreatmentViewModel>? GetTreatments()
{
try
{
return _logic.ReadList(null);
}
catch (Exception ex)
{
throw;
}
}
}
}

View File

@ -32,6 +32,7 @@ namespace HospitalWeb.Controllers
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = APIClient.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicines");
ViewBag.Treatments = APIClient.GetRequest<List<TreatmentViewModel>>($"api/treatment/gettreatments");
if (!id.HasValue)
{
return View(new RecipeViewModel());

View File

@ -31,7 +31,14 @@
</div>
</div>
<div class="row">
<table id="medicinesTable">
<div class="col-4">Добавление лекарств</div>
<div class="col-8">
<select id="treatments" name="treatments" class="form-control" asp-items="@(new SelectList(@ViewBag.Treatments,"Id", "Name"))"></select>
<button type="button" onclick="addTreatment()">Добавить лечение</button>
</div>
</div>
<div class="row">
<table id="medicinesTable" class="table">
<thead>
<tr>
<th>Название</th>
@ -47,6 +54,27 @@
<button type="button" class="btn btn-danger" data-id="@medicine.Key" onclick="removeMedicine('@medicine.Key')">Удалить</button>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="row">
<table id="treatmentsTable" class="table">
<thead>
<tr>
<th>Название</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var treatment in Model.RecipeTreatments)
{
<tr>
<td>@treatment.Value.Name</td>
<td>
<button type="button" class="btn btn-danger" data-id="@treatment.Key" onclick="removeTreatment('@treatment.Key')">Удалить</button>
</td>
</tr>
}
</tbody>
</table>
@ -58,9 +86,13 @@
</div>
</div>
@foreach (var medicine in Model.RecipeMedicines.Keys)
{
{
<input type="hidden" name="RecipeMedicines[@medicine]" value="@medicine"/>
}
}
@foreach (var treatment in Model.RecipeTreatments.Keys)
{
<input type="hidden" name="RecipeTreatments[@treatment]" value="@treatment"/>
}
</form>
}
@section scripts {
@ -91,6 +123,32 @@
$('#medicinesTable button[data-id="' + medicineId + '"]').closest('tr').remove();
$('#recipe-form input[name="RecipeMedicines[' + medicineId + ']"]').remove();
}
var recipeTreatments = @Json.Serialize(Model.RecipeTreatments);
function addTreatment() {
var treatmentId = $('#treatments').val();
var treatmentName = $('#treatments option:selected').text();
if (recipeTreatments.hasOwnProperty(treatmentId)) {
alert('This treatment is already added.');
return;
}
recipeTreatments[treatmentId] = { Id: treatmentId, Name: treatmentName };
var row = $('<tr>').append($('<td>').text(treatmentName));
var removeButton = $('<button>').text('Удалить').attr('data-id', treatmentId).attr('class', 'btn btn-danger').click((function(id) {
return function() {
removeTreatment(id);
};
})(treatmentId));
row.append($('<td>').append(removeButton));
$('#treatmentsTable tbody').append(row);
var input = $('<input>').attr('type', 'hidden').attr('name', 'RecipeTreatments[' + treatmentId + ']').val(treatmentId);
$('#recipe-form').append(input);
}
function removeTreatment(treatmentId) {
delete recipeTreatments[treatmentId];
$('#treatmentsTable button[data-id="' + treatmentId + '"]').closest('tr').remove();
$('#recipe-form input[name="RecipeTreatments[' + treatmentId + ']"]').remove();
}
</script>
}