PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideMongoDBImplement/Models/Route.cs

151 lines
3.8 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 Route : IRouteModel<string>
{
/// <summary>
/// Идентификатор
/// </summary>
[BsonId]
[BsonElement("_id")]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; private set; }
/// <summary>
/// Название маршрута
/// </summary>
[BsonRequired]
public string Name { get; private set; } = string.Empty;
/// <summary>
/// Идентификатор транспорта
/// </summary>
[BsonRequired]
[BsonElement("TransportId")]
public string? TransportId { get; private set; }
/// <summary>
/// Сущность "Транспорт"
/// </summary>
[BsonIgnoreIfNull]
[BsonIgnoreIfDefault]
public virtual Transport Transport { get; private set; } = new();
/// <summary>
/// Коллекция остановок маршрута
/// </summary>
[BsonElement("Stops")]
public Dictionary<string, (Stop Stop, int Number)>? Stops = new();
/// <summary>
/// Коллекция остановок маршрута
/// </summary>
[BsonIgnore]
private Dictionary<string, (IStopModel<string>, int)>? _routeStops = null;
/// <summary>
/// Коллекция остановок маршрута
/// </summary>
[BsonIgnore]
public Dictionary<string, (IStopModel<string>, int)>? RouteStops
{
get
{
if (_routeStops == null)
{
_routeStops = Stops?
.ToDictionary(recRS => recRS.Key, recRS => (recRS.Value.Stop as IStopModel<string>, recRS.Value.Number));
}
return _routeStops;
}
}
/// <summary>
/// Созданме модели
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
/// <returns></returns>
public static Route Create(RouteGuideDatabase context, RouteBindingModel model)
{
return new Route()
{
Name = model.Name,
TransportId = model.TransportId.ToString(),
Transport = context.Transport
.Find(Builders<Transport>.Filter.Eq(x => x.Id, model.TransportId))
.FirstOrDefault(),
Stops = model.RouteStops.ToDictionary(
x => x.Key.ToString()!,
x => (context.Stops
.Find(Builders<Stop>.Filter.Eq(y => y.Id, x.Key))
.FirstOrDefault(),
x.Value.Item2))
};
}
/// <summary>
/// Изменение модели
/// </summary>
/// <param name="model"></param>
public void Update(RouteBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
}
/// <summary>
/// Получение модели
/// </summary>
public RouteViewModel GetViewModel => new()
{
Id = Id!,
Name = Name,
TransportId = TransportId!,
RouteStops = RouteStops?
.ToDictionary(recRS => (object)recRS.Key, recRS => (new StopViewModel()
{
Id = (object)recRS.Value.Item1.Id!,
Name = recRS.Value.Item1.Name,
Street = recRS.Value.Item1.Street,
Number = recRS.Value.Item1.Number
} as IStopModel<object>, recRS.Value.Item2)) ?? new(),
TransportLicense = Transport.License
};
/// <summary>
/// Обновление списка связей
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
public void UpdateStops(RouteGuideDatabase context, RouteBindingModel model)
{
Stops = model.RouteStops.ToDictionary(
x => x.Key.ToString()!,
x => (context.Stops
.Find(Builders<Stop>.Filter.Eq(y => y.Id, x.Key))
.FirstOrDefault(),
x.Value.Item2));
_routeStops = null;
}
}
}