using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson; using RouteGuideDataModels.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using RouteGuideContracts.BindingModels; using RouteGuideContracts.ViewModels; using MongoDB.Driver; namespace RouteGuideMongoDBImplement.Models { /// /// Сущность "Расписание" /// public class Schedule : IScheduleModel { /// /// Идентификатор /// [BsonId] [BsonElement("_id")] [BsonRepresentation(BsonType.ObjectId)] public string? Id { get; private set; } /// /// Дата записи расписания /// [BsonRequired] public DateTime Date { get; private set; } = DateTime.Now; /// /// Время отправления /// [BsonRequired] public DateTime DepartureTime { get; private set; } = DateTime.MinValue; /// /// Время прибытия /// [BsonRequired] public DateTime ArrivalTime { get; private set; } = DateTime.MinValue; /// /// Периодичность движения транспорта /// [BsonRequired] public DateTime Frequency { get; private set; } = DateTime.MinValue; /// /// Идентификатор маршрута /// [BsonRequired] [BsonElement("route_id")] public string? RouteId { get; private set; } /// /// Сущность "Маршрут" /// [BsonIgnoreIfNull] [BsonIgnoreIfDefault] public virtual Route Route { get; private set; } = new(); /// /// Создание модели /// /// /// public static Schedule? Create(RouteGuideDatabase context, ScheduleBindingModel model) { if (model == null) { return null; } return new Schedule() { Date = model.Date, DepartureTime = model.DepartureTime, ArrivalTime = model.ArrivalTime, Frequency = model.Frequency, RouteId = model.RouteId.ToString(), Route = context.Routes .Find(Builders.Filter.Eq(x => x.Id, model.RouteId)) .FirstOrDefault() }; } /// /// Изменение модели /// /// public void Update(ScheduleBindingModel model) { if (model == null) { return; } Date = model.Date; DepartureTime = model.DepartureTime; ArrivalTime = model.ArrivalTime; Frequency = model.Frequency; } /// /// Получение модели /// public ScheduleViewModel GetViewModel => new() { Id = Id!, Date = Date, DepartureTime = DepartureTime, ArrivalTime = ArrivalTime, Frequency = Frequency, RouteId = RouteId!, RouteName = Route.Name }; } }