147 lines
5.2 KiB
C#
147 lines
5.2 KiB
C#
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<int, IMedicineModel>? _recipeMedicines = null;
|
|
|
|
[NotMapped]
|
|
public Dictionary<int, IMedicineModel> RecipeMedicines
|
|
{
|
|
get
|
|
{
|
|
if (_recipeMedicines == null)
|
|
{
|
|
_recipeMedicines = Medicines
|
|
.ToDictionary(rec => rec.MedicineId, rec =>
|
|
rec.Medicine as IMedicineModel);
|
|
}
|
|
return _recipeMedicines;
|
|
}
|
|
}
|
|
|
|
[ForeignKey("RecipeId")]
|
|
public virtual List<RecipeMedicine> Medicines { get; set; } = new();
|
|
|
|
private Dictionary<int, ITreatmentModel>? _recipeTreatments = null;
|
|
|
|
[NotMapped]
|
|
public Dictionary<int, ITreatmentModel> RecipeTreatments
|
|
{
|
|
get
|
|
{
|
|
if (_recipeTreatments == null)
|
|
{
|
|
_recipeTreatments = Treatments
|
|
.ToDictionary(rec => rec.TreatmentId, rec =>
|
|
rec.Treatment as ITreatmentModel);
|
|
}
|
|
return _recipeTreatments;
|
|
}
|
|
}
|
|
[ForeignKey("RecipeId")]
|
|
public virtual List<RecipeTreatment> 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;
|
|
}
|
|
public RecipeViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
Name = Name,
|
|
Date = Date,
|
|
ApothecaryId = ApothecaryId,
|
|
ApothecaryLogin = Apothecary?.Login,
|
|
RecipeMedicines = RecipeMedicines,
|
|
RecipeTreatments = RecipeTreatments
|
|
};
|
|
|
|
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);
|
|
var existingMedicineIds = recipeMedicines?.Select(x => x.MedicineId).ToList();
|
|
foreach (var rec in model.RecipeMedicines)
|
|
{
|
|
|
|
if (existingMedicineIds != null && !existingMedicineIds.Contains(rec.Key))
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|