diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/OwnerLogic.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/OwnerLogic.cs index e741d1e..31f8617 100644 --- a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/OwnerLogic.cs +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/OwnerLogic.cs @@ -92,7 +92,7 @@ namespace VeterinaryBusinessLogic.BusinessLogic } if (string.IsNullOrEmpty(model.Login)) { - throw new ArgumentNullException("Нет Email клиента", + throw new ArgumentNullException("Нет Login клиента", nameof(model.Login)); } if (string.IsNullOrEmpty(model.Password)) diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Models/Doctor.cs b/VeterinaryView/VeterinaryDatabaseImplement/Models/Doctor.cs new file mode 100644 index 0000000..de79787 --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Models/Doctor.cs @@ -0,0 +1,71 @@ +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 VeterinaryDataModels; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryDatabaseImplement.Models +{ + public class Doctor : IDoctorModel + { + public int Id { get; private set; } + [Required] + public string DoctorFIO { get; private set; } = string.Empty; + [Required] + public string Login { get; set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [ForeignKey("DoctorId")] + public virtual List Services { get; set; } = new(); + [ForeignKey("DoctorId")] + public virtual List Medications { get; set; } = new(); + [ForeignKey("DoctorId")] + public virtual List Visits { get; set; } = new(); + public static Doctor? Create(DoctorBindingModel model) + { + if (model == null) + { + return null; + } + return new Doctor() + { + Id = model.Id, + DoctorFIO = model.DoctorFIO, + Login = model.Login, + Password = model.Password + }; + } + public static Doctor Create(DoctorViewModel model) + { + return new Doctor() + { + Id = model.Id, + DoctorFIO = model.DoctorFIO, + Login = model.Login, + Password = model.Password + }; + } + public void Update(DoctorBindingModel model) + { + if (model == null) + { + return; + } + DoctorFIO = model.DoctorFIO; + Login = model.Login; + Password = model.Password; + } + public DoctorViewModel GetViewModel => new() + { + Id = Id, + DoctorFIO = DoctorFIO, + Login = Login, + Password = Password + }; + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Models/Drug.cs b/VeterinaryView/VeterinaryDatabaseImplement/Models/Drug.cs new file mode 100644 index 0000000..fd6faf0 --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Models/Drug.cs @@ -0,0 +1,99 @@ +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 VeterinaryDataModels; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryDatabaseImplement.Models +{ + public class Drug : IDrugModel + { + public int Id { get; set; } + [Required] + public string DrugName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + [Required] + public int Count { get; set; } + private Dictionary? _drugMedications = null; + [NotMapped] + public Dictionary DrugMedications + { + get + { + if (_drugMedications == null) + { + _drugMedications = Medications + .ToDictionary(recPC => recPC.MedicationId, recPC => + (recPC.Medication as IMedicationModel, recPC.Count)); + } + return _drugMedications; + } + } + [ForeignKey("DrugId")] + public virtual List Medications { get; set; } = new(); + [ForeignKey("DrugId")] + public virtual List Purchases { get; set; } = new(); + public static Drug Create(VeterinaryDataBase context, DrugBindingModel model) + { + return new Drug() + { + Id = model.Id, + DrugName = model.DrugName, + Price = model.Price, + Count = model.Count, + Medications = model.DrugMedications.Select(x => new DrugMedication + { + Medication = context.Medications.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(DrugBindingModel model) + { + DrugName = model.DrugName; + Price = model.Price; + } + public DrugViewModel GetViewModel => new() + { + Id = Id, + DrugName = DrugName, + Price = Price, + DrugMedications = DrugMedications + }; + public void UpdateMedications(VeterinaryDataBase context, DrugBindingModel model) + { + var drugMedications = context.DrugMedications.Where(rec => rec.DrugId == model.Id).ToList(); + if (drugMedications != null && drugMedications.Count > 0) + { + context.DrugMedications.RemoveRange(drugMedications.Where(rec => !model.DrugMedications.ContainsKey(rec.MedicationId))); + context.SaveChanges(); + foreach (var updateMedication in drugMedications) + { + updateMedication.Count = + model.DrugMedications[updateMedication.MedicationId].Item2; + model.DrugMedications.Remove(updateMedication.MedicationId); + } + context.SaveChanges(); + } + var drug = context.Drugs.First(x => x.Id == Id); + foreach (var pc in model.DrugMedications) + { + context.DrugMedications.Add(new DrugMedication + { + Drug = drug, + Medication = context.Medications.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _drugMedications = null; + } + + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Models/DrugMedication.cs b/VeterinaryView/VeterinaryDatabaseImplement/Models/DrugMedication.cs new file mode 100644 index 0000000..bf2c99d --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Models/DrugMedication.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 VeterinaryDatabaseImplement.Models +{ + public class DrugMedication + { + public int Id { get; set; } + [Required] + public int DrugId { get; set; } + [Required] + public int MedicationId { get; set; } + [Required] + public int Count { get; set; } + public virtual Medication Medication { get; set; } = new(); + public virtual Drug Drug { get; set; } = new(); + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Models/Medication.cs b/VeterinaryView/VeterinaryDatabaseImplement/Models/Medication.cs new file mode 100644 index 0000000..415fc81 --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Models/Medication.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryDataModels; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryDatabaseImplement.Models +{ + public class Medication : IMedicationModel + { + public int Id { get; private set; } + [Required] + public string MedicationName { get; private set; } = string.Empty; + [Required] + public double Price { get; set; } + [Required] + public int DoctorId { get; private set; } + [ForeignKey("MedicationId")] + public virtual List ServiceMedications { get; set; } = new(); + [ForeignKey("MedicationId")] + public virtual List DrugMedications { get; set; } = new(); + + public static Medication? Create(MedicationBindingModel model) + { + if (model == null) + { + return null; + } + return new Medication() + { + Id = model.Id, + MedicationName = model.MedicationName, + DoctorId = model.DoctorId, + Price = model.Price + }; + } + public static Medication Create(MedicationViewModel model) + { + return new Medication + { + Id = model.Id, + MedicationName = model.MedicationName, + DoctorId = model.DoctorId, + Price = model.Price + }; + } + public void Update(MedicationBindingModel model) + { + if (model == null) + { + return; + } + MedicationName = model.MedicationName; + Price = model.Price; + } + public MedicationViewModel GetViewModel => new() + { + Id = Id, + MedicationName = MedicationName, + Price = Price, + DoctorId = DoctorId + }; + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Models/Service.cs b/VeterinaryView/VeterinaryDatabaseImplement/Models/Service.cs new file mode 100644 index 0000000..60c4938 --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Models/Service.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryDataModels; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryDatabaseImplement.Models +{ + public class Service : IServiceModel + { + public int Id { get; set; } + [Required] + public string ServiceName { get; set; } = string.Empty; + [Required] + public int DoctorId { get; private set; } + [Required] + public int VisitId { get; private set; } + public virtual Doctor? Doctor { get; private set; } + private Dictionary? _serviceMedications = null; + [NotMapped] + public Dictionary ServiceMedications + { + get + { + if (_serviceMedications == null) + { + _serviceMedications = Medications + .ToDictionary(recPC => recPC.MedicationId, recPC => + (recPC.Medication as IMedicationModel, recPC.Count)); + } + return _serviceMedications; + } + } + [ForeignKey("ServiceId")] + public virtual List Medications { get; set; } = new(); + public static Service Create(VeterinaryDataBase context, ServiceBindingModel model) + { + return new Service() + { + Id = model.Id, + ServiceName = model.ServiceName, + DoctorId = model.DoctorId, + VisitId = model.VisitId, + Medications = model.ServiceMedications.Select(x => new ServiceMedication + { + Medication = context.Medications.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ServiceBindingModel model) + { + ServiceName = model.ServiceName; + } + public ServiceViewModel GetViewModel => new() + { + Id = Id, + ServiceName = ServiceName, + DoctorId = DoctorId, + VisitId = VisitId, + ServiceMedications = ServiceMedications + }; + + public void UpdateMedications(VeterinaryDataBase context, ServiceBindingModel model) + { + var serviceMedications = context.ServiceMedications.Where(rec => rec.ServiceId == model.Id).ToList(); + if (serviceMedications != null && serviceMedications.Count > 0) + { + context.ServiceMedications.RemoveRange(serviceMedications.Where(rec => !model.ServiceMedications.ContainsKey(rec.MedicationId))); + context.SaveChanges(); + foreach (var updateMedication in serviceMedications) + { + updateMedication.Count = + model.ServiceMedications[updateMedication.MedicationId].Item2; + model.ServiceMedications.Remove(updateMedication.MedicationId); + } + context.SaveChanges(); + } + var service = context.Services.First(x => x.Id == Id); + foreach (var pc in model.ServiceMedications) + { + context.ServiceMedications.Add(new ServiceMedication + { + Service = service, + Medication = context.Medications.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _serviceMedications = null; + } + + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Models/ServiceMedication.cs b/VeterinaryView/VeterinaryDatabaseImplement/Models/ServiceMedication.cs new file mode 100644 index 0000000..94f6f05 --- /dev/null +++ b/VeterinaryView/VeterinaryDatabaseImplement/Models/ServiceMedication.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VeterinaryDatabaseImplement.Models +{ + public class ServiceMedication + { + public int Id { get; set; } + [Required] + public int ServiceId { get; set; } + [Required] + public int MedicationId { get; set; } + [Required] + public int Count { get; set; } + public virtual Medication Medication { get; set; } = new(); + public virtual Service Service { get; set; } = new(); + } +} diff --git a/VeterinaryView/VeterinaryDatabaseImplement/VeterinaryDatabaseImplement.csproj b/VeterinaryView/VeterinaryDatabaseImplement/VeterinaryDatabaseImplement.csproj index 1d0ecbf..ad320eb 100644 --- a/VeterinaryView/VeterinaryDatabaseImplement/VeterinaryDatabaseImplement.csproj +++ b/VeterinaryView/VeterinaryDatabaseImplement/VeterinaryDatabaseImplement.csproj @@ -12,7 +12,6 @@ -