CourseWork_Hotel/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs

93 lines
3.3 KiB
C#

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<ConferenceBookingDinner> Dinners { get; set; }
private Dictionary<int, IDinnerModel> _conferenceBookingDinners = null;
public Dictionary<int, IDinnerModel> 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,
ConferenceId = model.ConferenceId,
HeadwaiterId = model.HeadwaiterId,
Dinners = model.ConferenceBookingDinners.Select(x => new ConferenceBookingDinner
{
Dinner = context.Dinners.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(ConferenceBookingBindingModel model)
{
ConferenceId = model.ConferenceId;
HeadwaiterId = model.HeadwaiterId;
}
public ConferenceBookingViewModel GetViewModel => new()
{
Id = Id,
ConferenceId = ConferenceId,
HeadwaiterId = HeadwaiterId,
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;
}
}
}