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 VeterinaryClinicContracts.BindingModels; using VeterinaryClinicContracts.ViewModels; using VeterinaryClinicDataModels.Models; namespace VeterinaryClinicDatabaseImplement.Models { public class Visit: IVisitModel { public int Id { get; set; } [Required] public DateTime Date { get; set; } = DateTime.Now; [ForeignKey("UserId")] public int UserId { get; set; } public virtual User User { get; set; } = new(); [ForeignKey("AnimalId")] public virtual List Animals { get; set; } = new(); private Dictionary? _visitAnimals = null; public Dictionary VisitAnimals { get { if (_visitAnimals == null) { _visitAnimals = Animals .ToDictionary(recVA => recVA.AnimalId, recVA => (recVA.Animal as IAnimalModel)); } return _visitAnimals; } } [ForeignKey("ServiceId")] public virtual List Services { get; set; } = new(); private Dictionary? _visitServices = null; public Dictionary VisitServices { get { if (_visitServices == null) { _visitServices = Services .ToDictionary(recVS => recVS.ServiceId, recVS => (recVS.Service as IServiceModel)); } return _visitServices; } } public static Visit? Create(VeterinaryClinicDatabase context, VisitBindingModel model) { if (model == null) { return null; } return new Visit() { Id = model.Id, Date = model.Date, UserId = model.UserId, User = context.Users .First(x => x.Id == model.UserId), Animals = model.VisitAnimals.Select(x => new VisitAnimal { Animal = context.Animals.First(y => y.Id == x.Key) }).ToList(), Services = model.VisitServices.Select(x => new VisitService { Service = context.Services.First(y => y.Id == x.Key) }).ToList() }; } public void Update(VisitBindingModel model) { if (model == null) { return; } Date = model.Date; } public VisitViewModel GetViewModel => new() { Id = Id, Date = Date, UserId = UserId, UserFullName = User.FullName, VisitAnimals = VisitAnimals, VisitServices = VisitServices }; public void UpdateAnimals(VeterinaryClinicDatabase context, VisitBindingModel model) { var visitAnimals = context.VisitAnimals.Where(rec => rec.VisitId == model.Id).ToList(); if (visitAnimals != null && visitAnimals.Count > 0) { context.VisitAnimals.RemoveRange(visitAnimals.Where(rec => !model.VisitAnimals.ContainsKey(rec.AnimalId))); context.SaveChanges(); } var visit = context.Visits.First(x => x.Id == Id); foreach (var rm in model.VisitAnimals) { context.VisitAnimals.Add(new VisitAnimal { Visit = visit, Animal = context.Animals.First(x => x.Id == rm.Key) }); context.SaveChanges(); } _visitAnimals = null; } public void UpdateMedication(VeterinaryClinicDatabase context, VisitBindingModel model) { var visitServices = context.VisitServices.Where(rec => rec.VisitId == model.Id).ToList(); if (visitServices != null && visitServices.Count > 0) { context.VisitServices.RemoveRange(visitServices.Where(rec => !model.VisitServices.ContainsKey(rec.ServiceId))); context.SaveChanges(); } var visit = context.Visits.First(x => x.Id == Id); foreach (var rm in model.VisitServices) { context.VisitServices.Add(new VisitService { Visit = visit, Service = context.Services.First(x => x.Id == rm.Key) }); context.SaveChanges(); } _visitServices = null; } } }