2023-04-02 16:19:39 +04:00
|
|
|
|
using HotelContracts.BindingModels;
|
|
|
|
|
using HotelContracts.ViewModels;
|
|
|
|
|
using HotelDataModels.Models;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2023-05-16 19:43:06 +04:00
|
|
|
|
using System.Text.Json.Serialization;
|
2023-04-02 14:32:57 +04:00
|
|
|
|
|
|
|
|
|
namespace HotelDataBaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class MealPlan : IMealPlanModel
|
|
|
|
|
{
|
2023-04-02 16:19:39 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public string MealPlanName { get; set; } = string.Empty;
|
2023-04-02 14:32:57 +04:00
|
|
|
|
|
2023-04-02 16:19:39 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public double MealPlanPrice { get; set; }
|
2023-04-02 14:32:57 +04:00
|
|
|
|
|
2023-04-02 16:19:39 +04:00
|
|
|
|
public int OrganiserId { get; private set; }
|
2023-04-02 14:32:57 +04:00
|
|
|
|
|
2023-04-02 16:19:39 +04:00
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
|
|
|
|
|
public virtual Organiser Organiser { get; set; }
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, IMemberModel> _mealPlanMembers = null;
|
2023-05-16 19:43:06 +04:00
|
|
|
|
|
2023-05-16 17:41:58 +04:00
|
|
|
|
[NotMapped]
|
2023-04-02 16:19:39 +04:00
|
|
|
|
public Dictionary<int, IMemberModel> MealPlanMembers
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_mealPlanMembers == null)
|
|
|
|
|
{
|
2023-05-16 19:43:06 +04:00
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
_mealPlanMembers = Members
|
|
|
|
|
.ToDictionary(x => x.MemberId, x => (context.Members
|
|
|
|
|
.FirstOrDefault(y => y.Id == x.MemberId)! as IMemberModel));
|
2023-04-02 16:19:39 +04:00
|
|
|
|
}
|
|
|
|
|
return _mealPlanMembers;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-16 19:43:06 +04:00
|
|
|
|
|
|
|
|
|
[ForeignKey("MealPlanId")]
|
|
|
|
|
public virtual List<Room> Rooms { get; set; }
|
|
|
|
|
|
|
|
|
|
[ForeignKey("MealPlanId")]
|
|
|
|
|
public virtual List<MealPlanMember> Members { get; set; } = new();
|
|
|
|
|
|
2023-04-02 16:19:39 +04:00
|
|
|
|
public static MealPlan Create(HotelDataBase context, MealPlanBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new MealPlan()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
MealPlanName = model.MealPlanName,
|
|
|
|
|
MealPlanPrice = model.MealPlanPrice,
|
2023-05-14 17:51:51 +04:00
|
|
|
|
OrganiserId = model.OrganiserId,
|
2023-04-02 16:19:39 +04:00
|
|
|
|
Members = model.MealPlanMembers.Select(x => new MealPlanMember
|
|
|
|
|
{
|
|
|
|
|
Member = context.Members.First(y => y.Id == x.Key),
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(MealPlanBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
MealPlanName = model.MealPlanName;
|
|
|
|
|
MealPlanPrice = model.MealPlanPrice;
|
2023-05-14 17:51:51 +04:00
|
|
|
|
OrganiserId = model.OrganiserId;
|
2023-04-02 16:19:39 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MealPlanViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
MealPlanName = MealPlanName,
|
|
|
|
|
MealPlanPrice = MealPlanPrice,
|
2023-05-14 17:51:51 +04:00
|
|
|
|
OrganiserId=OrganiserId,
|
2023-04-02 16:19:39 +04:00
|
|
|
|
MealPlanMembers = MealPlanMembers
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void UpdateMembers(HotelDataBase context, MealPlanBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var mealPlanMembers = context.MealPlanMembers.Where(rec => rec.MealPlanId == model.Id).ToList();
|
|
|
|
|
|
2023-05-16 19:43:06 +04:00
|
|
|
|
if (mealPlanMembers != null && mealPlanMembers.Count > 0)
|
2023-04-02 16:32:10 +04:00
|
|
|
|
{
|
2023-04-02 16:19:39 +04:00
|
|
|
|
context.MealPlanMembers.RemoveRange(mealPlanMembers.Where(rec => !model.MealPlanMembers.ContainsKey(rec.MemberId)));
|
|
|
|
|
context.SaveChanges();
|
2023-05-16 19:43:06 +04:00
|
|
|
|
|
|
|
|
|
foreach (var updateMember in mealPlanMembers)
|
|
|
|
|
{
|
|
|
|
|
model.MealPlanMembers.Remove(updateMember.MemberId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
2023-04-02 16:19:39 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mealPlan = context.MealPlans.First(x => x.Id == Id);
|
|
|
|
|
|
|
|
|
|
foreach (var cm in model.MealPlanMembers)
|
|
|
|
|
{
|
|
|
|
|
context.MealPlanMembers.Add(new MealPlanMember
|
|
|
|
|
{
|
|
|
|
|
MealPlan = mealPlan,
|
|
|
|
|
Member = context.Members.First(x => x.Id == cm.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_mealPlanMembers = null;
|
|
|
|
|
}
|
2023-04-02 14:32:57 +04:00
|
|
|
|
}
|
|
|
|
|
}
|