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 17:41:58 +04:00
|
|
|
|
using System.Text.Json.Serialization;
|
2023-04-02 14:32:57 +04:00
|
|
|
|
|
|
|
|
|
namespace HotelDataBaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Conference : IConferenceModel
|
|
|
|
|
{
|
2023-04-02 16:19:39 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public string ConferenceName { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public DateTime StartDate { get; set; } = DateTime.Now;
|
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; }
|
2023-04-02 14:32:57 +04:00
|
|
|
|
|
2023-04-02 16:19:39 +04:00
|
|
|
|
public virtual Organiser Organiser { get; set; }
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, IMemberModel> _conferenceMembers = null;
|
2023-05-16 17:41:58 +04:00
|
|
|
|
|
|
|
|
|
[NotMapped]
|
2023-04-02 16:19:39 +04:00
|
|
|
|
public Dictionary<int, IMemberModel> ConferenceMembers
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_conferenceMembers == null)
|
|
|
|
|
{
|
2023-05-16 17:41:58 +04:00
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
_conferenceMembers = 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 _conferenceMembers;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 17:41:58 +04:00
|
|
|
|
[ForeignKey("ConferenceId")]
|
|
|
|
|
public virtual List<ConferenceBooking> ConferenceBookings { get; set; }
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ConferenceId")]
|
|
|
|
|
public virtual List<ConferenceMember> Members { get; set; } = new();
|
|
|
|
|
|
2023-04-02 16:19:39 +04:00
|
|
|
|
public static Conference Create(HotelDataBase context, ConferenceBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Conference()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
ConferenceName = model.ConferenceName,
|
|
|
|
|
StartDate = model.StartDate,
|
2023-05-14 17:51:51 +04:00
|
|
|
|
OrganiserId=model.OrganiserId,
|
2023-04-02 16:19:39 +04:00
|
|
|
|
Members = model.ConferenceMembers.Select(x => new ConferenceMember
|
|
|
|
|
{
|
|
|
|
|
Member = context.Members.First(y => y.Id == x.Key),
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(ConferenceBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
ConferenceName = model.ConferenceName;
|
|
|
|
|
StartDate = model.StartDate;
|
2023-05-14 17:51:51 +04:00
|
|
|
|
OrganiserId = model.OrganiserId;
|
2023-04-02 16:19:39 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConferenceViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
ConferenceName = ConferenceName,
|
|
|
|
|
StartDate = StartDate,
|
2023-05-14 17:51:51 +04:00
|
|
|
|
OrganiserId = OrganiserId,
|
2023-04-02 16:19:39 +04:00
|
|
|
|
ConferenceMembers = ConferenceMembers
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void UpdateMembers(HotelDataBase context, ConferenceBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var conferenceMembers = context.ConferenceMembers.Where(rec => rec.ConferenceId == model.Id).ToList();
|
2023-05-16 17:41:58 +04:00
|
|
|
|
|
|
|
|
|
if (conferenceMembers != null && conferenceMembers.Count > 0)
|
2023-04-02 16:32:10 +04:00
|
|
|
|
{
|
2023-04-02 16:19:39 +04:00
|
|
|
|
context.ConferenceMembers.RemoveRange(conferenceMembers.Where(rec => !model.ConferenceMembers.ContainsKey(rec.MemberId)));
|
|
|
|
|
context.SaveChanges();
|
2023-05-16 17:41:58 +04:00
|
|
|
|
|
|
|
|
|
foreach (var updateMember in conferenceMembers)
|
|
|
|
|
{
|
|
|
|
|
model.ConferenceMembers.Remove(updateMember.MemberId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
2023-04-02 16:19:39 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var conference = context.Conferences.First(x => x.Id == Id);
|
|
|
|
|
foreach (var cm in model.ConferenceMembers)
|
|
|
|
|
{
|
|
|
|
|
context.ConferenceMembers.Add(new ConferenceMember
|
|
|
|
|
{
|
|
|
|
|
Conference = conference,
|
2023-05-16 17:41:58 +04:00
|
|
|
|
Member = context.Members.First(x => x.Id == cm.Key),
|
2023-04-02 16:19:39 +04:00
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_conferenceMembers = null;
|
|
|
|
|
}
|
2023-04-02 14:32:57 +04:00
|
|
|
|
}
|
|
|
|
|
}
|