using HotelContracts.BindingModels; using HotelContracts.ViewModels; using HotelDataModels.Models; 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; namespace HotelDataBaseImplement.Models { public class ConferenceBooking : IConferenceBookingModel { public int HeadwaiterId { get; private set; } public int ConferenceId { get; private set; } public int Id { get; private set; } public virtual Headwaiter Headwaiter { get; set; } public virtual Conference Conference { get; set; } [ForeignKey("ConferenceBookingId")] public virtual List Dinners { get; set; } private Dictionary _conferenceBookingDinners = null; public Dictionary ConferenceBookingDinners { get { if (_conferenceBookingDinners == null) { _conferenceBookingDinners = Dinners.ToDictionary(recPC => recPC.DinnerId, recPC => (recPC.Dinner as IDinnerModel)); } return _conferenceBookingDinners; } } public static ConferenceBooking Create(HotelDataBase context, ConferenceBookingBindingModel model) { return new ConferenceBooking() { Id = model.Id, Dinners = model.ConferenceBookingDinners.Select(x => new ConferenceBookingDinner { Dinner = context.Dinners.First(y => y.Id == x.Key), }).ToList() }; } public void Update(ConferenceBookingBindingModel model) { } public ConferenceBookingViewModel GetViewModel => new() { Id = Id, ConferenceBookingDinners = ConferenceBookingDinners }; public void UpdateDinners(HotelDataBase context, ConferenceBookingBindingModel model) { var conferenceBookingDinners = context.ConferenceBookingDinners.Where(rec => rec.ConferenceBookingId == model.Id).ToList(); if (conferenceBookingDinners != null) { context.ConferenceBookingDinners.RemoveRange(conferenceBookingDinners.Where(rec => !model.ConferenceBookingDinners.ContainsKey(rec.DinnerId))); context.SaveChanges(); } var conferenceBooking = context.ConferenceBookings.First(x => x.Id == Id); foreach (var cm in model.ConferenceBookingDinners) { context.ConferenceBookingDinners.Add(new ConferenceBookingDinner { ConferenceBooking = conferenceBooking, Dinner = context.Dinners.First(x => x.Id == cm.Key) }); context.SaveChanges(); } _conferenceBookingDinners = null; } } }