From d7e0c7a71ee98a89aad424efdc2b4c94e4d535c5 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Wed, 24 Apr 2024 11:32:17 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=BD=D0=B0=D0=BA=D0=BE=D1=81=D1=8F=D1=87?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=81=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B5=D0=BC,?= =?UTF-8?q?=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Implement.csproj => VetClinicDataBaseImplement.csproj} | 0 VetClinic/VetClinic.sln | 8 ++++---- VetClinic/VetClinicRestApi/VetClinicRestApi.csproj | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename VetClinic/DinerDataBaseImplement/{DinerDataBaseImplement.csproj => VetClinicDataBaseImplement.csproj} (100%) diff --git a/VetClinic/DinerDataBaseImplement/DinerDataBaseImplement.csproj b/VetClinic/DinerDataBaseImplement/VetClinicDataBaseImplement.csproj similarity index 100% rename from VetClinic/DinerDataBaseImplement/DinerDataBaseImplement.csproj rename to VetClinic/DinerDataBaseImplement/VetClinicDataBaseImplement.csproj diff --git a/VetClinic/VetClinic.sln b/VetClinic/VetClinic.sln index 4971aa4..b4f022d 100644 --- a/VetClinic/VetClinic.sln +++ b/VetClinic/VetClinic.sln @@ -7,13 +7,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VetClinicBusinessLogic", "V EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VetClinicDataModels", "VetClinicDataModels\VetClinicDataModels.csproj", "{F8D5504B-8955-45E7-BF6B-3ABB7C3DC1BC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VetClinicRestApi", "VetClinicRestApi\VetClinicRestApi.csproj", "{5C215E87-7C87-4D5F-B186-BE02EBB33308}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VetClinicRestApi", "VetClinicRestApi\VetClinicRestApi.csproj", "{5C215E87-7C87-4D5F-B186-BE02EBB33308}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VetClinicContracts", "VetClinicContracts\VetClinicContracts.csproj", "{F2944E52-DC59-42B0-98D2-AC93E22DAB8C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VetClinicContracts", "VetClinicContracts\VetClinicContracts.csproj", "{F2944E52-DC59-42B0-98D2-AC93E22DAB8C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerDataBaseImplement", "DinerDataBaseImplement\DinerDataBaseImplement.csproj", "{4BA6DC6C-8D75-4F4B-88E3-E634B360C14F}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VetClinicDataBaseImplement", "DinerDataBaseImplement\VetClinicDataBaseImplement.csproj", "{4BA6DC6C-8D75-4F4B-88E3-E634B360C14F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VetClinicAdminApp", "VetClinicAdminApi\VetClinicAdminApp.csproj", "{75280728-CC4C-4C43-8921-2DC4AD8A9192}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VetClinicAdminApp", "VetClinicAdminApi\VetClinicAdminApp.csproj", "{75280728-CC4C-4C43-8921-2DC4AD8A9192}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/VetClinic/VetClinicRestApi/VetClinicRestApi.csproj b/VetClinic/VetClinicRestApi/VetClinicRestApi.csproj index 8c46184..7a5241b 100644 --- a/VetClinic/VetClinicRestApi/VetClinicRestApi.csproj +++ b/VetClinic/VetClinicRestApi/VetClinicRestApi.csproj @@ -16,7 +16,7 @@ - + From 17d4058b9baec630dadb7beca5e30e01146e42d2 Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Wed, 24 Apr 2024 12:38:06 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8=20(=D0=B2=D1=81=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BD=D0=BE=20=D0=BF=D1=80=D0=B8=D0=B4=D0=B5?= =?UTF-8?q?=D1=82=D1=81=D1=8F=20=D0=B4=D0=BE=D0=BF=D0=B8=D0=BB=D0=B8=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D1=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DinerDataBaseImplement/Models/Guidance.cs | 58 ++++++++++ .../DinerDataBaseImplement/Models/Medicine.cs | 98 ++++++++++++++++ .../Models/MedicineAnimal.cs | 21 ++++ .../Models/Pharmacist.cs | 73 ++++++++++++ .../DinerDataBaseImplement/Models/Service.cs | 107 ++++++++++++++++++ .../Models/ServiceMedicine.cs | 23 ++++ .../VetClinicDatabase.cs | 12 +- .../BindingModels/MedicineBindingModel.cs | 2 +- .../BindingModels/ServiceBindingModel.cs | 2 +- .../ViewModels/MedicineViewModel.cs | 2 +- .../ViewModels/ServiceViewModel.cs | 2 +- .../Models/IMedicineModel.cs | 2 +- .../Models/IServiceModel.cs | 4 +- 13 files changed, 396 insertions(+), 10 deletions(-) create mode 100644 VetClinic/DinerDataBaseImplement/Models/Guidance.cs create mode 100644 VetClinic/DinerDataBaseImplement/Models/Medicine.cs create mode 100644 VetClinic/DinerDataBaseImplement/Models/MedicineAnimal.cs create mode 100644 VetClinic/DinerDataBaseImplement/Models/Pharmacist.cs create mode 100644 VetClinic/DinerDataBaseImplement/Models/Service.cs create mode 100644 VetClinic/DinerDataBaseImplement/Models/ServiceMedicine.cs diff --git a/VetClinic/DinerDataBaseImplement/Models/Guidance.cs b/VetClinic/DinerDataBaseImplement/Models/Guidance.cs new file mode 100644 index 0000000..e8c68ad --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Models/Guidance.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.ViewModels; +using VetClinicDataModels.Models; + +namespace VetClinicDataBaseImplement.Models +{ + public class Guidance : IGuidanceModel + { + public int Id { get; private set; } + [Required] + public int ServiceId { get; private set; } + public virtual Service Service { get; private set; } + [Required] + public string Text { get; private set; } = string.Empty; + [Required] + public DateTime Date { get; private set; } + public static Guidance? Create(VetClinicDatabase context, GuidanceBindingModel model) + { + if (model == null) + { + return null; + } + return new Guidance() + { + Id = model.Id, + ServiceId = model.ServiceId, + Text = model.Text, + Date = model.Date, + Service = context.Services.FirstOrDefault(x => x.Id == model.ServiceId), + }; + } + + public void Update(GuidanceBindingModel? model) + { + if (model == null) + { + return; + } + Text = model.Text; + Date = model.Date; + } + + public GuidanceViewModel GetViewModel => new() + { + ServiceId = ServiceId, + Text = Text, + Date = Date, + Id = Id, + ServiceName = Service.ServiceName + }; + } +} diff --git a/VetClinic/DinerDataBaseImplement/Models/Medicine.cs b/VetClinic/DinerDataBaseImplement/Models/Medicine.cs new file mode 100644 index 0000000..662e0b9 --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Models/Medicine.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicDataModels.Models; +using VetClinicContracts.BindingModels; +using VetClinicContracts.ViewModels; + +namespace VetClinicDataBaseImplement.Models +{ + public class Medicine : IMedicineModel + { + public int Id { get; set; } + [Required] + public string MedicineName { get; set; } = string.Empty; + [Required] + public int PharmacistId { get; set; } + public virtual Pharmacist Pharmacist { get; private set; } + [Required] + public double Price { get; set; } + private Dictionary? _medicineAnimals = + null; + [NotMapped] + public Dictionary MedicineAnimals + { + get + { + if (_medicineAnimals == null) + { + _medicineAnimals = Animals + .ToDictionary(recPC => recPC.AnimalId, recPC => + recPC.Animal as IAnimalModel); + } + return _medicineAnimals; + } + } + [ForeignKey("MedicineId")] + public virtual List Animals { get; set; } = new(); + [ForeignKey("MedicineId")] + public virtual List Services { get; set; } = new(); + public static Medicine Create(VetClinicDatabase context, + MedicineBindingModel model) + { + return new Medicine() + { + Id = model.Id, + MedicineName = model.MedicineName, + Price = model.Price, + Animals = model.MedicineAnimals.Select(x => new + MedicineAnimal + { + Animal = context.Animals.First(y => y.Id == x.Key), + }).ToList(), + Pharmacist = context.Pharmacists.First(x => x.Id == model.PharmacistId) + }; + } + public void Update(MedicineBindingModel model) + { + MedicineName = model.MedicineName; + Price = model.Price; + } + public MedicineViewModel GetViewModel => new() + { + Id = Id, + MedicineName = MedicineName, + Price = Price, + MedicineAnimals = MedicineAnimals, + PharmacistFIO = Pharmacist.PharmacistFIO + }; + public void UpdateComponents(VetClinicDatabase context, + MedicineBindingModel model) + { + var medicineAnimals = context.MedicineAnimals.Where(rec => + rec.MedicineId == model.Id).ToList(); + if (medicineAnimals != null && medicineAnimals.Count > 0) + { + context.MedicineAnimals.RemoveRange(medicineAnimals.Where(rec +=> !model.MedicineAnimals.ContainsKey(rec.AnimalId))); + + context.SaveChanges(); + } + var medicine = context.Medicines.First(x => x.Id == Id); + foreach (var pc in model.MedicineAnimals) + { + context.MedicineAnimals.Add(new MedicineAnimal + { + Medicine = medicine, + Animal = context.Animals.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _medicineAnimals = null; + } + } +} diff --git a/VetClinic/DinerDataBaseImplement/Models/MedicineAnimal.cs b/VetClinic/DinerDataBaseImplement/Models/MedicineAnimal.cs new file mode 100644 index 0000000..22d06e3 --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Models/MedicineAnimal.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VetClinicDataBaseImplement.Models +{ + public class MedicineAnimal + { + public int Id { get; set; } + [Required] + public int MedicineId { get; set; } + [Required] + public int AnimalId { get; set; } + public virtual Medicine Medicine { get; set; } = new(); + public virtual Animal Animal { get; set; } = new(); + } +} diff --git a/VetClinic/DinerDataBaseImplement/Models/Pharmacist.cs b/VetClinic/DinerDataBaseImplement/Models/Pharmacist.cs new file mode 100644 index 0000000..3f93d15 --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Models/Pharmacist.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.ViewModels; +using VetClinicDataModels.Models; + +namespace VetClinicDataBaseImplement.Models +{ + public class Pharmacist : IPharmacistModel + { + public int Id { get; private set; } + [Required] + public string PharmacistFIO { get; private set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [ForeignKey("PharmacistId")] + public virtual List Medicines { get; set; } = + new(); + [ForeignKey("PharmacistId")] + public virtual List Services { get; set; } = + new(); + + public static Pharmacist? Create(PharmacistBindingModel model) + { + if (model == null) + { + return null; + } + return new Pharmacist() + { + Id = model.Id, + PharmacistFIO = model.PharmacistFIO, + Email = model.Email, + Password = model.Password + }; + } + + public static Pharmacist Create(PharmacistViewModel model) + { + return new Pharmacist() + { + Id = model.Id, + PharmacistFIO = model.PharmacistFIO, + Email = model.Email, + Password = model.Password + }; + } + public void Update( PharmacistBindingModel model) + { + if (model == null) + { + return; + } + PharmacistFIO = model.PharmacistFIO; + Email = model.Email; + Password = model.Password; + } + public PharmacistBindingModel GetViewModel => new() + { + Id = Id, + PharmacistFIO = PharmacistFIO, + Email = Email, + Password = Password + }; + } +} diff --git a/VetClinic/DinerDataBaseImplement/Models/Service.cs b/VetClinic/DinerDataBaseImplement/Models/Service.cs new file mode 100644 index 0000000..2e01439 --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Models/Service.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicDataModels.Models; +using VetClinicContracts.BindingModels; +using VetClinicContracts.ViewModels; + +namespace VetClinicDataBaseImplement.Models +{ + public class Service : IServiceModel + { + public int Id { get; set; } + [Required] + public string ServiceName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + [Required] + public int PharmacistId { get; set; } + public virtual Pharmacist Pharmacist { get; private set; } + private Dictionary? _serviceMedicines = + null; + [NotMapped] + public Dictionary ServiceMedicines + { + get + { + if (_serviceMedicines == null) + { + _serviceMedicines = Medicines + .ToDictionary(recPC => recPC.MedicineId, recPC => + (recPC.Medicine as IMedicineModel, recPC.Count)); + } + return _serviceMedicines; + } + } + [ForeignKey("ServiceId")] + public virtual List Medicines { get; set; } = new(); + [ForeignKey("ServiceId")] + public virtual List Visits { get; set; } = new(); + [ForeignKey("ServiceId")] + public virtual List Guidances { get; set; } = new(); + public static Service Create(VetClinicDatabase context, + ServiceBindingModel model) + { + return new Service() + { + Id = model.Id, + ServiceName = model.ServiceName, + Price = model.Price, + Medicines = model.ServiceMedicines.Select(x => new + ServiceMedicine + { + Medicine = context.Medicines.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ServiceBindingModel model) + { + ServiceName = model.ServiceName; + Price = model.Price; + } + public ServiceViewModel GetViewModel => new() + { + Id = Id, + ServiceName = ServiceName, + Price = Price, + ServiceMedicines = ServiceMedicines + }; + public void UpdateMedicines(VetClinicDatabase context, + ServiceBindingModel model) + { + var serviceMedicines = context.ServiceMedicines.Where(rec => + rec.ServiceId == model.Id).ToList(); + if (serviceMedicines != null && serviceMedicines.Count > 0) + { + context.ServiceMedicines.RemoveRange(serviceMedicines.Where(rec +=> !model.ServiceMedicines.ContainsKey(rec.MedicineId))); + + context.SaveChanges(); + foreach (var updateComponent in serviceMedicines) + { + updateComponent.Count = + model.ServiceMedicines[updateComponent.MedicineId].Item2; + model.ServiceMedicines.Remove(updateComponent.MedicineId); + } + context.SaveChanges(); + } + var service = context.Services.First(x => x.Id == Id); + foreach (var pc in model.ServiceMedicines) + { + context.ServiceMedicines.Add(new ServiceMedicine + { + Service = service, + Medicine = context.Medicines.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _serviceMedicines = null; + } + } +} diff --git a/VetClinic/DinerDataBaseImplement/Models/ServiceMedicine.cs b/VetClinic/DinerDataBaseImplement/Models/ServiceMedicine.cs new file mode 100644 index 0000000..5e3b3d0 --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Models/ServiceMedicine.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VetClinicDataBaseImplement.Models +{ + public class ServiceMedicine + { + public int Id { get; set; } + [Required] + public int ServiceId { get; set; } + [Required] + public int MedicineId { get; set; } + [Required] + public int Count { get; set; } + public virtual Service Service { get; set; } = new(); + public virtual Medicine Medicine { get; set; } = new(); + } +} diff --git a/VetClinic/DinerDataBaseImplement/VetClinicDatabase.cs b/VetClinic/DinerDataBaseImplement/VetClinicDatabase.cs index d66b522..c54fdf0 100644 --- a/VetClinic/DinerDataBaseImplement/VetClinicDatabase.cs +++ b/VetClinic/DinerDataBaseImplement/VetClinicDatabase.cs @@ -15,7 +15,13 @@ namespace VetClinicDataBaseImplement } public virtual DbSet Animals { set; get; } public virtual DbSet Admins { set; get; } - // public virtual DbSet Visits { set; get; } - // public virtual DbSet Vaccinations { set; get; } - } + // public virtual DbSet Visits { set; get; } + // public virtual DbSet Vaccinations { set; get; } + public virtual DbSet Pharmacists { set; get; } + public virtual DbSet Services { set; get; } + public virtual DbSet Medicines { set; get; } + public virtual DbSet ServiceMedicines { set; get; } + public virtual DbSet MedicineAnimals { set; get; } + public virtual DbSet Guidances { set; get; } + } } diff --git a/VetClinic/VetClinicContracts/BindingModels/MedicineBindingModel.cs b/VetClinic/VetClinicContracts/BindingModels/MedicineBindingModel.cs index 5abd45c..8f5ed54 100644 --- a/VetClinic/VetClinicContracts/BindingModels/MedicineBindingModel.cs +++ b/VetClinic/VetClinicContracts/BindingModels/MedicineBindingModel.cs @@ -11,7 +11,7 @@ namespace VetClinicContracts.BindingModels { public int Id { get; set; } public string MedicineName { get; set; } = string.Empty; - public int Price { get; set; } + public double Price { get; set; } public int PharmacistId { get; set; } public Dictionary MedicineAnimals { get; set; } = new(); } diff --git a/VetClinic/VetClinicContracts/BindingModels/ServiceBindingModel.cs b/VetClinic/VetClinicContracts/BindingModels/ServiceBindingModel.cs index 2ff2fd7..2b25dee 100644 --- a/VetClinic/VetClinicContracts/BindingModels/ServiceBindingModel.cs +++ b/VetClinic/VetClinicContracts/BindingModels/ServiceBindingModel.cs @@ -12,7 +12,7 @@ namespace VetClinicContracts.BindingModels { public int Id { get; set; } public string ServiceName { get; set; } = string.Empty; - public int Price { get; set; } + public double Price { get; set; } public int PharmacistId { get; set; } public Dictionary ServiceMedicines { get; set; } = new(); } diff --git a/VetClinic/VetClinicContracts/ViewModels/MedicineViewModel.cs b/VetClinic/VetClinicContracts/ViewModels/MedicineViewModel.cs index 87bdb71..d7c56f8 100644 --- a/VetClinic/VetClinicContracts/ViewModels/MedicineViewModel.cs +++ b/VetClinic/VetClinicContracts/ViewModels/MedicineViewModel.cs @@ -14,7 +14,7 @@ namespace VetClinicContracts.ViewModels [DisplayName("Название медикамента")] public string MedicineName { get; set; } = string.Empty; [DisplayName("Цена медикамента")] - public int Price { get; set; } + public double Price { get; set; } [DisplayName("Фармацевт")] public string PharmacistFIO { get; set; } = string.Empty; public int PharmacistId { get; set; } diff --git a/VetClinic/VetClinicContracts/ViewModels/ServiceViewModel.cs b/VetClinic/VetClinicContracts/ViewModels/ServiceViewModel.cs index 4d6ec24..356ac3d 100644 --- a/VetClinic/VetClinicContracts/ViewModels/ServiceViewModel.cs +++ b/VetClinic/VetClinicContracts/ViewModels/ServiceViewModel.cs @@ -14,7 +14,7 @@ namespace VetClinicContracts.ViewModels [DisplayName("Название услуги")] public string ServiceName { get; set; } = string.Empty; [DisplayName("Цена услуги")] - public int Price { get; set; } + public double Price { get; set; } [DisplayName("Фармацевт")] public string PharmacistFIO { get; set; } = string.Empty; public int PharmacistId { get; set; } diff --git a/VetClinic/VetClinicDataModels/Models/IMedicineModel.cs b/VetClinic/VetClinicDataModels/Models/IMedicineModel.cs index 8793843..941b542 100644 --- a/VetClinic/VetClinicDataModels/Models/IMedicineModel.cs +++ b/VetClinic/VetClinicDataModels/Models/IMedicineModel.cs @@ -9,7 +9,7 @@ namespace VetClinicDataModels.Models public interface IMedicineModel : IId { string MedicineName { get; } - int Price { get; } + double Price { get; } int PharmacistId { get; } Dictionary MedicineAnimals { get; } } diff --git a/VetClinic/VetClinicDataModels/Models/IServiceModel.cs b/VetClinic/VetClinicDataModels/Models/IServiceModel.cs index 28a7527..c4ce0ac 100644 --- a/VetClinic/VetClinicDataModels/Models/IServiceModel.cs +++ b/VetClinic/VetClinicDataModels/Models/IServiceModel.cs @@ -6,10 +6,10 @@ using System.Threading.Tasks; namespace VetClinicDataModels.Models { - public interface IServiceModel : IId + public interface IServiceModel : IId { string ServiceName { get; } - int Price { get; } + double Price { get; } int PharmacistId { get; } Dictionary ServiceMedicines { get; } } From 14a03218c089be6323856db395c9225d24cab2dc Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Wed, 24 Apr 2024 14:04:29 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/GuidanceStorage.cs | 79 +++++++++++++ .../Implements/MedicineStorage.cs | 105 +++++++++++++++++ .../Implements/PharmacistStorage.cs | 89 ++++++++++++++ .../Implements/ServiceStorage.cs | 110 ++++++++++++++++++ .../DinerDataBaseImplement/Models/Medicine.cs | 2 +- .../Models/Pharmacist.cs | 2 +- .../DinerDataBaseImplement/Models/Service.cs | 3 +- 7 files changed, 387 insertions(+), 3 deletions(-) create mode 100644 VetClinic/DinerDataBaseImplement/Implements/GuidanceStorage.cs create mode 100644 VetClinic/DinerDataBaseImplement/Implements/MedicineStorage.cs create mode 100644 VetClinic/DinerDataBaseImplement/Implements/PharmacistStorage.cs create mode 100644 VetClinic/DinerDataBaseImplement/Implements/ServiceStorage.cs diff --git a/VetClinic/DinerDataBaseImplement/Implements/GuidanceStorage.cs b/VetClinic/DinerDataBaseImplement/Implements/GuidanceStorage.cs new file mode 100644 index 0000000..19fbcdf --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Implements/GuidanceStorage.cs @@ -0,0 +1,79 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; +using VetClinicDataBaseImplement.Models; + +namespace VetClinicDataBaseImplement.Implements +{ + public class GuidanceStorage : IGuidanceStorage + { + public List GetFullList() + { + using var context = new VetClinicDatabase(); + return context.Guidances.Include(x => x.Service).Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(GuidanceSearchModel model) + { + + using var context = new VetClinicDatabase(); + return context.Guidances.Include(x => x.Service).Where(x => (!model.Id.HasValue || model.Id == x.Id) + && (!model.ServiceId.HasValue || model.ServiceId == x.ServiceId) && (!model.DateFrom.HasValue || model.DateFrom <= x.Date) + && (!model.DateTo.HasValue || model.DateTo >= x.Date)) + .Select(x => x.GetViewModel) + .ToList(); + } + public GuidanceViewModel? GetElement(GuidanceSearchModel model) + { + using var context = new VetClinicDatabase(); + return context.Guidances.Include(x => x.Service).FirstOrDefault(x => (!model.Id.HasValue || model.Id == x.Id) + && (!model.ServiceId.HasValue || model.ServiceId == x.ServiceId) && (!model.DateFrom.HasValue || model.DateFrom <= x.Date) + && (!model.DateTo.HasValue || model.DateTo >= x.Date))?.GetViewModel; + } + public GuidanceViewModel? Insert(GuidanceBindingModel model) + { + using var context = new VetClinicDatabase(); + var newGuidance = Guidance.Create(context, model); + if (newGuidance == null) + { + return null; + } + context.Guidances.Add(newGuidance); + context.SaveChanges(); + return newGuidance.GetViewModel; + } + public GuidanceViewModel? Update(GuidanceBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Guidances + .Include(x => x.Service) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Guidances.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public GuidanceViewModel? Delete(GuidanceBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Guidances.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Guidances.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/VetClinic/DinerDataBaseImplement/Implements/MedicineStorage.cs b/VetClinic/DinerDataBaseImplement/Implements/MedicineStorage.cs new file mode 100644 index 0000000..1f843dd --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Implements/MedicineStorage.cs @@ -0,0 +1,105 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; +using VetClinicDataBaseImplement.Models; + +namespace VetClinicDataBaseImplement.Implements +{ + public class MedicineStorage : IMedicineStorage + { + public List GetFullList() + { + 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(); + } + public List GetFilteredList(MedicineSearchModel model) + { + using var context = new VetClinicDatabase(); + return context.Medicines.Include(x => x.Pharmacist).Include(x => x.Animals) + .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(); + } + public MedicineViewModel? GetElement(MedicineSearchModel model) + { + if (string.IsNullOrEmpty(model.MedicineName) && + !model.Id.HasValue) + { + return null; + } + using var context = new VetClinicDatabase(); + return context.Medicines.Include(x => x.Pharmacist) + .Include(x => x.Animals) + .ThenInclude(x => x.Animal) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.MedicineName) && + x.MedicineName == model.MedicineName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public MedicineViewModel? Insert(MedicineBindingModel model) + { + using var context = new VetClinicDatabase(); + var newMedicine = Medicine.Create(context, model); + if (newMedicine == null) + { + return null; + } + context.Medicines.Add(newMedicine); + context.SaveChanges(); + return newMedicine.GetViewModel; + } + public MedicineViewModel? Update(MedicineBindingModel model) + { + using var context = new VetClinicDatabase(); + 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(); + medicine.UpdateAnimals(context, model); + transaction.Commit(); + return medicine.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public MedicineViewModel? Delete(MedicineBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Medicines + .Include(x => x.Animals).ThenInclude(x => x.Animal) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Medicines.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/VetClinic/DinerDataBaseImplement/Implements/PharmacistStorage.cs b/VetClinic/DinerDataBaseImplement/Implements/PharmacistStorage.cs new file mode 100644 index 0000000..f48bbbb --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Implements/PharmacistStorage.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; +using VetClinicDataBaseImplement.Models; + +namespace VetClinicDataBaseImplement.Implements +{ + public class PharmacistStorage : IPharmacistStorage + { + public List GetFullList() + { + using var context = new VetClinicDatabase(); + return context.Pharmacists + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PharmacistSearchModel model) + { + if (string.IsNullOrEmpty(model.PharmacistFIO) && string.IsNullOrEmpty(model.Email) && + string.IsNullOrEmpty(model.Password)) + { + return new(); + } + using var context = new VetClinicDatabase(); + return context.Pharmacists + .Where(x => (string.IsNullOrEmpty(model.PharmacistFIO) || x.PharmacistFIO.Contains(model.PharmacistFIO) && + string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email) && + string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password))) + .Select(x => x.GetViewModel) + .ToList(); + } + public PharmacistViewModel? GetElement(PharmacistSearchModel model) + { + if (string.IsNullOrEmpty(model.PharmacistFIO) && string.IsNullOrEmpty(model.Email) && + !model.Id.HasValue) + { + return null; + } + using var context = new VetClinicDatabase(); + return context.Pharmacists + .FirstOrDefault(x => (string.IsNullOrEmpty(model.PharmacistFIO) || x.PharmacistFIO == model.PharmacistFIO) && + (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password)) + ?.GetViewModel; + } + public PharmacistViewModel? Insert(PharmacistBindingModel model) + { + var newPharmacist = Pharmacist.Create(model); + if (newPharmacist == null) + { + return null; + } + using var context = new VetClinicDatabase(); + context.Pharmacists.Add(newPharmacist); + context.SaveChanges(); + return newPharmacist.GetViewModel; + } + public PharmacistViewModel? Update(PharmacistBindingModel model) + { + using var context = new VetClinicDatabase(); + var pharmacist = context.Pharmacists.FirstOrDefault(x => x.Id == model.Id); + if (pharmacist == null) + { + return null; + } + pharmacist.Update(model); + context.SaveChanges(); + return pharmacist.GetViewModel; + } + public PharmacistViewModel? Delete(PharmacistBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Pharmacists.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Pharmacists.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/VetClinic/DinerDataBaseImplement/Implements/ServiceStorage.cs b/VetClinic/DinerDataBaseImplement/Implements/ServiceStorage.cs new file mode 100644 index 0000000..19a6bb0 --- /dev/null +++ b/VetClinic/DinerDataBaseImplement/Implements/ServiceStorage.cs @@ -0,0 +1,110 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; +using VetClinicDataBaseImplement.Models; + +namespace VetClinicDataBaseImplement.Implements +{ + public class ServiceStorage : IServiceStorage + { + public List GetFullList() + { + using var context = new VetClinicDatabase(); + return context.Services.Include(x => x.Pharmacist) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ServiceSearchModel model) + { + using var context = new VetClinicDatabase(); + return context.Services.Include(x => x.Pharmacist) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .Where(x => (string.IsNullOrEmpty(model.ServiceName) || x.ServiceName.Contains(model.ServiceName) + && (!model.PharmacistId.HasValue || x.PharmacistId == model.PharmacistId))) + .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 VetClinicDatabase(); + return context.Services.Include(x => x.Pharmacist) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .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 VetClinicDatabase(); + 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 VetClinicDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var iceCream = context.Services.FirstOrDefault(rec => + rec.Id == model.Id); + if (iceCream == null) + { + return null; + } + iceCream.Update(model); + context.SaveChanges(); + iceCream.UpdateMedicines(context, model); + transaction.Commit(); + return iceCream.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ServiceViewModel? Delete(ServiceBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Services + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .ThenInclude(x => x.Animals).ThenInclude(x => x.Animal) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Services.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/VetClinic/DinerDataBaseImplement/Models/Medicine.cs b/VetClinic/DinerDataBaseImplement/Models/Medicine.cs index 662e0b9..91c993f 100644 --- a/VetClinic/DinerDataBaseImplement/Models/Medicine.cs +++ b/VetClinic/DinerDataBaseImplement/Models/Medicine.cs @@ -70,7 +70,7 @@ namespace VetClinicDataBaseImplement.Models MedicineAnimals = MedicineAnimals, PharmacistFIO = Pharmacist.PharmacistFIO }; - public void UpdateComponents(VetClinicDatabase context, + public void UpdateAnimals(VetClinicDatabase context, MedicineBindingModel model) { var medicineAnimals = context.MedicineAnimals.Where(rec => diff --git a/VetClinic/DinerDataBaseImplement/Models/Pharmacist.cs b/VetClinic/DinerDataBaseImplement/Models/Pharmacist.cs index 3f93d15..502cf68 100644 --- a/VetClinic/DinerDataBaseImplement/Models/Pharmacist.cs +++ b/VetClinic/DinerDataBaseImplement/Models/Pharmacist.cs @@ -62,7 +62,7 @@ namespace VetClinicDataBaseImplement.Models Email = model.Email; Password = model.Password; } - public PharmacistBindingModel GetViewModel => new() + public PharmacistViewModel GetViewModel => new() { Id = Id, PharmacistFIO = PharmacistFIO, diff --git a/VetClinic/DinerDataBaseImplement/Models/Service.cs b/VetClinic/DinerDataBaseImplement/Models/Service.cs index 2e01439..751400d 100644 --- a/VetClinic/DinerDataBaseImplement/Models/Service.cs +++ b/VetClinic/DinerDataBaseImplement/Models/Service.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using VetClinicDataModels.Models; using VetClinicContracts.BindingModels; using VetClinicContracts.ViewModels; +using Microsoft.EntityFrameworkCore; namespace VetClinicDataBaseImplement.Models { @@ -96,7 +97,7 @@ namespace VetClinicDataBaseImplement.Models context.ServiceMedicines.Add(new ServiceMedicine { Service = service, - Medicine = context.Medicines.First(x => x.Id == pc.Key), + Medicine = context.Medicines.Include(x => x.Animals).ThenInclude(x => x.Animal).First(x => x.Id == pc.Key), Count = pc.Value.Item2 }); context.SaveChanges();