CourseWork_EventVisitor/EventVisitor/EventVisitorDatabase/Entities/EventEntity.cs

161 lines
5.2 KiB
C#
Raw Normal View History

2024-11-03 01:20:30 +04:00
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;
2024-11-03 10:18:08 +04:00
using System.ComponentModel.DataAnnotations;
2024-11-03 01:20:30 +04:00
namespace EventVisitorDatabase.Entities
{
public class EventEntity
{
public int Id { get; set; }
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
public string Name { get; set; } = string.Empty;
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
public DateTime Date { get; set; }
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
public DateTime TimeStart { get; set; }
public DateTime TimeEnd { get; set; }
public string Description { get; set; } = string.Empty;
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
public string Type { get; set; } = string.Empty;
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
public string ContactPhone { get; set; } = string.Empty;
public string ContactEmail { get; set; } = string.Empty;
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
public string Address { get; set; } = string.Empty;
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
public string City { get; set; } = string.Empty;
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
public string Status { get; set; } = string.Empty;
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
public int CountVisitors { get; set; }
2024-11-03 10:18:08 +04:00
[Required]
2024-11-03 01:20:30 +04:00
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,
};
}
}