goblinrf 2024-04-27 23:07:55 +04:00
commit 0728fb599c
10 changed files with 92 additions and 50 deletions

View File

@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
using PharmacistApp.Models;
using System.Diagnostics;
using VetClinicContracts.BindingModels;
using VetClinicContracts.SearchModels;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;
using VetClinicDataModels.Models;
@ -116,27 +117,45 @@ View(APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicin
{
return Redirect("~/Home/Enter");
}
ViewBag.Animals = APIPharmacist.GetRequest<List<AnimalViewModel>>($"api/animal/getanimallist");
return View();
}
[HttpPost]
public void CreateMedicine(string name, int price)
public void CreateMedicine(string name, string price, List<int> animals)
{
if (APIPharmacist.Pharmacist == null)
{
throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
}
if (string.IsNullOrEmpty(name) || price <= 0)
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/createmedicine", new MedicineBindingModel
{
MedicineName = name,
Price = price,
PharmacistId = APIPharmacist.Pharmacist.Id
Price = Math.Round(_price, 2),
PharmacistId = APIPharmacist.Pharmacist.Id,
MedicineAnimals = a
}) ;
Response.Redirect("Index");
}
@ -172,28 +191,43 @@ View(APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicin
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();
}
[HttpPost]
public void UpdateMedicine(int medicine, string name, int price,
Dictionary<int, IAnimalModel> medicineAnimals = null)
public void UpdateMedicine(int medicine, string name, string price,
List<int> animals)
{
if (APIPharmacist.Pharmacist == null)
{
throw new Exception("Âû êàê ñþäà ïîïàëè? Ñþäà âõîä òîëüêî àâòîðèçîâàííûì");
}
if (string.IsNullOrEmpty(name) || price <= 0)
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 = price,
Price = Math.Round(_price, 2),
PharmacistId = APIPharmacist.Pharmacist.Id,
MedicineAnimals = medicineAnimals
MedicineAnimals = a
});
Response.Redirect("Index");
}
@ -205,7 +239,7 @@ View(APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicin
}
[HttpGet]
public Tuple<MedicineViewModel, string>? GetMedicine(int medicineId)
public Tuple<MedicineViewModel, List<string>>? GetMedicine(int medicineId)
{
if (APIPharmacist.Pharmacist == null)
{
@ -216,16 +250,8 @@ View(APIPharmacist.GetRequest<List<MedicineViewModel>>($"api/medicine/getmedicin
{
return default;
}
string table = "";
result.Item1.MedicineAnimals.Clear();
for (int i = 0; i < result.Item2.Count; i++)
{
var animal = result.Item2[i];
table += "<tr>";
table += $"<td>{animal}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
return result;
}
[HttpGet]

View File

@ -16,7 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\VetClinicContracts\VetClinicContracts.csproj" />
<ProjectReference Include="..\VetClinicDataBaseImplement\VetClinicDataBaseImplement.csproj" />
<ProjectReference Include="..\VetClinicRestApi\VetClinicRestApi.csproj" />
</ItemGroup>
</Project>

View File

@ -18,6 +18,17 @@
<input type="text" name="price" id="price" />
</div>
</div>
<div class="row">
<div class="col-4">Животные:</div>
<div class="col-8">
<select name="animals" class="form-control" multiple size="6" id="animals">
@foreach (var animal in ViewBag.Animals)
{
<option value="@animal.Id">@animal.AnimalName</option>
}
</select>
</div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4">

View File

@ -13,9 +13,6 @@
<div class="col-8">
<select id="medicine" name="medicine" class="form-control" asp-items="@(new SelectList(@ViewBag.Medicines, "Id", "MedicineName"))"></select>
</div>
<div class="col-8">
<select id="animal" name="animal" class="form-control" asp-items="@(new SelectList(@ViewBag.Animals, "Id", "AnimalName"))"></select>
</div>
</div>
<div class="row">
<div class="col-4">Название:</div>
@ -23,20 +20,19 @@
</div>
<div class="row">
<div class="col-4">Цена:</div>
<div class="col-8"><input type="number" id="price" name="price" class="form-control" /></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="animals" class="form-control" multiple size="5" id="animals">
@foreach (var animal in ViewBag.Animals)
{
<option value="@animal.Id" id="@animal.AnimalName">@animal.AnimalName</option>
}
</select>
</div>
</div>
<table class="table">
<thead>
<tr>
<th>
Животное
</th>
</tr>
</thead>
<tbody id="table-elements">
</tbody>
</table>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Сохранить" class="btn btn-primary" /></div>
@ -48,16 +44,22 @@
<script>
function check() {
var medicine = $('#medicine').val();
$("#animals option:selected").removeAttr("selected");
if (medicine) {
$.ajax({
method: "GET",
url: "/Home/GetMedicine",
data: { medicineId: medicine },
success: function (result) {
console.log(result.item2);
$('#name').val(result.item1.medicineName);
$('#price').val(result.item1.price);
$('#table-elements').html(result.item2);
$.map(result.item2, function ( n ) {
console.log("#" + n);
$("#" + n).attr("selected", "selected")
});
}
});
};
}

View File

@ -7,7 +7,7 @@ using VetClinicDataModels.Models;
namespace VetClinicContracts.BindingModels
{
public class AnimalBindingModel
public class AnimalBindingModel : IAnimalModel
{
public int Id { get; set; }
public int AdminId { get; set; }

View File

@ -15,8 +15,6 @@ namespace VetClinicContracts.ViewModels
public string MedicineName { get; set; } = string.Empty;
[DisplayName("Цена медикамента")]
public double Price { get; set; }
[DisplayName("Фармацевт")]
public string PharmacistFIO { get; set; } = string.Empty;
public int PharmacistId { get; set; }
public Dictionary<int, IAnimalModel> MedicineAnimals { get; set; } = new();
}

View File

@ -19,7 +19,6 @@ namespace VetClinicDataBaseImplement.Implements
using var context = new VetClinicDatabase();
return context.Medicines.Include(x => x.Pharmacist).Include(x => x.Animals)
.ThenInclude(x => x.Animal)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
@ -30,7 +29,6 @@ namespace VetClinicDataBaseImplement.Implements
.ThenInclude(x => x.Animal)
.Where(x => (string.IsNullOrEmpty(model.MedicineName) || x.MedicineName.Contains(model.MedicineName))
&& (!model.PharmacistId.HasValue || x.PharmacistId == model.PharmacistId))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}

View File

@ -54,6 +54,7 @@ namespace VetClinicDataBaseImplement.Models
{
Animal = context.Animals.First(y => y.Id == x.Key),
}).ToList(),
PharmacistId = model.PharmacistId,
Pharmacist = context.Pharmacists.First(x => x.Id == model.PharmacistId)
};
}
@ -68,7 +69,6 @@ namespace VetClinicDataBaseImplement.Models
MedicineName = MedicineName,
Price = Price,
MedicineAnimals = MedicineAnimals,
PharmacistFIO = Pharmacist.PharmacistFIO
};
public void UpdateAnimals(VetClinicDatabase context,
MedicineBindingModel model)
@ -81,7 +81,10 @@ namespace VetClinicDataBaseImplement.Models
{
context.MedicineAnimals.RemoveRange(medicineAnimals.Where(rec
=> !model.MedicineAnimals.ContainsKey(rec.AnimalId)));
foreach (var animal in medicineAnimals)
{
model.MedicineAnimals.Remove(animal.AnimalId);
}
context.SaveChanges();
}
var medicine = context.Medicines.First(x => x.Id == Id);

View File

@ -11,7 +11,7 @@ namespace VetClinicDataBaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS02;Initial Catalog=VetClinicDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=VetClinicDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}

View File

@ -27,7 +27,9 @@ namespace VetClinicRestApi.Controllers
var elem = _medicine.ReadElement(new MedicineSearchModel { Id = medicineId });
if (elem == null)
return null;
return Tuple.Create(elem, elem.MedicineAnimals.Select(x => x.Value.AnimalName).ToList());
var res = Tuple.Create(elem, elem.MedicineAnimals.Select(x => x.Value.AnimalName).ToList());
res.Item1.MedicineAnimals = null;
return res;
}
catch (Exception ex)
{
@ -36,11 +38,14 @@ namespace VetClinicRestApi.Controllers
}
}
[HttpGet]
public List<MedicineViewModel> GetMedicines(int pharmacistId)
public List<MedicineViewModel>? GetMedicines(int pharmacistId)
{
try
{
return _medicine.ReadList(new MedicineSearchModel { PharmacistId = pharmacistId});
var res = _medicine.ReadList(new MedicineSearchModel { PharmacistId = pharmacistId });
foreach (var medicine in res)
medicine.MedicineAnimals = null;
return res;
}
catch (Exception ex)
{
@ -67,7 +72,6 @@ namespace VetClinicRestApi.Controllers
{
try
{
model.MedicineAnimals = null!;
return _medicine.Update(model);
}
catch (Exception ex)