From c43bf6c66deec27532cae2cefb319be799343cab Mon Sep 17 00:00:00 2001 From: ValAnn Date: Mon, 29 Apr 2024 16:33:02 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BC=D0=B5=D0=B4=D0=B8=D1=86=D0=B8=D0=BD?= =?UTF-8?q?=D0=B5=20=D1=81=D1=82=D0=BE=D1=80=D0=B0=D0=B4=D0=B6,=20=D0=BB?= =?UTF-8?q?=D1=8E=D0=B1=D0=B8=D0=BE=D0=BC=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implementss/MedicineStorage.cs | 96 +++++++++++++------ 1 file changed, 67 insertions(+), 29 deletions(-) diff --git a/Hospital/HospitalDataBaseImplement/Implementss/MedicineStorage.cs b/Hospital/HospitalDataBaseImplement/Implementss/MedicineStorage.cs index d1ca9ba..0390fdd 100644 --- a/Hospital/HospitalDataBaseImplement/Implementss/MedicineStorage.cs +++ b/Hospital/HospitalDataBaseImplement/Implementss/MedicineStorage.cs @@ -10,45 +10,26 @@ using HospitalContracts.SearchModels; using HospitalContracts.StoragesContracts; using HospitalContracts.ViewModels; using Microsoft.EntityFrameworkCore; +using HospitalDatabaseImplement.Models; namespace HospitalDatabaseImplement.Implements { public class MedicineStorage : IMedicineStorage { - public MedicineViewModel? GetElement(MedicineSearchModel model) + public List GetFullList() { - if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) - { - return null; - } using var context = new HospitalDatabase(); return context.Medicines - .Include(x => x.Pharmacist) - .Include(x => x.Procedures) - .ThenInclude(x => x.Recipe) - .FirstOrDefault(x => ((!string.IsNullOrEmpty(model.Name) && x.FIO == model.Name) - || (model.Id.HasValue && x.Id == model.Id)))?.GetViewModel; + .Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(MedicineSearchModel model) { using var context = new HospitalDatabase(); - if (!string.IsNullOrEmpty(model.Name)) + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) { return context.Medicines - .Include(x => x.Doctor) - .Include(x => x.Recipes) - .ThenInclude(x => x.Recipe) - .Where(x => (x.Id == model.Id)).ToList() - .Select(x => x.GetViewModel).ToList(); - } - else if (model.DoctorId.HasValue) - { - return context.Medicines - .Include(x => x.Doctor) - .Include(x => x.Recipes) - .ThenInclude(x => x.Recipe) - .Where(x => x.DoctorId == model.DoctorId) + .Where(x => x.Name.Contains(model.Name)).ToList() .Select(x => x.GetViewModel).ToList(); } else @@ -57,10 +38,67 @@ namespace HospitalDatabaseImplement.Implements } } -namespace HospitalDatabaseImplement.Implementss -{ - public class MedicineStorage - { - + public MedicineViewModel? GetElement(MedicineSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Medicines + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public MedicineViewModel? Insert(MedicineBindingModel model) + { + using var context = new HospitalDatabase(); + var newMedicine = Medicine.Create(model); + if (newMedicine == null) + { + return null; + } + context.Medicines.Add(newMedicine); + context.SaveChanges(); + return newMedicine.GetViewModel; + } + + public MedicineViewModel? Update(MedicineBindingModel model) + { + using var context = new HospitalDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var medicine = context.Medicines + .FirstOrDefault(rec => rec.Id == model.Id); + if (medicine == null) + { + return null; + } + medicine.Update(model); + context.SaveChanges(); + transaction.Commit(); + return medicine.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public MedicineViewModel? Delete(MedicineBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Medicines + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Medicines.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } +