2024-04-28 18:11:56 +04:00
|
|
|
|
using HotelContracts.BindingModels;
|
|
|
|
|
using HotelContracts.ViewModels;
|
|
|
|
|
using HotelDataModels.Models;
|
|
|
|
|
using Npgsql.PostgresTypes;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.Globalization;
|
2024-04-22 17:05:38 +04:00
|
|
|
|
|
|
|
|
|
namespace HotelDataBaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class MealPlan : IMealPlanModel
|
|
|
|
|
{
|
2024-04-28 18:11:56 +04:00
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string MealPlanName { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public double MealPlanPrice { get; set; }
|
|
|
|
|
|
|
|
|
|
public int OrganiserId { get; private set; }
|
|
|
|
|
|
|
|
|
|
public virtual Organiser Organiser { get; set; }
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, IParticipantModel> _mealPlanParticipants = null;
|
|
|
|
|
|
|
|
|
|
[ForeignKey("MealPlanId")]
|
|
|
|
|
public virtual List<MealPlanParticipant> Participants { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
[ForeignKey("MealPlanId")]
|
|
|
|
|
public virtual List<MealPlanRoom> Rooms { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IParticipantModel> MealPlanParticipants
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if( _mealPlanParticipants == null )
|
|
|
|
|
{
|
|
|
|
|
using var contex = new HotelDataBase();
|
|
|
|
|
_mealPlanParticipants = Participants.ToDictionary(x => x.ParticipantId, x => (contex.Participants
|
|
|
|
|
.FirstOrDefault(y => y.Id == x.ParticipantId)! as IParticipantModel));
|
|
|
|
|
}
|
|
|
|
|
return _mealPlanParticipants;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static MealPlan Create(HotelDataBase context, MealPlanBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new MealPlan()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
MealPlanName = model.MealPlanName,
|
|
|
|
|
MealPlanPrice = model.MealPlanPrice,
|
|
|
|
|
OrganiserId = model.OrganiserId,
|
|
|
|
|
Participants = model.MealPlanParticipants.Select(x => new MealPlanParticipant
|
|
|
|
|
{
|
|
|
|
|
Participant = context.Participants.First(y => y.Id == x.Key),
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(MealPlanBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
MealPlanName = model.MealPlanName;
|
|
|
|
|
MealPlanPrice = model.MealPlanPrice;
|
|
|
|
|
OrganiserId = model.OrganiserId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MealPlanViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
MealPlanName = MealPlanName,
|
|
|
|
|
MealPlanPrice = MealPlanPrice,
|
|
|
|
|
OrganiserId = OrganiserId,
|
2024-05-01 17:00:04 +04:00
|
|
|
|
MealPlanParticipants = MealPlanParticipants
|
2024-04-28 18:11:56 +04:00
|
|
|
|
};
|
|
|
|
|
|
2024-05-01 17:00:04 +04:00
|
|
|
|
public void UpdateParticipants(HotelDataBase context, MealPlanBindingModel model)
|
2024-04-28 18:11:56 +04:00
|
|
|
|
{
|
|
|
|
|
var mealPlanParticipants = context.MealPlanParticipants.Where(rec => rec.MealPlanId == model.Id).ToList();
|
|
|
|
|
|
|
|
|
|
if (mealPlanParticipants != null && mealPlanParticipants.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
context.MealPlanParticipants.RemoveRange(mealPlanParticipants.Where(rec => !model.MealPlanParticipants.ContainsKey(rec.ParticipantId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
foreach (var updateMember in mealPlanParticipants)
|
|
|
|
|
{
|
|
|
|
|
model.MealPlanParticipants.Remove(updateMember.ParticipantId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mealPlan = context.MealPlans.First(x => x.Id == Id);
|
|
|
|
|
|
|
|
|
|
foreach (var cm in model.MealPlanParticipants)
|
|
|
|
|
{
|
|
|
|
|
context.MealPlanParticipants.Add(new MealPlanParticipant
|
|
|
|
|
{
|
|
|
|
|
MealPlan = mealPlan,
|
|
|
|
|
Participant = context.Participants.First(x => x.Id == cm.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_mealPlanParticipants = null;
|
|
|
|
|
}
|
2024-04-22 17:05:38 +04:00
|
|
|
|
}
|
|
|
|
|
}
|