CourseWork_EventVisitor/EventVisitor/EventVisitorDatabase/Entities/EventEntity.cs

161 lines
5.2 KiB
C#

using EventVisitorModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Xml.Linq;
using EventVisitorLogic.ViewModels;
using EventVisitorLogic.BindingModels;
using System.ComponentModel.DataAnnotations;
namespace EventVisitorDatabase.Entities
{
public class EventEntity
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public DateTime Date { get; set; }
[Required]
public DateTime TimeStart { get; set; }
public DateTime TimeEnd { get; set; }
public string Description { get; set; } = string.Empty;
[Required]
public string Type { get; set; } = string.Empty;
[Required]
public string ContactPhone { get; set; } = string.Empty;
public string ContactEmail { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public string City { get; set; } = string.Empty;
[Required]
public string Status { get; set; } = string.Empty;
[Required]
public int CountVisitors { get; set; }
[Required]
public int FreePlaces { get; set; }
[ForeignKey("EventId")]
public virtual List<OrganizerEvent> Organizers { get; set; } = new();
private Dictionary<int, OrganizerEntity>? _eventOrginizers = null;
[NotMapped]
public Dictionary<int, OrganizerEntity> OrganizerEvent
{
get
{
if (_eventOrginizers == null)
{
_eventOrginizers = Organizers
.ToDictionary(rec => rec.OrganizerId, rec => (rec.Organizer as OrganizerEntity));
}
return _eventOrginizers;
}
}
public static EventEntity? Create(EventBindingModel model)
{
if (model == null)
{
return null;
}
return new EventEntity()
{
Id = (int)(model?.Id),
Name = model.Name,
Date = model.Date,
TimeStart = model.TimeStart,
TimeEnd = model.TimeEnd,
Description = model.Description,
Type = model.Type,
ContactPhone = model.ContactPhone,
ContactEmail = model.ContactEmail,
Address = model.Address,
City = model.City,
Status = model.Status,
CountVisitors = model.CountVisitors,
FreePlaces = model.FreePlaces
};
}
public static EventEntity Create(EventViewModel model)
{
return new EventEntity()
{
Id = model.Id,
Name = model.Name,
Date = model.Date,
TimeStart = model.TimeStart,
TimeEnd = model.TimeEnd,
Description = model.Description,
Type = model.Type,
ContactPhone = model.ContactPhone,
ContactEmail = model.ContactEmail,
Address = model.Address,
City = model.City,
Status = model.Status,
CountVisitors = model.CountVisitors,
FreePlaces = model.FreePlaces
};
}
public void UpdateOrganizer(EventVisitorDbContext context, EventBindingModel model)
{
var events = context.Events.First(x => x.Id == Id);
foreach (var fs in model.OrganizerEvent)
{
context.OrganizerEvent.Add(new OrganizerEvent
{
Event = events,
Organizer = context.Organizers.First(x => x.Id == fs.Value.Id)
});
context.SaveChanges();
}
_eventOrginizers = null;
}
public void Update(EventBindingModel model)
{
Name = model.Name;
Date = model.Date;
TimeStart = model.TimeStart;
TimeEnd = model.TimeEnd;
Description = model.Description;
Type = model.Type;
ContactPhone = model.ContactPhone;
ContactEmail = model.ContactEmail;
Address = model.Address;
City = model.City;
Status = model.Status;
CountVisitors = model.CountVisitors;
FreePlaces = model.FreePlaces;
}
public EventViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Date = Date,
TimeStart = TimeStart,
TimeEnd = TimeEnd,
Description = Description,
Type = Type,
ContactPhone = ContactPhone,
ContactEmail = ContactEmail,
Address = Address,
City = City,
Status = Status,
CountVisitors = CountVisitors,
FreePlaces = FreePlaces,
};
}
}