diff --git a/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/AbstractSaveToPdfDoctor.cs b/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/AbstractSaveToPdfDoctor.cs index a5188d7..c7fc5e0 100644 --- a/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/AbstractSaveToPdfDoctor.cs +++ b/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/AbstractSaveToPdfDoctor.cs @@ -30,7 +30,7 @@ namespace VeterinaryBusinessLogic.OfficePackage CreateTable(new List { "4cm", "4cm", "4cm", "4cm" }); CreateRow(new PdfRowParameters { - Texts = new List { "Дата", "Название медикамента", "Услуга рекомендации","Название визита" }, + Texts = new List { "Дата", "Название медикамента", "Посещение ","Лекарство" }, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Implements/DrugStorage.cs b/VeterinaryView/VeterinaryDatabaseImplement/Implements/DrugStorage.cs index e4270ed..2232812 100644 --- a/VeterinaryView/VeterinaryDatabaseImplement/Implements/DrugStorage.cs +++ b/VeterinaryView/VeterinaryDatabaseImplement/Implements/DrugStorage.cs @@ -45,7 +45,7 @@ namespace VeterinaryDatabaseImplement.Implements return null; } using var context = new VeterinaryDatabase(); - return context.Drugs + return context.Drugs.Include(x => x.Doctor) .Include(x => x.Medications) .ThenInclude(x => x.Medication) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DrugName) && @@ -81,7 +81,7 @@ namespace VeterinaryDatabaseImplement.Implements drug.UpdateMedications(context, model); // работает context.SaveChanges(); - drug.Update(context,model);// не работает сумма + drug.Update(model);// не работает сумма context.SaveChanges(); transaction.Commit(); return drug.GetViewModel; diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Models/Drug.cs b/VeterinaryView/VeterinaryDatabaseImplement/Models/Drug.cs index 569d37c..5693b50 100644 --- a/VeterinaryView/VeterinaryDatabaseImplement/Models/Drug.cs +++ b/VeterinaryView/VeterinaryDatabaseImplement/Models/Drug.cs @@ -24,6 +24,8 @@ namespace VeterinaryDatabaseImplement.Models public int Count { get; set; } [Required] public int DoctorId { get; private set; } + public virtual Doctor? Doctor { get; private set; } + [Required] public DateTime DateCreate { get; private set; } private Dictionary? _drugMedications = null; @@ -69,19 +71,10 @@ namespace VeterinaryDatabaseImplement.Models }; } // цену считает неверно - public void Update(VeterinaryDatabase context, DrugBindingModel model) + public void Update( DrugBindingModel model) { - var justMedications = model.DrugMedications.Select(x => new DrugMedication - { - Medication = context.Medications.First(y => y.Id == x.Key) - }).ToList(); - double pric = 0; - foreach (var justMedication in justMedications) - { - pric += justMedication.Medication.Price; - } + DrugName = model.DrugName; - Price = pric; } public DrugViewModel GetViewModel => new() { @@ -90,11 +83,18 @@ namespace VeterinaryDatabaseImplement.Models Price = Price, Count= Count, DoctorId=DoctorId, - DateCreate = DateCreate + DateCreate = DateCreate, + DrugMedications = DrugMedications }; public void UpdateMedications(VeterinaryDatabase context, DrugBindingModel model) { var drugMedications = context.DrugMedications.Where(rec => rec.DrugId == model.Id).ToList(); + var list = new List (); + foreach (var rec in model.DrugMedications) + { + list.Add(rec.Key); + } + //var tmp = model.DrugMedications; if (drugMedications != null && drugMedications.Count > 0) { // из таблицы бд удаляем строчки, которые были отменены(которые раньше были а тепперь их в модели нет) @@ -123,6 +123,17 @@ namespace VeterinaryDatabaseImplement.Models context.SaveChanges(); } _drugMedications = null; + var justMedications = list.Select(x => new DrugMedication + { + Medication = context.Medications.First(y => y.Id == x) + }).ToList(); + double pric = 0; + foreach (var justMedication in justMedications) + { + pric += justMedication.Medication.Price; + } + Price = pric; + context.SaveChanges(); } diff --git a/VeterinaryView/VeterinaryRestApi/Controllers/DrugController.cs b/VeterinaryView/VeterinaryRestApi/Controllers/DrugController.cs index 472f6c7..d013e3e 100644 --- a/VeterinaryView/VeterinaryRestApi/Controllers/DrugController.cs +++ b/VeterinaryView/VeterinaryRestApi/Controllers/DrugController.cs @@ -19,18 +19,20 @@ namespace VeterinaryRestApi.Controllers _drug = drug; } [HttpGet] - public Tuple>? GetDrug(int drugId) + public Tuple>>? GetDrug(int drugId) { try { var elem = _drug.ReadElement(new DrugSearchModel { Id = drugId }); if (elem == null) return null; - return Tuple.Create(elem, elem.DrugMedications.Select(x => x.Value.MedicationName).ToList()); + var res = Tuple.Create(elem, elem.DrugMedications.Select(x => Tuple.Create(x.Value.MedicationName, x.Value.Id)).ToList()); + res.Item1.DrugMedications = null!; + return res; } catch (Exception ex) { - _logger.LogError(ex, "Ошибка получения лекарства по id={Id}", drugId); + _logger.LogError(ex, "Ошибка получения услуги по id={Id}", drugId); throw; } } @@ -52,15 +54,22 @@ namespace VeterinaryRestApi.Controllers } [HttpGet] - public List GetDrugs(int doctorId) + public List GetDrugs(int? doctorId = null) { try { - return _drug.ReadList(new DrugSearchModel { DoctorId = doctorId }); + List res; + if (!doctorId.HasValue) + res = _drug.ReadList(null); + else + res = _drug.ReadList(new DrugSearchModel { DoctorId = doctorId.Value }); + foreach (var drug in res) + drug.DrugMedications = null; + return res; } catch (Exception ex) { - _logger.LogError(ex, "Ошибка получения списка лекарств"); + _logger.LogError(ex, "Ошибка получения списка услуг"); throw; } } diff --git a/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs b/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs index 70a95e4..5d8a7cb 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs +++ b/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs @@ -314,13 +314,13 @@ namespace VeterinaryShowDoctorApp.Controllers } [HttpGet] - public Tuple>? GetDrug(int drugId) + public Tuple>>? GetDrug(int drugId) { if (APIDoctor.Doctor == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } - var result = APIDoctor.GetRequest>>($"api/drug/getdrug?drugid={drugId}"); + var result = APIDoctor.GetRequest>>>($"api/drug/getdrug?drugid={drugId}"); if (result == null) { return default; diff --git a/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/UpdateDrug.cshtml b/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/UpdateDrug.cshtml index a3234da..f7603ba 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/UpdateDrug.cshtml +++ b/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/UpdateDrug.cshtml @@ -33,7 +33,7 @@ @@ -44,6 +44,8 @@ + + @section Scripts { -} +} \ No newline at end of file