using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Net.NetworkInformation; using System.Net; using System.Text; using System.Threading.Tasks; using static System.Runtime.InteropServices.JavaScript.JSType; using EventVisitorLogic.BindingModels; using EventVisitorLogic.ViewModels; using System.ComponentModel.DataAnnotations; namespace EventVisitorDatabase.Entities { public class OrganizerEntity { public int Id { get; set; } [Required] public string Surname { get; set; } = string.Empty; [Required] public string Name { get; set; } = string.Empty; public string LastName { get; set; } = string.Empty; public string OrganizationName { get; set; } = string.Empty; [Required] public string Role { get; set; } = string.Empty; [Required] public string Email { get; set; } = string.Empty; [Required] public string Password { get; set; } = string.Empty; [Required] public string Phone { get; set; } = string.Empty; [ForeignKey("OrganizerId")] public virtual List Events { get; set; } = new(); private Dictionary? _eventOrginizers = null; [NotMapped] public Dictionary OrganizerEvent { get { if (_eventOrginizers == null) { _eventOrginizers = Events .ToDictionary(rec => rec.EventId, rec => (rec.Event as EventEntity)); } return _eventOrginizers; } } public static OrganizerEntity? Create(OrganizerBindingModel model) { if (model == null) { return null; } return new OrganizerEntity() { Id = model.Id, Name = model.Name, Phone = model.Phone, Role = model.Role, Surname = model.Surname, LastName = model.LastName, OrganizationName = model.OrganizationName, Email = model.Email, Password = model.Password }; } public static OrganizerEntity Create(OrganizerViewModel model) { return new OrganizerEntity() { Id = model.Id, Name = model.Name, Phone = model.Phone, Role = model.Role, Surname = model.Surname, LastName = model.LastName, OrganizationName = model.OrganizationName, Email = model.Email, Password= model.Password }; } public void UpdateEvents(EventVisitorDbContext context, OrganizerBindingModel model) { var organizer = context.Organizers.First(x => x.Id == Id); foreach (var fs in model.OrganizerEvent) { context.OrganizerEvent.Add(new OrganizerEvent { Organizer = organizer, Event = context.Events.First(x => x.Id == fs.Value.Id) }); context.SaveChanges(); } _eventOrginizers = null; } public void Update(OrganizerBindingModel model) { Name = model.Name; Phone = model.Phone; Role = model.Role; Surname = model.Surname; LastName = model.LastName; OrganizationName = model.OrganizationName; Password = model.Password; } public OrganizerViewModel GetViewModel => new() { Id = Id, Name = Name, Phone = Phone, Role = Role, Surname = Surname, LastName = LastName, OrganizationName = OrganizationName, Email = Email, Password = Password }; } }