using HospitalContracts.BindingModels; using HospitalContracts.ViewModels; using HospitalDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Diagnostics; namespace HospitalDatabaseImplement.Models { public class Recipe : IRecipeModel { public int Id { get; private set; } [Required] [MaxLength(50)] public string Name { get; private set; } = string.Empty; [Required] public DateTime Date { get; private set; } = DateTime.Now; [Required] public int ApothecaryId { get; private set; } public virtual Apothecary Apothecary { get; set; } private Dictionary? _recipeMedicines = null; [NotMapped] public Dictionary RecipeMedicines { get { if (_recipeMedicines == null) { _recipeMedicines = Medicines .ToDictionary(rec => rec.MedicineId, rec => rec.Medicine as IMedicineModel); } return _recipeMedicines; } } [ForeignKey("RecipeId")] public virtual List Medicines { get; set; } = new(); private Dictionary? _recipeTreatments = null; [NotMapped] public Dictionary RecipeTreatments { get { if (_recipeTreatments == null) { _recipeTreatments = Treatments .ToDictionary(rec => rec.TreatmentId, rec => rec.Treatment as ITreatmentModel); } return _recipeTreatments; } } [ForeignKey("RecipeId")] public virtual List Treatments { get; set; } = new(); public static Recipe Create(HospitalDatabase context, RecipeBindingModel model) { return new Recipe() { Id = model.Id, Name = model.Name, Date = model.Date, ApothecaryId = model.ApothecaryId, Medicines = model.RecipeMedicines.Select(x => new RecipeMedicine { Medicine = context.Medicines.First(y => y.Id == x.Key), }).ToList(), Treatments = model.RecipeTreatments.Select(x => new RecipeTreatment { Treatment = context.Treatments.First(y => y.Id == x.Key) } ).ToList() }; } public void Update(RecipeBindingModel model) { Name = model.Name; Date = model.Date; } public RecipeViewModel GetViewModel => new() { Id = Id, Name = Name, Date = Date, ApothecaryId = ApothecaryId, ApothecaryLogin = Apothecary.Login }; public void UpdateMedicines(HospitalDatabase context, RecipeBindingModel model) { var recipeMedicines = context.RecipeMedicines.Where(rec => rec.RecipeId == model.Id).ToList(); if (recipeMedicines != null && recipeMedicines.Count > 0) { context.RecipeMedicines.RemoveRange(recipeMedicines.Where(rec => !model.RecipeMedicines.ContainsKey(rec.MedicineId))); context.SaveChanges(); } var recipe = context.Recipes.First(x => x.Id == Id); foreach (var rec in model.RecipeMedicines) { context.RecipeMedicines.Add(new RecipeMedicine { Recipe = recipe, Medicine = context.Medicines.First(x => x.Id == rec.Key), }); context.SaveChanges(); } _recipeMedicines = null; } public void UpdateTreatments(HospitalDatabase context, RecipeBindingModel model) { var recipeTreatments = context.RecipeTreatments.Where(rec => rec.RecipeId == model.Id).ToList(); if (recipeTreatments != null && recipeTreatments.Count > 0) { context.RecipeTreatments.RemoveRange(recipeTreatments.Where(rec => !model.RecipeTreatments.ContainsKey(rec.TreatmentId))); context.SaveChanges(); } var recipe = context.Recipes.First(x => x.Id == Id); foreach (var rec in model.RecipeTreatments) { context.RecipeTreatments.Add(new RecipeTreatment { Recipe = recipe, Treatment = context.Treatments.First(x => x.Id == rec.Key), }); context.SaveChanges(); } _recipeTreatments = null; } } }