From cfd9c7c4061fd54fd31cf096e8135f6ff622eb9d Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Thu, 25 Apr 2024 22:16:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D1=80=D1=83=D1=87=D0=B8=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D1=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/DrugLogic.cs | 6 +- .../BusinessLogicContracts/IDrugLogic.cs | 2 + .../StorageContracts/IDrugStorage.cs | 2 + .../Implements/DoctorStorage.cs | 89 +++++++++++ .../Implements/DrugStorage.cs | 146 ++++++++++++++++++ .../Implements/MedicationStorage.cs | 89 +++++++++++ .../Implements/ServiceStorage.cs | 109 +++++++++++++ .../VeterinaryDatabaseImplement.csproj | 4 - 8 files changed, 442 insertions(+), 5 deletions(-) create mode 100644 VeterinaryView/VeterinaryDatabaseImplement/Implements/DoctorStorage.cs create mode 100644 VeterinaryView/VeterinaryDatabaseImplement/Implements/DrugStorage.cs create mode 100644 VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs create mode 100644 VeterinaryView/VeterinaryDatabaseImplement/Implements/ServiceStorage.cs diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/DrugLogic.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/DrugLogic.cs index 552f2a3..fb76138 100644 --- a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/DrugLogic.cs +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/DrugLogic.cs @@ -9,7 +9,7 @@ using VeterinaryContracts.SearchModels; using VeterinaryContracts.StorageContracts; using VeterinaryContracts.ViewModels; using Microsoft.Extensions.Logging; - +using VeterinaryDataModels; namespace VeterinaryBusinessLogic.BusinessLogic { @@ -113,5 +113,9 @@ namespace VeterinaryBusinessLogic.BusinessLogic throw new InvalidOperationException("Лекарство с таким названием уже есть"); } } + public bool MakeSell(IDrugModel drug, int count) + { + return _drugStorage.SellDrugs(drug, count); + } } } diff --git a/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IDrugLogic.cs b/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IDrugLogic.cs index 8a2b9fd..4126b18 100644 --- a/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IDrugLogic.cs +++ b/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IDrugLogic.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using VeterinaryContracts.BindingModels; using VeterinaryContracts.ViewModels; using VeterinaryContracts.SearchModels; +using VeterinaryDataModels; namespace VeterinaryContracts.BusinessLogicContracts { @@ -16,5 +17,6 @@ namespace VeterinaryContracts.BusinessLogicContracts bool Create(DrugBindingModel model); bool Update(DrugBindingModel model); bool Delete(DrugBindingModel model); + bool MakeSell(IDrugModel drug, int count); } } diff --git a/VeterinaryView/VeterinaryContracts/StorageContracts/IDrugStorage.cs b/VeterinaryView/VeterinaryContracts/StorageContracts/IDrugStorage.cs index 0f4f045..f7ee623 100644 --- a/VeterinaryView/VeterinaryContracts/StorageContracts/IDrugStorage.cs +++ b/VeterinaryView/VeterinaryContracts/StorageContracts/IDrugStorage.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using VeterinaryContracts.BindingModels; using VeterinaryContracts.ViewModels; using VeterinaryContracts.SearchModels; +using VeterinaryDataModels; namespace VeterinaryContracts.StorageContracts { @@ -17,5 +18,6 @@ namespace VeterinaryContracts.StorageContracts DrugViewModel? Insert(DrugBindingModel model); DrugViewModel? Update(DrugBindingModel model); DrugViewModel? Delete(DrugBindingModel model); + public bool SellDrugs(IDrugModel model, int count); } } diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Implements/DoctorStorage.cs b/VeterinaryView/VeterinaryDatabaseImplement/Implements/DoctorStorage.cs new file mode 100644 index 0000000..c37a711 --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Implements/DoctorStorage.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryContracts.StorageContracts; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.ViewModels; +using VeterinaryDatabaseImplement.Models; + +namespace VeterinaryDatabaseImplement.Implements +{ + public class DoctorStorage : IDoctorStorage + { + public List GetFullList() + { + using var context = new VeterinaryDatabase(); + return context.Doctors + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(DoctorSearchModel model) + { + if (string.IsNullOrEmpty(model.DoctorFIO) && string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Password) && + string.IsNullOrEmpty(model.DoctorFIO)) + { + return new(); + } + using var context = new VeterinaryDatabase(); + return context.Doctors + .Where(x => (string.IsNullOrEmpty(model.DoctorFIO) || x.DoctorFIO.Contains(model.DoctorFIO) && + (string.IsNullOrEmpty(model.Login) || x.DoctorFIO.Contains(model.Login)) && + (string.IsNullOrEmpty(model.Password) || x.DoctorFIO.Contains(model.Password)))) + .Select(x => x.GetViewModel) + .ToList(); + } + public DoctorViewModel? GetElement(DoctorSearchModel model) + { + if (string.IsNullOrEmpty(model.DoctorFIO) && string.IsNullOrEmpty(model.Login) && + !model.Id.HasValue) + { + return null; + } + using var context = new VeterinaryDatabase(); + return context.Doctors + .FirstOrDefault(x => (string.IsNullOrEmpty(model.DoctorFIO) || x.DoctorFIO == model.DoctorFIO) && + (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Login) || x.Login == model.Login) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password)) + ?.GetViewModel; + } + public DoctorViewModel? Insert(DoctorBindingModel model) + { + var newDoctor = Doctor.Create(model); + if (newDoctor == null) + { + return null; + } + using var context = new VeterinaryDatabase(); + context.Doctors.Add(newDoctor); + context.SaveChanges(); + return newDoctor.GetViewModel; + } + public DoctorViewModel? Update(DoctorBindingModel model) + { + using var context = new VeterinaryDatabase(); + var client = context.Doctors.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + context.SaveChanges(); + return client.GetViewModel; + } + public DoctorViewModel? Delete(DoctorBindingModel model) + { + using var context = new VeterinaryDatabase(); + var element = context.Doctors.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Doctors.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Implements/DrugStorage.cs b/VeterinaryView/VeterinaryDatabaseImplement/Implements/DrugStorage.cs new file mode 100644 index 0000000..a45316e --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Implements/DrugStorage.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryContracts.StorageContracts; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.ViewModels; +using VeterinaryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using VeterinaryDataModels; + + +namespace VeterinaryDatabaseImplement.Implements +{ + public class DrugStorage : IDrugStorage + { + public List GetFullList() + { + using var context = new VeterinaryDatabase(); + return context.Drugs + .Include(x => x.Medications) + .ThenInclude(x => x.Medication) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(DrugSearchModel model) + { + if (string.IsNullOrEmpty(model.DrugName)) + { + return new(); + } + using var context = new VeterinaryDatabase(); + return context.Drugs.Include(x => x.Medications) + .ThenInclude(x => x.Medication) + .Where(x => x.DrugName.Contains(model.DrugName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public DrugViewModel? GetElement(DrugSearchModel model) + { + if (string.IsNullOrEmpty(model.DrugName) && + !model.Id.HasValue) + { + return null; + } + using var context = new VeterinaryDatabase(); + return context.Drugs + .Include(x => x.Medications) + .ThenInclude(x => x.Medication) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DrugName) && + x.DrugName == model.DrugName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public DrugViewModel? Insert(DrugBindingModel model) + { + using var context = new VeterinaryDatabase(); + var newDrug = Drug.Create(context, model); + if (newDrug == null) + { + return null; + } + context.Drugs.Add(newDrug); + context.SaveChanges(); + return newDrug.GetViewModel; + } + public DrugViewModel? Update(DrugBindingModel model) + { + using var context = new VeterinaryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var drug = context.Drugs.FirstOrDefault(rec => + rec.Id == model.Id); + if (drug == null) + { + return null; + } + drug.Update(model); + context.SaveChanges(); + drug.UpdateMedications(context, model); + transaction.Commit(); + return drug.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public DrugViewModel? Delete(DrugBindingModel model) + { + using var context = new VeterinaryDatabase(); + var element = context.Drugs + .Include(x => x.Medications) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Drugs.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public bool SellDrugs(IDrugModel model, int count) + { + if (model == null) + return false; + using var context = new VeterinaryDatabase(); + using var transaction = context.Database.BeginTransaction(); + //List lst = new List(); + //foreach (var el in context.ShopDrugs.Where(x => x.DrugId == model.Id)) + //{ + int dif = count; + if (model.Count < dif) + dif = model.Count; + model.Count -= dif; + count -= dif; + if (el.Count == 0) + { + lst.Add(el); + } + if (count == 0) + break; + //} + if (count > 0) + { + transaction.Rollback(); + return false; + } + // почистили лист + foreach (var el in lst) + { + context.ShopDrugs.Remove(el); + } + context.SaveChanges(); + transaction.Commit(); + return true; + } + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs b/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs new file mode 100644 index 0000000..b6c967d --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryContracts.StorageContracts; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.ViewModels; +using VeterinaryDatabaseImplement.Models; +using VeterinaryDatabaseImplement; + + +namespace VeterinaryDatabaseImplement.Implements +{ + public class MedicationStorage : IMedicationStorage + { + public List GetFullList() + { + using var context = new VeterinaryDatabase(); + return context.Medications + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(MedicationSearchModel model) + { + if (string.IsNullOrEmpty(model.MedicationName)) + { + return new(); + } + using var context = new VeterinaryDatabase(); + return context.Medications + .Where(x => x.MedicationName.Contains(model.MedicationName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public MedicationViewModel? GetElement(MedicationSearchModel model) + { + if (string.IsNullOrEmpty(model.MedicationName) && !model.Id.HasValue) + { + return null; + } + using var context = new VeterinaryDatabase(); + return context.Medications + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.MedicationName) && x.MedicationName == + model.MedicationName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public MedicationViewModel? Insert(MedicationBindingModel model) + { + var newMedication = Medication.Create(model); + if (newMedication == null) + { + return null; + } + using var context = new VeterinaryDatabase(); + context.Medications.Add(newMedication); + context.SaveChanges(); + return newMedication.GetViewModel; + } + public MedicationViewModel? Update(MedicationBindingModel model) + { + using var context = new VeterinaryDatabase(); + var medication = context.Medications.FirstOrDefault(x => x.Id ==model.Id); + if (medication == null) + { + return null; + } + medication.Update(model); + context.SaveChanges(); + return medication.GetViewModel; + } + public MedicationViewModel? Delete(MedicationBindingModel model) + { + using var context = new VeterinaryDatabase(); + var element = context.Medications.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Medications.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Implements/ServiceStorage.cs b/VeterinaryView/VeterinaryDatabaseImplement/Implements/ServiceStorage.cs new file mode 100644 index 0000000..8b8ea1d --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Implements/ServiceStorage.cs @@ -0,0 +1,109 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.StorageContracts; +using VeterinaryContracts.ViewModels; +using VeterinaryDatabaseImplement.Models; + +namespace VeterinaryDatabaseImplement.Implements +{ + public class ServiceStorage : IServiceStorage + { + public List GetFullList() + { + using var context = new VeterinaryDatabase(); + return context.Services.Include(x=>x.Doctor) + .Include(x => x.Medications) + .ThenInclude(x => x.Medication) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ServiceSearchModel model) + { + if (string.IsNullOrEmpty(model.ServiceName)) + { + return new(); + } + using var context = new VeterinaryDatabase(); + return context.Services.Include(x => x.Doctor).Include(x => x.Medications) + .ThenInclude(x => x.Medication) + .Where(x => x.ServiceName.Contains(model.ServiceName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public ServiceViewModel? GetElement(ServiceSearchModel model) + { + if (string.IsNullOrEmpty(model.ServiceName) && + !model.Id.HasValue) + { + return null; + } + using var context = new VeterinaryDatabase(); + return context.Services.Include(x => x.Doctor) + .Include(x => x.Medications) + .ThenInclude(x => x.Medication) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ServiceName) && + x.ServiceName == model.ServiceName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public ServiceViewModel? Insert(ServiceBindingModel model) + { + using var context = new VeterinaryDatabase(); + var newService = Service.Create(context, model); + if (newService == null) + { + return null; + } + context.Services.Add(newService); + context.SaveChanges(); + return newService.GetViewModel; + } + public ServiceViewModel? Update(ServiceBindingModel model) + { + using var context = new VeterinaryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var service = context.Services.FirstOrDefault(rec => + rec.Id == model.Id); + if (service == null) + { + return null; + } + service.Update(model); + context.SaveChanges(); + service.UpdateMedications(context, model); + transaction.Commit(); + return service.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ServiceViewModel? Delete(ServiceBindingModel model) + { + using var context = new VeterinaryDatabase(); + var element = context.Services.Include(x => x.Doctor) + .Include(x => x.Medications) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Services.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/VeterinaryDatabaseImplement.csproj b/VeterinaryView/VeterinaryDatabaseImplement/VeterinaryDatabaseImplement.csproj index bfa4467..defc204 100644 --- a/VeterinaryView/VeterinaryDatabaseImplement/VeterinaryDatabaseImplement.csproj +++ b/VeterinaryView/VeterinaryDatabaseImplement/VeterinaryDatabaseImplement.csproj @@ -11,10 +11,6 @@ - - - -