PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideMongoDBImplement/Models/Schedule.cs
2024-04-23 19:38:25 +04:00

123 lines
2.9 KiB
C#

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
{
/// <summary>
/// Сущность "Расписание"
/// </summary>
public class Schedule : IScheduleModel<string>
{
/// <summary>
/// Идентификатор
/// </summary>
[BsonId]
[BsonElement("_id")]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; private set; }
/// <summary>
/// Дата записи расписания
/// </summary>
[BsonRequired]
public DateTime Date { get; private set; } = DateTime.Now;
/// <summary>
/// Время отправления
/// </summary>
[BsonRequired]
public DateTime DepartureTime { get; private set; } = DateTime.MinValue;
/// <summary>
/// Время прибытия
/// </summary>
[BsonRequired]
public DateTime ArrivalTime { get; private set; } = DateTime.MinValue;
/// <summary>
/// Периодичность движения транспорта
/// </summary>
[BsonRequired]
public DateTime Frequency { get; private set; } = DateTime.MinValue;
/// <summary>
/// Идентификатор маршрута
/// </summary>
[BsonRequired]
[BsonElement("route_id")]
public string? RouteId { get; private set; }
/// <summary>
/// Сущность "Маршрут"
/// </summary>
[BsonIgnoreIfNull]
[BsonIgnoreIfDefault]
public virtual Route Route { get; private set; } = new();
/// <summary>
/// Создание модели
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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<Route>.Filter.Eq(x => x.Id, model.RouteId))
.FirstOrDefault()
};
}
/// <summary>
/// Изменение модели
/// </summary>
/// <param name="model"></param>
public void Update(ScheduleBindingModel model)
{
if (model == null)
{
return;
}
Date = model.Date;
DepartureTime = model.DepartureTime;
ArrivalTime = model.ArrivalTime;
Frequency = model.Frequency;
}
/// <summary>
/// Получение модели
/// </summary>
public ScheduleViewModel GetViewModel => new()
{
Id = Id!,
Date = Date,
DepartureTime = DepartureTime,
ArrivalTime = ArrivalTime,
Frequency = Frequency,
RouteId = RouteId!,
RouteName = Route.Name
};
}
}