From f933bb425821efe6eb9a36848aa3e9f57f8ffc23 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Wed, 24 Apr 2024 18:55:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B2=D1=81?= =?UTF-8?q?=D1=91,=20=D0=BA=D1=80=D0=BE=D0=BC=D0=B5=20=D0=98=D0=9D=D0=A2?= =?UTF-8?q?=D0=95=D0=A0=D0=A4=D0=95=D0=99=D0=A1=D0=90=20=D0=9F=D0=9E=D0=9B?= =?UTF-8?q?=D0=AC=D0=97=D0=9E=D0=92=D0=90=D0=A2=D0=95=D0=9B=D0=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/AnimalLogic.cs | 10 +- .../BusinessLogics/VaccinationLogic.cs | 3 +- .../BusinessLogics/VisitLogic.cs | 118 +++++++++++++++ .../BindingModels/AnimalBindingModel.cs | 3 + .../SearchModels/AnimalSearchModel.cs | 1 + .../SearchModels/VisitSearchModel.cs | 2 + .../StoragesContracts/IVisitStorage.cs | 2 +- .../ViewModels/AnimalViewModel.cs | 3 +- .../ViewModels/VisitViewModel.cs | 4 +- .../Implements/AnimalStorage.cs | 12 +- .../Implements/GuidanceStorage.cs | 79 ++++++++++ .../Implements/MedicineStorage.cs | 105 +++++++++++++ .../Implements/PharmacistStorage.cs | 89 +++++++++++ .../Implements/ServiceStorage.cs | 110 ++++++++++++++ .../Implements/VisitStorage.cs | 120 +++++++++++++++ .../Models/Animal.cs | 64 +++++++- .../Models/Guidance.cs | 58 ++++++++ .../Models/Medicine.cs | 98 ++++++++++++ .../Models/MedicineAnimal.cs | 21 +++ .../Models/Pharmacist.cs | 73 +++++++++ .../Models/Service.cs | 108 ++++++++++++++ .../Models/ServiceMedicine.cs | 23 +++ .../Models/ServiceVisit.cs | 4 +- .../Models/Visit.cs | 139 ++++++++++++++++-- .../VetClinicDatabase .cs | 1 + .../Models/IAnimalModel.cs | 4 +- .../VetClinicDataModels/Models/IVisitModel.cs | 1 - 27 files changed, 1217 insertions(+), 38 deletions(-) create mode 100644 VetClinic/VetClinicBusinessLogic/BusinessLogics/VisitLogic.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Implements/GuidanceStorage.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Implements/MedicineStorage.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Implements/PharmacistStorage.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Implements/ServiceStorage.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Implements/VisitStorage.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Models/Guidance.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Models/Medicine.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Models/MedicineAnimal.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Models/Pharmacist.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Models/Service.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Models/ServiceMedicine.cs diff --git a/VetClinic/VetClinicBusinessLogic/BusinessLogics/AnimalLogic.cs b/VetClinic/VetClinicBusinessLogic/BusinessLogics/AnimalLogic.cs index bbf3a17..5a9eecf 100644 --- a/VetClinic/VetClinicBusinessLogic/BusinessLogics/AnimalLogic.cs +++ b/VetClinic/VetClinicBusinessLogic/BusinessLogics/AnimalLogic.cs @@ -11,10 +11,10 @@ namespace VetClinicBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IAnimalStorage _animalStorage; - public AnimalLogic(ILogger logger, IAnimalStorage orderStorage) + public AnimalLogic(ILogger logger, IAnimalStorage animalStorage) { _logger = logger; - _animalStorage = orderStorage; + _animalStorage = animalStorage; } public List? ReadList(AnimalSearchModel? model) { @@ -90,10 +90,6 @@ namespace VetClinicBusinessLogic.BusinessLogics { return; } - //if (model.VaccitinationId < 0) - //{ - // throw new ArgumentNullException("Неверный идентификатор прививки", nameof(model.VaccitinationId)); - //} if (model.AnimalName == "") { throw new ArgumentNullException("Имя не должно быть пустым", nameof(model.AnimalName)); @@ -102,7 +98,7 @@ namespace VetClinicBusinessLogic.BusinessLogics { throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.AdminId)); } - //_logger.LogInformation("Animal. VaccinationId: {VaccinationId}. AnimalName: {AnimalName}. Family: {Family}. Id: {Id}. AdminId: {AdminId}. VisitId: {VisitId}", model.VaccinationId, model.AnimalName, model.Family, model.Id, model.AdminId, model.VisitId); + var element = _animalStorage.GetElement(new AnimalSearchModel { Id = model.Id diff --git a/VetClinic/VetClinicBusinessLogic/BusinessLogics/VaccinationLogic.cs b/VetClinic/VetClinicBusinessLogic/BusinessLogics/VaccinationLogic.cs index 95af9e0..2adcbd4 100644 --- a/VetClinic/VetClinicBusinessLogic/BusinessLogics/VaccinationLogic.cs +++ b/VetClinic/VetClinicBusinessLogic/BusinessLogics/VaccinationLogic.cs @@ -5,13 +5,14 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using VetClinicContracts.BindingModels; +using VetClinicContracts.BusinessLogicsContracts; using VetClinicContracts.SearchModels; using VetClinicContracts.StoragesContracts; using VetClinicContracts.ViewModels; namespace VetClinicBusinessLogic.BusinessLogics { - public class VaccinationLogic + public class VaccinationLogic : IVaccinationLogic { private readonly ILogger _logger; private readonly IVaccinationStorage _vaccinationStorage; diff --git a/VetClinic/VetClinicBusinessLogic/BusinessLogics/VisitLogic.cs b/VetClinic/VetClinicBusinessLogic/BusinessLogics/VisitLogic.cs new file mode 100644 index 0000000..f8b959d --- /dev/null +++ b/VetClinic/VetClinicBusinessLogic/BusinessLogics/VisitLogic.cs @@ -0,0 +1,118 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.BusinessLogicsContracts; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; + +namespace VetClinicBusinessLogic.BusinessLogics +{ + public class VisitLogic : IVisitLogic + { + private readonly ILogger _logger; + private readonly IVisitStorage _visitStorage; + public VisitLogic(ILogger logger, IVisitStorage visitStorage) + { + _logger = logger; + _visitStorage = visitStorage; + } + public List? ReadList(VisitSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _visitStorage.GetFullList() : _visitStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + return list; + } + public VisitViewModel? ReadElement(VisitSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadList. NameVisit:{NameVisit}. DateVisit {DateVisit}. Id:{Id}", model.NameVisit, model.DateVisit, model?.Id); + + var element = _visitStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(VisitBindingModel model) + { + CheckModel(model); + + if (_visitStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(VisitBindingModel model) + { + CheckModel(model); + if (_visitStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(VisitBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_visitStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(VisitBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.NameVisit == "") + { + throw new ArgumentNullException("Имя не должно быть пустым", nameof(model.NameVisit)); + } + if (model.AdminId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.AdminId)); + } + if (model.DateVisit == new DateTime()) + { + throw new ArgumentNullException("Укажите Время", nameof(model.DateVisit)); + } + + var element = _visitStorage.GetElement(new VisitSearchModel + { + Id = model.Id + }); + + } + } +} diff --git a/VetClinic/VetClinicContracts/BindingModels/AnimalBindingModel.cs b/VetClinic/VetClinicContracts/BindingModels/AnimalBindingModel.cs index 856e1d3..5515709 100644 --- a/VetClinic/VetClinicContracts/BindingModels/AnimalBindingModel.cs +++ b/VetClinic/VetClinicContracts/BindingModels/AnimalBindingModel.cs @@ -11,7 +11,10 @@ namespace VetClinicContracts.BindingModels { public int Id { get; set; } public int AdminId { get; set; } + public int VaccinationId { get; set; } public Dictionary VisitAnimals { get; set; } = new(); + + public Dictionary MedicineAnimals { get; set; } = new(); public string AnimalName { get; set; } = string.Empty; public string? Family { get; set; } = string.Empty; diff --git a/VetClinic/VetClinicContracts/SearchModels/AnimalSearchModel.cs b/VetClinic/VetClinicContracts/SearchModels/AnimalSearchModel.cs index 86a21fe..aabd0bb 100644 --- a/VetClinic/VetClinicContracts/SearchModels/AnimalSearchModel.cs +++ b/VetClinic/VetClinicContracts/SearchModels/AnimalSearchModel.cs @@ -13,6 +13,7 @@ namespace VetClinicContracts.SearchModels public int? AdminId { get; set; } public Dictionary Visits { get; set; } = new(); + public Dictionary Medicines { get; set; } = new(); public string? AnimalName { get; set; } diff --git a/VetClinic/VetClinicContracts/SearchModels/VisitSearchModel.cs b/VetClinic/VetClinicContracts/SearchModels/VisitSearchModel.cs index 2754ad9..483f842 100644 --- a/VetClinic/VetClinicContracts/SearchModels/VisitSearchModel.cs +++ b/VetClinic/VetClinicContracts/SearchModels/VisitSearchModel.cs @@ -12,6 +12,8 @@ namespace VetClinicContracts.SearchModels public int? AdminId { get; set; } public string? NameVisit { get; set; } + + public string? DateVisit { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/VetClinic/VetClinicContracts/StoragesContracts/IVisitStorage.cs b/VetClinic/VetClinicContracts/StoragesContracts/IVisitStorage.cs index d77be25..1cb302a 100644 --- a/VetClinic/VetClinicContracts/StoragesContracts/IVisitStorage.cs +++ b/VetClinic/VetClinicContracts/StoragesContracts/IVisitStorage.cs @@ -15,7 +15,7 @@ namespace VetClinicContracts.StoragesContracts List GetFilteredList(VisitSearchModel model); VisitViewModel? GetElement(VisitSearchModel model); VisitViewModel? Insert(VisitBindingModel model); - VisitViewModel? Update(VaccinationBindingModel model); + VisitViewModel? Update(VisitBindingModel model); VisitViewModel? Delete(VisitBindingModel model); } } diff --git a/VetClinic/VetClinicContracts/ViewModels/AnimalViewModel.cs b/VetClinic/VetClinicContracts/ViewModels/AnimalViewModel.cs index 73a5ad4..a244d0c 100644 --- a/VetClinic/VetClinicContracts/ViewModels/AnimalViewModel.cs +++ b/VetClinic/VetClinicContracts/ViewModels/AnimalViewModel.cs @@ -12,8 +12,9 @@ namespace VetClinicContracts.ViewModels { public int Id { get; set; } public int AdminId { get; set; } - + public int VaccinationId { get; set; } public Dictionary VisitAnimals { get; set; } = new(); + public Dictionary MedicineAnimals { get; set; } = new(); [DisplayName("Имя животного")] public string AnimalName { get; set; } = string.Empty; diff --git a/VetClinic/VetClinicContracts/ViewModels/VisitViewModel.cs b/VetClinic/VetClinicContracts/ViewModels/VisitViewModel.cs index 73a1bab..171b7c0 100644 --- a/VetClinic/VetClinicContracts/ViewModels/VisitViewModel.cs +++ b/VetClinic/VetClinicContracts/ViewModels/VisitViewModel.cs @@ -12,8 +12,8 @@ namespace VetClinicContracts.ViewModels { [DisplayName("Номер")] public int Id { get; set; } - public int ServiceId { get; set; } - public int AdminId { get; set; } + + public int AdminId { get; set; } [DisplayName("Животное")] public string AnimalName { get; set; } = string.Empty; diff --git a/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs index e6c7cd0..5114525 100644 --- a/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs +++ b/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs @@ -15,7 +15,7 @@ namespace VetClinicBaseImplement.Implements using var context = new VetClinicDatabase(); return context.Animals - //.Include(x => x.Vaccination) + .Include(x => x.Vaccination) .Include(x => x.Admin) .Include(x => x.Visits) .ThenInclude(x => x.Visit) @@ -32,6 +32,7 @@ namespace VetClinicBaseImplement.Implements using var context = new VetClinicDatabase(); return context.Animals .Include(x => x.Admin) + .Include(x => x.Vaccination) .Include(x => x.Visits) .ThenInclude(x => x.Visit) .Where(x => x.AnimalName.Contains(model.AnimalName)) @@ -47,9 +48,10 @@ namespace VetClinicBaseImplement.Implements using var context = new VetClinicDatabase(); return context.Animals .Include(x => x.Admin) + .Include(x => x.Vaccination) .Include(x => x.Visits) .ThenInclude(x => x.Visit) - //.Include(x => x.Vaccination) + .Include(x => x.Vaccination) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.AnimalName) && x.AnimalName == model.AnimalName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public AnimalViewModel? Insert(AnimalBindingModel model) @@ -92,9 +94,9 @@ namespace VetClinicBaseImplement.Implements { using var context = new VetClinicDatabase(); var element = context.Animals - //.Include(x => x.Visit) - .Include(x => x.Admin) - //.Include(x => x.Vaccination) + .Include(x => x.Visits) + .ThenInclude(x => x.Visit) + .Include(x => x.Vaccinations) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) diff --git a/VetClinic/VetClinicDataBaseImplement/Implements/GuidanceStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/GuidanceStorage.cs new file mode 100644 index 0000000..19fbcdf --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/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/VetClinicDataBaseImplement/Implements/MedicineStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/MedicineStorage.cs new file mode 100644 index 0000000..1f843dd --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/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/VetClinicDataBaseImplement/Implements/PharmacistStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/PharmacistStorage.cs new file mode 100644 index 0000000..f48bbbb --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/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/VetClinicDataBaseImplement/Implements/ServiceStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/ServiceStorage.cs new file mode 100644 index 0000000..19a6bb0 --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/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/VetClinicDataBaseImplement/Implements/VisitStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/VisitStorage.cs new file mode 100644 index 0000000..417bb8b --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/Implements/VisitStorage.cs @@ -0,0 +1,120 @@ +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.ViewModels; +using VetClinicDataBaseImplement.Models; + +namespace VetClinicDataBaseImplement.Implements +{ + public class VisitStorage + { + public List GetFullList() + { + using var context = new VetClinicDatabase(); + + return context.Visits + + .Include(x => x.Admin) + .Include(x => x.Animals) + .ThenInclude(x => x.Animal) + .Include(x => x.Services) + .ThenInclude(x => x.Service) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(VisitSearchModel model) + { + + if (string.IsNullOrEmpty(model.NameVisit)) + { + return new(); + } + using var context = new VetClinicDatabase(); + return context.Visits + .Include(x => x.Admin) + .Include(x => x.Animals) + .ThenInclude(x => x.Animal) + .Where(x => x.NameVisit.Contains(model.NameVisit)) + .Include(x => x.Services) + .ThenInclude(x => x.Service) + .Where(x => x.NameVisit.Contains(model.NameVisit)) + .ToList() + .Select(x => x.GetViewModel).ToList(); + } + public VisitViewModel? GetElement(VisitSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new VetClinicDatabase(); + return context.Visits + .Include(x => x.Admin) + .Include(x => x.Animals) + .ThenInclude(x => x.Animal) + .Include(x => x.Services) + .ThenInclude(x => x.Service) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.NameVisit) && x.NameVisit == model.NameVisit) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public VisitViewModel? Insert(VisitBindingModel model) + { + using var context = new VetClinicDatabase(); + var newVisit = Visit.Create(context, model); + if (newVisit == null) + { + return null; + } + context.Visits.Add(newVisit); + context.SaveChanges(); + return newVisit.GetViewModel; + } + public VisitViewModel? Update(VisitBindingModel model) + { + using var context = new VetClinicDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var visit = context.Visits.FirstOrDefault(rec => rec.Id == model.Id); + if (visit == null) + { + return null; + } + visit.Update(model); + context.SaveChanges(); + visit.UpdateAnimals(context, model); + transaction.Commit(); + return visit.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public VisitViewModel? Delete(VisitBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Visits + .Include(x => x.Animals).ThenInclude(x => x.Animal) + .Include(x => x.Services).ThenInclude(x => x.Service) + .FirstOrDefault(rec => rec.Id == model.Id); + + + if (element != null) + { + + context.Visits.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} +} diff --git a/VetClinic/VetClinicDataBaseImplement/Models/Animal.cs b/VetClinic/VetClinicDataBaseImplement/Models/Animal.cs index 0189201..7c37bf3 100644 --- a/VetClinic/VetClinicDataBaseImplement/Models/Animal.cs +++ b/VetClinic/VetClinicDataBaseImplement/Models/Animal.cs @@ -18,20 +18,32 @@ namespace VetClinicDataBaseImplement.Models public int Id { get; set; } [Required] public int AdminId { get; set; } + [Required] + public int VaccinationId { get; set; } + [Required] public Dictionary? _visitAnimals = null; [Required] + public Dictionary? _medicineAnimals = null; + [Required] public string AnimalName { get; set; } = string.Empty; [Required] public string Family { get; set; } = string.Empty; + [ForeignKey("AnimalId")] - public virtual List Visits { get; set; } = new(); - //[ForeignKey("AnimalId")] - //public virtual List Vaccinations { get; set; } = new(); + public virtual List Vaccinations { get; set; } = new(); public virtual Admin Admin { get; set; } + public virtual Vaccination Vaccination { get; set; } + + [ForeignKey("AnimalId")] + public virtual List Visits { get; set; } = new(); + + + [ForeignKey("AnimalId")] + public virtual List Medicines { get; set; } = new(); [NotMapped] public Dictionary VisitAnimals { @@ -40,11 +52,23 @@ namespace VetClinicDataBaseImplement.Models if (_visitAnimals == null) { _visitAnimals = Visits.ToDictionary(recPC => recPC.VisitId, recPC => - recPC.Animal as IVisitModel); + recPC.Visit as IVisitModel); } return _visitAnimals; } } + public Dictionary MedicineAnimals + { + get + { + if (_medicineAnimals == null) + { + _medicineAnimals = Medicines.ToDictionary(recPC => recPC.MedicineId, recPC => + recPC.Medicine as IMedicineModel); + } + return _medicineAnimals; + } + } public static Animal? Create(VetClinicDatabase context, AnimalBindingModel? model) { if (model == null) @@ -55,14 +79,17 @@ namespace VetClinicDataBaseImplement.Models { Id = model.Id, AdminId = model.AdminId, - //VaccinationId = model.VaccinationId + VaccinationId = model.VaccinationId, AnimalName = model.AnimalName, Family = model.Family, Visits = model.VisitAnimals.Select(x => new VisitAnimal { Visit = context.Visits.First(y => y.Id == x.Key) }).ToList(), - + Medicines = model.MedicineAnimals.Select(x => new MedicineAnimal + { + Medicine = context.Medicines.First(y => y.Id == x.Key) + }).ToList(), }; } @@ -80,8 +107,9 @@ namespace VetClinicDataBaseImplement.Models Id = Id, VisitAnimals = VisitAnimals, AdminId = AdminId, - //VaccinationId = VaccinationId + VaccinationId = VaccinationId, AnimalName = AnimalName, + MedicineAnimals = MedicineAnimals, Family = Family }; @@ -107,5 +135,27 @@ namespace VetClinicDataBaseImplement.Models } _visitAnimals = null; } + public void UpdateMedicines(VetClinicDatabase context, AnimalBindingModel model) + { + var medicineAnimals = context.MedicineAnimals.Where(rec => rec.AnimalId == model.Id).ToList(); + if (medicineAnimals != null) + { // удалили те, которых нет в модели + context.MedicineAnimals.RemoveRange(medicineAnimals.Where(rec => !model.MedicineAnimals.ContainsKey(rec.MedicineId))); + context.SaveChanges(); + + } + var animal = context.Animals.First(x => x.Id == Id); + foreach (var pc in model.VisitAnimals) + { + context.MedicineAnimals.Add(new MedicineAnimal + { + Animal = animal, + Medicine = context.Medicines.First(x => x.Id == pc.Key), + + }); + context.SaveChanges(); + } + _medicineAnimals = null; + } } } diff --git a/VetClinic/VetClinicDataBaseImplement/Models/Guidance.cs b/VetClinic/VetClinicDataBaseImplement/Models/Guidance.cs new file mode 100644 index 0000000..e8c68ad --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/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/VetClinicDataBaseImplement/Models/Medicine.cs b/VetClinic/VetClinicDataBaseImplement/Models/Medicine.cs new file mode 100644 index 0000000..91c993f --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/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 UpdateAnimals(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/VetClinicDataBaseImplement/Models/MedicineAnimal.cs b/VetClinic/VetClinicDataBaseImplement/Models/MedicineAnimal.cs new file mode 100644 index 0000000..22d06e3 --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/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/VetClinicDataBaseImplement/Models/Pharmacist.cs b/VetClinic/VetClinicDataBaseImplement/Models/Pharmacist.cs new file mode 100644 index 0000000..502cf68 --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/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 PharmacistViewModel GetViewModel => new() + { + Id = Id, + PharmacistFIO = PharmacistFIO, + Email = Email, + Password = Password + }; + } +} diff --git a/VetClinic/VetClinicDataBaseImplement/Models/Service.cs b/VetClinic/VetClinicDataBaseImplement/Models/Service.cs new file mode 100644 index 0000000..751400d --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/Models/Service.cs @@ -0,0 +1,108 @@ +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; +using Microsoft.EntityFrameworkCore; + +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.Include(x => x.Animals).ThenInclude(x => x.Animal).First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _serviceMedicines = null; + } + } +} diff --git a/VetClinic/VetClinicDataBaseImplement/Models/ServiceMedicine.cs b/VetClinic/VetClinicDataBaseImplement/Models/ServiceMedicine.cs new file mode 100644 index 0000000..5e3b3d0 --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/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/VetClinicDataBaseImplement/Models/ServiceVisit.cs b/VetClinic/VetClinicDataBaseImplement/Models/ServiceVisit.cs index f871a86..4d53dcc 100644 --- a/VetClinic/VetClinicDataBaseImplement/Models/ServiceVisit.cs +++ b/VetClinic/VetClinicDataBaseImplement/Models/ServiceVisit.cs @@ -7,11 +7,11 @@ using System.Threading.Tasks; namespace VetClinicDataBaseImplement.Models { - public class ServiceVisit : + public class ServiceVisit { public int Id { get; set; } [Required] - public int ServicelId { get; set; } + public int ServiceId { get; set; } [Required] public int VisitId { get; set; } diff --git a/VetClinic/VetClinicDataBaseImplement/Models/Visit.cs b/VetClinic/VetClinicDataBaseImplement/Models/Visit.cs index fb15682..6516c87 100644 --- a/VetClinic/VetClinicDataBaseImplement/Models/Visit.cs +++ b/VetClinic/VetClinicDataBaseImplement/Models/Visit.cs @@ -15,18 +15,137 @@ namespace VetClinicDataBaseImplement.Models { public class Visit : IVisitModel { - public int Id { get; private set; } + public int Id { get; set; } + [Required] + public int AdminId { get; set; } + [Required] + public Dictionary? _visitAnimals = null; + [Required] + public Dictionary? _serviceVisits = null; + [Required] + public string NameVisit { get; set; } = string.Empty; [Required] - public int AnimalId { get; private set; } - public virtual Animal Animal { get; private set; } + public DateTime DateVisit { get; set; } - public virtual Service Service { get; private set; } - [Required] - public string NameVisit { get; private set; } = string.Empty; - [Required] - public double CostVaccination { get; private set; } - [Required] - public DateTime? DateStamp { get; private set; } + [ForeignKey("VisitId")] + public virtual List Animals { get; set; } = new(); + + [ForeignKey("VisitId")] + public virtual List Services { get; set; } = new(); + + public virtual Admin Admin { get; set; } + + public Dictionary VisitAnimals + { + get + { + if (_visitAnimals == null) + { + _visitAnimals = Animals.ToDictionary(recPC => recPC.AnimalId, recPC => + recPC.Animal as IAnimalModel); + } + return _visitAnimals; + } + } + public Dictionary ServiceVisits + { + get + { + if (_serviceVisits == null) + { + _serviceVisits = Services.ToDictionary(recPC => recPC.ServiceId, recPC => + recPC.Service as IServiceModel); + } + return _serviceVisits; + } + } + public static Visit? Create(VetClinicDatabase context, VisitBindingModel? model) + { + if (model == null) + { + return null; + } + return new Visit() + { + Id = model.Id, + AdminId = model.AdminId, + + NameVisit = model.NameVisit, + DateVisit = model.DateVisit, + Animals = model.VisitAnimals.Select(x => new VisitAnimal + { + Animal = context.Animals.First(y => y.Id == x.Key) + }).ToList(), + Services = model.ServiceVisits.Select(x => new ServiceVisit + { + Service = context.Services.First(y => y.Id == x.Key) + }).ToList(), + }; + } + public void Update(VisitBindingModel? model) + { + if (model == null) + { + return; + } + NameVisit = model.NameVisit; + DateVisit = model.DateVisit; + } + public VisitViewModel GetViewModel => new() + { + Id = Id, + VisitAnimals = VisitAnimals, + AdminId = AdminId, + NameVisit = NameVisit, + DateVisit = DateVisit, + ServiceVisits = ServiceVisits + + }; + public void UpdateAnimals(VetClinicDatabase context, VisitBindingModel model) + { + var visitAnimals = context.VisitAnimals.Where(rec => rec.VisitId == model.Id).ToList(); + if (visitAnimals != null) + { // удалили те, которых нет в модели + context.VisitAnimals.RemoveRange(visitAnimals.Where(rec => !model.VisitAnimals.ContainsKey(rec.AnimalId))); + context.SaveChanges(); + + } + var visit = context.Visits.First(x => x.Id == Id); + foreach (var pc in model.VisitAnimals) + { + context.VisitAnimals.Add(new VisitAnimal + { + Visit = visit, + Animal = context.Animals.First(x => x.Id == pc.Key), + + }); + context.SaveChanges(); + } + _visitAnimals = null; + } + public void UpdateService(VetClinicDatabase context, VisitBindingModel model) + { + var serviceVisits = context.ServiceVisits.Where(rec => rec.VisitId == model.Id).ToList(); + if (serviceVisits != null) + { // удалили те, которых нет в модели + context.ServiceVisits.RemoveRange(serviceVisits.Where(rec => !model.ServiceVisits.ContainsKey(rec.ServiceId))); + context.SaveChanges(); + + } + var visit = context.Visits.First(x => x.Id == Id); + foreach (var pc in model.ServiceVisits) + { + context.ServiceVisits.Add(new ServiceVisit + { + Visit = visit, + Service = context.Services.First(x => x.Id == pc.Key), + + }); + context.SaveChanges(); + } + _serviceVisits = null; + } } + } diff --git a/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs b/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs index 2e70239..c490c60 100644 --- a/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs +++ b/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs @@ -18,6 +18,7 @@ namespace VetClinicDataBaseImplement public virtual DbSet Animals { set; get; } public virtual DbSet Admins { set; get; } public virtual DbSet VisitAnimals { set; get; } + public virtual DbSet ServiceVisits { set; get; } public virtual DbSet Visits { set; get; } public virtual DbSet Vaccinations { set; get; } public virtual DbSet Pharmacists { set; get; } diff --git a/VetClinic/VetClinicDataModels/Models/IAnimalModel.cs b/VetClinic/VetClinicDataModels/Models/IAnimalModel.cs index 0867340..6484cc5 100644 --- a/VetClinic/VetClinicDataModels/Models/IAnimalModel.cs +++ b/VetClinic/VetClinicDataModels/Models/IAnimalModel.cs @@ -10,7 +10,9 @@ namespace VetClinicDataModels.Models { int AdminId { get; } Dictionary VisitAnimals { get; } - string AnimalName { get; } + + Dictionary MedicineAnimals { get; } + string AnimalName { get; } string? Family { get; } } } diff --git a/VetClinic/VetClinicDataModels/Models/IVisitModel.cs b/VetClinic/VetClinicDataModels/Models/IVisitModel.cs index 0c5d8e4..d569ba5 100644 --- a/VetClinic/VetClinicDataModels/Models/IVisitModel.cs +++ b/VetClinic/VetClinicDataModels/Models/IVisitModel.cs @@ -9,7 +9,6 @@ namespace VetClinicDataModels.Models public interface IVisitModel : IId { int Id { get;} - int ServiceId { get; } int AdminId { get; } string NameVisit { get;} DateTime DateVisit { get; }