113 lines
3.2 KiB
C#
Raw Normal View History

2024-04-28 18:11:56 +04:00
using HotelContracts.BindingModels;
using HotelContracts.ViewModels;
using HotelDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
2024-04-28 18:11:56 +04:00
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelDataBaseImplement.Models
{
public class Conference : IConferenceModel
{
public int Id { get; private set; }
[Required]
public string ConferenceName { get; set; } = string.Empty;
[Required]
public string Subject { get; set; } = string.Empty;
public int OrganiserId { get; private set; }
2024-04-28 18:11:56 +04:00
public virtual Organiser Organiser { get; set; }
2024-04-28 18:11:56 +04:00
private Dictionary<int, IParticipantModel> _conferenceParticipents = null;
[ForeignKey("ConferenceId")]
public virtual List<ConferenceBooking> Bookings { get; set; } = new();
[ForeignKey("ConferenceId")]
public virtual List<ConferenceParticipant> Participants { get; set; } = new();
[NotMapped]
public Dictionary<int, IParticipantModel> ConferenceParticipants
{
get
{
2024-04-28 18:11:56 +04:00
if (_conferenceParticipents == null)
{
using var context = new HotelDataBase();
_conferenceParticipents = Participants.ToDictionary(x => x.ParticipantId, x => (context.Participants
.FirstOrDefault(y => y.Id == x.ParticipantId)! as IParticipantModel));
}
return _conferenceParticipents;
}
}
public static Conference Create(HotelDataBase context, ConferenceBindingModel model)
{
return new Conference()
{
Id = model.Id,
ConferenceName = model.ConferenceName,
Subject = model.Subject,
OrganiserId = model.OrganiserId,
Participants = model.ConferenceParticipants.Select(x => new ConferenceParticipant
{
Participant = context.Participants.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(ConferenceBindingModel model)
{
ConferenceName = model.ConferenceName;
Subject = model.Subject;
OrganiserId = model.OrganiserId;
}
public ConferenceViewModel GetViewModel => new()
{
Id = Id,
ConferenceName = ConferenceName,
Subject = Subject,
OrganiserId = OrganiserId,
ConferenceParticipants = ConferenceParticipants
};
public void UpdateMembers(HotelDataBase context, ConferenceBindingModel model)
{
var сonferenceParticipants = context.ConferenceParticipants.Where(rec => rec.ConferenceId == model.Id).ToList();
if (сonferenceParticipants != null && сonferenceParticipants.Count > 0)
{
context.ConferenceParticipants.RemoveRange(сonferenceParticipants.Where(rec => !model.ConferenceParticipants.ContainsKey(rec.ParticipantId)));
context.SaveChanges();
foreach (var updateParticipant in сonferenceParticipants)
{
model.ConferenceParticipants.Remove(updateParticipant.ParticipantId);
}
context.SaveChanges();
}
var conference = context.Conferences.First(x => x.Id == Id);
foreach (var cm in model.ConferenceParticipants)
{
context.ConferenceParticipants.Add(new ConferenceParticipant
{
Conference = conference,
Participant = context.Participants.First(x => x.Id == cm.Key),
});
context.SaveChanges();
}
2024-04-28 18:11:56 +04:00
_conferenceParticipents = null;
}
}
}