поручитель сделал формы для лекарств. осталось только сервисы и пересобрать миграцию. ну и отдебажить

This commit is contained in:
antoc0der 2024-04-29 20:07:18 +04:00
parent 39a8d5a564
commit c7c55114e8
6 changed files with 316 additions and 7 deletions

View File

@ -3,7 +3,10 @@ using System.Diagnostics;
using VeterinaryShowDoctorApp.Models;
using VeterinaryContracts.ViewModels;
using VeterinaryContracts.BindingModels;
using System.Text;
using VeterinaryDataModels;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.StorageContracts;
namespace VeterinaryShowDoctorApp.Controllers
{
@ -190,36 +193,147 @@ namespace VeterinaryShowDoctorApp.Controllers
Response.Redirect("Index");
}
public IActionResult Drugs()
{
if (APIDoctor.Doctor == null)
{
return Redirect("~/Home/Enter");
}
return View(APIDoctor.GetRequest<List<DrugViewModel>>($"api/drug/getdrugs?doctorid={APIDoctor.Doctor.Id}"));
}
public IActionResult CreateDrug()
{
if (APIDoctor.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medications = APIDoctor.GetRequest<List<MedicationViewModel>>($"api/medication/getmedications");
return View();
}
[HttpPost]
public void CreateDrug(string name, int price)
public void CreateDrug(string name, List<int> medications, int count)
{
if (APIDoctor.Doctor == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(name) || price <= 0)
double _price=0;
if (string.IsNullOrEmpty(name) || count <=0)
{
throw new Exception("Ошибка в введенных данных");
}
APIDoctor.PostRequest("api/medication/createmedication", new MedicationBindingModel
Dictionary<int, IMedicationModel> a = new Dictionary<int, IMedicationModel>();
foreach (int medication in medications)
{
MedicationName = name,
Price = price,
DoctorId = APIDoctor.Doctor.Id
a.Add(medication, new MedicationSearchModel { Id = medication } as IMedicationModel);
}
foreach (var elem in a)
{
_price += elem.Value.Price;
}
APIDoctor.PostRequest("api/drug/createdrug", new DrugBindingModel
{
DrugName = name,
Price = Math.Round(_price, 2),
DrugMedications = a,
Count = count
});
Response.Redirect("Index");
}
public IActionResult DeleteDrug()
{
if (APIDoctor.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Drugs = APIDoctor.GetRequest<List<DrugViewModel>>($"api/drug/getdrugs?doctorid={APIDoctor.Doctor.Id}");
return View();
}
[HttpPost]
public void DeleteDrug(int drug)
{
if (APIDoctor.Doctor == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIDoctor.PostRequest("api/drug/deletedrug", new DrugBindingModel
{
Id = drug
});
Response.Redirect("Index");
}
public IActionResult UpdateDrug()
{
if (APIDoctor.Doctor == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Drugs = APIDoctor.GetRequest<List<DrugViewModel>>($"api/drug/getdrugs?doctorid={APIDoctor.Doctor.Id}");
ViewBag.Medications = APIDoctor.GetRequest<List<MedicationViewModel>>($"api/medication/getmedications");
return View();
}
[HttpPost]
public void UpdateDrug(int drug, string name, List<int> medications, int count)
{
if (APIDoctor.Doctor == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
double _price=0;
if (string.IsNullOrEmpty(name) || count <= 0)
{
throw new Exception("Ошибка в введенных данных");
}
Dictionary<int, IMedicationModel> a = new Dictionary<int, IMedicationModel>();
foreach (int medication in medications)
{
a.Add(medication, new MedicationSearchModel { Id = medication } as IMedicationModel);
}
foreach (var elem in a)
{
_price += elem.Value.Price;
}
APIDoctor.PostRequest("api/drug/updatedrug", new DrugBindingModel
{
Id = drug,
DrugName = name,
Price = Math.Round(_price, 2),
DrugMedications = a,
Count = count
});
Response.Redirect("Index");
}
[HttpGet]
public Tuple<DrugViewModel, List<string>>? GetDrug(int drugId)
{
if (APIDoctor.Doctor == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
var result = APIDoctor.GetRequest<Tuple<DrugViewModel, List<string>>>($"api/drug/getdrug?drugid={drugId}");
if (result == null)
{
return default;
}
return result;
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()

View File

@ -0,0 +1,44 @@
@{
ViewData["Title"] = "CreateDrug";
}
<div class="text-center">
<h2 class="display-4">Создание лекарства</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Название:</div>
<div class="col-8">
<input type="text" name="name" id="name" />
</div>
</div>
<div class="row">
<div class="col-4">Цена:</div>
<div class="col-8">
<input type="text" name="price" id="price" />
</div>
</div>
<div class="row">
<div class="col-4">Количество:</div>
<div class="col-8">
<input type="number" name="count" id="count" />
</div>
</div>
<div class="row">
<div class="col-4">Медикаменты:</div>
<div class="col-8">
<select name="medications" class="form-control" multiple size="6" id="medications">
@foreach (var medication in ViewBag.Medications)
{
<option value="@medication.Id">@medication.MedicationName</option>
}
</select>
</div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4">
<input type="submit" value="Создать" class="btn btn-primary" />
</div>
</div>
</form>

View File

@ -0,0 +1,18 @@
@{
ViewData["Title"] = "DeleteDrug";
}
<div class="text-center">
<h2 class="display-4">Удаление лекарства</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Лекарство:</div>
<div class="col-8">
<select id="drug" name="drug" class="form-control" asp-items="@(new SelectList(@ViewBag.Drugs, "Id", "DrugName"))"></select>
</div>
</div>
<div class="row">
<div class="col-4"></div>
<div class="col-8"><input type="submit" value="Удалить" class="btn btn-danger" /></div>
</div>
</form>s

View File

@ -0,0 +1,53 @@
@using VeterinaryContracts.ViewModels
@model List<DrugViewModel>
@{
ViewData["Title"] = "Drugs";
}
<div class="text-center">
<h1 class="display-4">Лекарства</h1>
</div>
<div class="text-center">
@{
if (Model == null)
{
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<p>
<a asp-action="CreateDrug">Создать лекарство</a>
<a asp-action="UpdateDrug">Обновить лекарство</a>
<a asp-action="DeleteDrug">Удалить лекарство</a>
</p>
<table class="table">
<thead>
<tr>
<th>
Номер
</th>
<th>
Название
</th>
<th>
Цена
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem =>item.Id)
</td>
<td>
@Html.DisplayFor(modelItem =>item.DrugName)
</td>
<td>
@Html.DisplayFor(modelItem =>item.Price)
</td>
</tr>
}
</tbody>
</table>
}
</div>

View File

@ -0,0 +1,77 @@
@using VeterinaryContracts.ViewModels;
@{
ViewData["Title"] = "UpdateDrug";
}
<div class="text-center">
<h2 class="display-4">Редактирование лекарства</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Услуга:</div>
<div class="col-8">
<select id="drug" name="drug" class="form-control" asp-items="@(new SelectList(@ViewBag.Drugs, "Id", "DrugName"))"></select>
</div>
</div>
<div class="row">
<div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="name" id="name" class="form-control" /></div>
</div>
<div class="row">
<div class="col-4">Цена:</div>
<div class="col-8"><input type="text" id="price" name="price" class="form-control" /></div>
</div>
<div class="row">
<div class="col-4">Количество:</div>
<div class="col-8"><input type="number" name="count" id="count" />
</div>
</div>
<div class="row">
<div class="col-4">Медикаменты:</div>
<div class="col-8">
<select name="medications" class="form-control" multiple size="5" id="medications">
@foreach (var medication in ViewBag.Medications)
{
<option value="@medication.Id" id="@medication.MedicationName">@medication.MedicationName</option>
}
</select>
</div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Сохранить" class="btn btn-primary" /></div>
</div>
</form>
@section Scripts
{
<script>
function check() {
var drug = $('#drug').val();
$("#medications option:selected").removeAttr("selected");
if (drug) {
$.ajax({
method: "GET",
url: "/Home/GetDrug",
data: { drugId: drug },
success: function (result) {
console.log(result.item2);
$('#name').val(result.item1.drugName);
$('#price').val(result.item1.price);
$('#count').val(result.item1.count);
$.map(result.item2, function (n) {
console.log("#" + n);
$("#" + n).attr("selected", "selected")
});
}
});
};
}
check();
$('#drug').on('change', function () {
check();
});
</script>
}

View File

@ -31,6 +31,9 @@
<li class="nav-item">
<a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Register">Регистрация</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Drugs">Лекарства</a>
</li>
</ul>
</div>
</div>