поручитель модели хранилища

This commit is contained in:
antoc0der 2024-04-24 23:45:02 +04:00
parent bbe1f57823
commit da51b14f66
8 changed files with 386 additions and 2 deletions

View File

@ -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))

View File

@ -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<Service> Services { get; set; } = new();
[ForeignKey("DoctorId")]
public virtual List<Medication> Medications { get; set; } = new();
[ForeignKey("DoctorId")]
public virtual List<Visit> 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
};
}
}

View File

@ -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<int, (IMedicationModel, int)>? _drugMedications = null;
[NotMapped]
public Dictionary<int, (IMedicationModel, int)> 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<DrugMedication> Medications { get; set; } = new();
[ForeignKey("DrugId")]
public virtual List<Purchase> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -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<ServiceMedication> ServiceMedications { get; set; } = new();
[ForeignKey("MedicationId")]
public virtual List<DrugMedication> 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
};
}
}

View File

@ -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<int, (IMedicationModel, int)>? _serviceMedications = null;
[NotMapped]
public Dictionary<int, (IMedicationModel, int)> 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<ServiceMedication> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -12,7 +12,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Models\" />
<Folder Include="Implements\" />
</ItemGroup>