2024-05-01 19:51:05 +04:00

119 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HotelContracts.BindingModels;
using HotelContracts.ViewModels;
using HotelDataModels.Models;
using Microsoft.EntityFrameworkCore;
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 Conference : IConferenceModel
{
public int Id { get; private set; }
[Required]
public string ConferenceName { get; set; } = string.Empty;
[Required]
public string Subject { get; set; } = string.Empty;
[Required]
public DateTime StartDate { get; set; } = DateTime.Now;
public int OrganiserId { get; private set; }
public virtual Organiser Organiser { get; set; }
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
{
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,
StartDate = model.StartDate,
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;
StartDate = model.StartDate;
OrganiserId = model.OrganiserId;
}
public ConferenceViewModel GetViewModel => new()
{
Id = Id,
ConferenceName = ConferenceName,
Subject = Subject,
StartDate= StartDate,
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();
}
_conferenceParticipents = null;
}
}
}