Сделал формочку услуги

This commit is contained in:
gg12 darfren 2024-04-28 17:34:36 +04:00
parent 48f2eb83b4
commit 62570a8348
10 changed files with 352 additions and 138 deletions

View File

@ -321,148 +321,168 @@ View(APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicin
return result;
}
//public IActionResult CreateService()
//{
// if (APIPharmacist.Pharmacist == null)
// {
// return Redirect("~/Home/Enter");
// }
// ViewBag.Medicines = APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/animal/getmedicines");
public IActionResult Services()
{
if (APIPharmacist.Pharmacist == null)
{
return Redirect("~/Home/Enter");
}
return
View(APIPharmacist.GetRequest<List<ServiceViewModel>>($"api/service/getservices?pharmacistid={APIPharmacist.Pharmacist.Id}"));
// return View();
//}
}
public IActionResult CreateService()
{
if (APIPharmacist.Pharmacist == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Medicines = APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicines");
return View();
}
//[HttpPost]
//public void CreateService(string name, string price, List<int> medicines)
//{
// if (APIPharmacist.Pharmacist == null)
// {
// throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
// }
// double _price;
// try
// {
// _price = Convert.ToDouble(price);
// }
// catch (Exception ex)
// {
// throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
// }
// if (string.IsNullOrEmpty(name) || _price <= 0)
// {
// throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
// }
// Dictionary<int, IMedicineModel> a = new Dictionary<int, IMedicineModel>();
// foreach (int medicine in medicines)
// {
// a.Add(medicine, new AnimalSearchModel { Id = medicine } as IMedicineModel);
// }
[HttpPost]
public void CreateService(string name, string price, List<int> medicines)
{
if (APIPharmacist.Pharmacist == null)
{
throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
}
StringBuilder st = new StringBuilder(price);
for (int i = 0; i < price.Length; i++)
{
if (price[i] == '.')
st[i] = ',';
}
price = st.ToString();
double _price;
try
{
_price = Convert.ToDouble(price);
}
catch (Exception ex)
{
throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
}
if (string.IsNullOrEmpty(name) || _price <= 0)
{
throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
}
Dictionary<int, IMedicineModel> a = new Dictionary<int, IMedicineModel>();
foreach (int medicine in medicines)
{
a.Add(medicine, new AnimalSearchModel { Id = medicine } as IMedicineModel);
}
// APIPharmacist.PostRequest("api/service/createservice", new ServiceBindingModel
// {
// ServiceName = name,
// Price = Math.Round(_price, 2),
// PharmacistId = APIPharmacist.Pharmacist.Id,
// ServiceMedicines = a
// });
// Response.Redirect("Index");
//}
APIPharmacist.PostRequest("api/service/createservice", new ServiceBindingModel
{
ServiceName = name,
Price = Math.Round(_price, 2),
PharmacistId = APIPharmacist.Pharmacist.Id,
ServiceMedicines = a
});
Response.Redirect("Index");
}
//public IActionResult DeleteMedicine()
//{
// if (APIPharmacist.Pharmacist == null)
// {
// return Redirect("~/Home/Enter");
// }
// ViewBag.Medicines = APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicines?pharmacistid={APIPharmacist.Pharmacist.Id}");
// return View();
//}
public IActionResult DeleteService()
{
if (APIPharmacist.Pharmacist == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Services = APIPharmacist.GetRequest<List<ServiceViewModel>>($"api/service/getservices?pharmacistid={APIPharmacist.Pharmacist.Id}");
return View();
}
//[HttpPost]
//public void DeleteMedicine(int medicine)
//{
// if (APIPharmacist.Pharmacist == null)
// {
// throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
// }
// APIPharmacist.PostRequest("api/medicine/deletemedicine", new MedicineBindingModel
// {
// Id = medicine
// });
// Response.Redirect("Index");
//}
[HttpPost]
public void DeleteService(int service)
{
if (APIPharmacist.Pharmacist == null)
{
throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
}
APIPharmacist.PostRequest("api/service/deleteservice", new ServiceBindingModel
{
Id = service
});
Response.Redirect("Index");
}
//public IActionResult UpdateMedicine()
//{
// if (APIPharmacist.Pharmacist == null)
// {
// return Redirect("~/Home/Enter");
// }
// ViewBag.Medicines = APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicines?pharmacistid={APIPharmacist.Pharmacist.Id}");
// ViewBag.Animals = APIPharmacist.GetRequest<List<AnimalViewModel>>($"api/animal/getanimallist");
// return View();
//}
public IActionResult UpdateService()
{
if (APIPharmacist.Pharmacist == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Services = APIPharmacist.GetRequest<List<ServiceViewModel>>($"api/service/getservices?pharmacistid={APIPharmacist.Pharmacist.Id}");
ViewBag.Medicines = APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicines");
return View();
}
//[HttpPost]
//public void UpdateMedicine(int medicine, string name, string price,
// List<int> animals)
//{
// if (APIPharmacist.Pharmacist == null)
// {
// throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
// }
// double _price;
// try
// {
// _price = Convert.ToDouble(price);
// }
// catch (Exception ex)
// {
// throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
// }
// if (string.IsNullOrEmpty(name) || _price <= 0)
// {
// throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
// }
// Dictionary<int, IAnimalModel> a = new Dictionary<int, IAnimalModel>();
// foreach (int animal in animals)
// {
// a.Add(animal, new AnimalSearchModel { Id = animal } as IAnimalModel);
// }
// APIPharmacist.PostRequest("api/medicine/updatemedicine", new MedicineBindingModel
// {
// Id = medicine,
// MedicineName = name,
// Price = Math.Round(_price, 2),
// PharmacistId = APIPharmacist.Pharmacist.Id,
// MedicineAnimals = a
// });
// Response.Redirect("Index");
//}
[HttpPost]
public void UpdateService(int service, string name, string price,
List<int> medicines)
{
if (APIPharmacist.Pharmacist == null)
{
throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
}
StringBuilder st = new StringBuilder(price);
for (int i = 0; i < price.Length; i++)
{
if (price[i] == '.')
st[i] = ',';
}
price = st.ToString();
double _price;
try
{
_price = Convert.ToDouble(price);
}
catch (Exception ex)
{
throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
}
if (string.IsNullOrEmpty(name) || _price <= 0)
{
throw new Exception("Îøèáêà â ââåäåííûõ äàííûõ");
}
Dictionary<int, IMedicineModel> a = new Dictionary<int, IMedicineModel>();
foreach (int medicine in medicines)
{
a.Add(medicine, new MedicineSearchModel { Id = medicine } as IMedicineModel);
}
APIPharmacist.PostRequest("api/service/updateservice", new ServiceBindingModel
{
Id = service,
ServiceName = name,
Price = Math.Round(_price, 2),
PharmacistId = APIPharmacist.Pharmacist.Id,
ServiceMedicines = a
});
Response.Redirect("Index");
}
//[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
//public IActionResult Error()
//{
// return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
//}
[HttpGet]
public Tuple<ServiceViewModel, List<string>>? GetService(int serviceId)
{
if (APIPharmacist.Pharmacist == null)
{
throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
}
var result = APIPharmacist.GetRequest<Tuple<ServiceViewModel, List<string>>>($"api/service/getservice?serviceid={serviceId}");
if (result == null)
{
return default;
}
//[HttpGet]
//public Tuple<MedicineViewModel, List<string>>? GetMedicine(int medicineId)
//{
// if (APIPharmacist.Pharmacist == null)
// {
// throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
// }
// var result = APIPharmacist.GetRequest<Tuple<MedicineViewModel, List<string>>>($"api/medicine/getmedicine?medicineid={medicineId}");
// if (result == null)
// {
// return default;
// }
// return result;
//}
return result;
}
}
}

View File

@ -0,0 +1,39 @@
@{
ViewData["Title"] = "CreateService";
}
<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">
<select name="medicines" class="form-control" multiple size="6" id="medicines">
@foreach (var medicine in ViewBag.Medicines)
{
<option value="@medicine.Id">@medicine.MedicineName</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

@ -1,5 +1,5 @@
@{
ViewData["Title"] = "Delete";
ViewData["Title"] = "DeleteMedicine";
}
<div class="text-center">
<h2 class="display-4">Удаление медикамента</h2>

View File

@ -0,0 +1,18 @@
@{
ViewData["Title"] = "DeleteService";
}
<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="service" name="service" class="form-control" asp-items="@(new SelectList(@ViewBag.Services, "Id", "ServiceName"))"></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,56 @@
@using VetClinicContracts.ViewModels
@model List<ServiceViewModel>
@{
ViewData["Title"] = "Services";
}
<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="CreateService">Создать услугу</a>
<a asp-action="UpdateService">Обновить услугу</a>
<a asp-action="DeleteService">Удалить услугу</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.ServiceName)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.Price)
</td>
</tr>
}
</tbody>
</table>
}
</div>

View File

@ -0,0 +1,71 @@
@using VetClinicContracts.ViewModels;
@{
ViewData["Title"] = "UpdateService";
}
<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="service" name="service" class="form-control" asp-items="@(new SelectList(@ViewBag.Services, "Id", "ServiceName"))"></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">
<select name="medicines" class="form-control" multiple size="5" id="medicines">
@foreach (var medicine in ViewBag.Medicines)
{
<option value="@medicine.Id" id="@medicine.MedicineName">@medicine.MedicineName</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 service = $('#service').val();
$("#medicines option:selected").removeAttr("selected");
if (service) {
$.ajax({
method: "GET",
url: "/Home/GetService",
data: { serviceId: service },
success: function (result) {
console.log(result.item2);
$('#name').val(result.item1.serviceName);
$('#price').val(result.item1.price);
$.map(result.item2, function ( n ) {
console.log("#" + n);
$("#" + n).attr("selected", "selected")
});
}
});
};
}
check();
$('#service').on('change', function () {
check();
});
</script>
}

View File

@ -23,6 +23,9 @@
<li class="nav-item">
<a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Index">Медикаменты</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Services">Услуги</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asparea="" asp-controller="Home" asp-action="Privacy">Личные данные</a>
</li>

View File

@ -52,6 +52,8 @@ namespace VetClinicDataBaseImplement.Models
Id = model.Id,
ServiceName = model.ServiceName,
Price = model.Price,
PharmacistId = model.PharmacistId,
Pharmacist = context.Pharmacists.First(x => x.Id == model.PharmacistId),
Medicines = model.ServiceMedicines.Select(x => new
ServiceMedicine
{

View File

@ -28,7 +28,7 @@ namespace VetClinicRestApi.Controllers
if (elem == null)
return null;
var res = Tuple.Create(elem, elem.MedicineAnimals.Select(x => x.Value.AnimalName).ToList());
res.Item1.MedicineAnimals = null;
res.Item1.MedicineAnimals = null!;
return res;
}
catch (Exception ex)
@ -38,7 +38,7 @@ namespace VetClinicRestApi.Controllers
}
}
[HttpGet]
public List<MedicineViewModel>? GetMedicines(int pharmacistId)
public List<MedicineViewModel>? GetMedicines(int? pharmacistId = null)
{
try
{

View File

@ -4,6 +4,7 @@ using VetClinicContracts.BindingModels;
using VetClinicContracts.BusinessLogicsContracts;
using VetClinicContracts.SearchModels;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;
namespace VetClinicRestApi.Controllers
{
@ -27,7 +28,9 @@ namespace VetClinicRestApi.Controllers
var elem = _service.ReadElement(new ServiceSearchModel { Id = serviceId });
if (elem == null)
return null;
return Tuple.Create(elem, elem.ServiceMedicines.Select(x => x.Value.MedicineName).ToList());
var res = Tuple.Create(elem, elem.ServiceMedicines.Select(x => x.Value.MedicineName).ToList());
res.Item1.ServiceMedicines = null!;
return res;
}
catch (Exception ex)
{
@ -36,11 +39,14 @@ namespace VetClinicRestApi.Controllers
}
}
[HttpGet]
public List<ServiceViewModel> GetServices(int pharmacistId)
public List<ServiceViewModel> GetServices(int? pharmacistId = null)
{
try
{
return _service.ReadList(new ServiceSearchModel { PharmacistId = pharmacistId });
var res = _service.ReadList(new ServiceSearchModel { PharmacistId = pharmacistId });
foreach (var service in res)
service.ServiceMedicines = null;
return res;
}
catch (Exception ex)
{
@ -67,7 +73,6 @@ namespace VetClinicRestApi.Controllers
{
try
{
model.ServiceMedicines = null!;
return _service.Update(model);
}
catch (Exception ex)