CourseWork_EventVisitor/EventVisitor/EventVisitorDatabase/Entities/OrganizerEntity.cs

112 lines
3.5 KiB
C#
Raw Normal View History

2024-11-03 01:20:30 +04:00
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;
namespace EventVisitorDatabase.Entities
{
public class OrganizerEntity
{
public int Id { get; set; }
public string Surname { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string OrganizationName { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
[ForeignKey("OrganizerId")]
public virtual List<OrganizerEvent> Events { get; set; } = new();
private Dictionary<int, EventEntity>? _eventOrginizers = null;
[NotMapped]
public Dictionary<int, EventEntity> 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 = (int)(model?.Id),
Name = model.Name,
Phone = model.Phone,
Role = model.Role,
Surname = model.Surname,
LastName = model.LastName,
OrganizationName = model.OrganizationName
};
}
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
};
}
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;
}
public OrganizerViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Phone = Phone,
Role = Role,
Surname = Surname,
LastName = LastName,
OrganizationName = OrganizationName
};
}
}