using RouteGuideContracts.BindingModels;
using RouteGuideContracts.ViewModels;
using RouteGuideDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace RouteGuideDatabaseImplement.Models
{
///
/// Сущность "Маршрут"
///
public class Route : IRouteModel
{
///
/// Идентификатор
///
public int Id { get; private set; }
///
/// Название маршрута
///
[Required]
public string Name { get; private set; } = string.Empty;
///
/// Идентификатор транспорта
///
[ForeignKey("TransportId")]
public int TransportId { get; private set; }
///
/// Сущность "Транспорт"
///
public virtual Transport Transport { get; private set; } = new();
///
/// Коллекция остановок маршрута
///
private Dictionary, int)>? _routeStops = null;
///
/// Коллекция остановок маршрута
///
[NotMapped]
public Dictionary, int)> RouteStops
{
get
{
if (_routeStops == null)
{
_routeStops = Stops
.ToDictionary(recRS => recRS.StopId, recRS => (recRS.Stop as IStopModel, recRS.Number));
}
return _routeStops;
}
}
///
/// Связь с классом связи маршрутов и остановок
///
[ForeignKey("RouteId")]
public virtual List Stops { get; set; } = new();
///
/// Созданме модели
///
///
///
///
public static Route Create(RouteGuideDatabase context, RouteBindingModel model)
{
return new Route()
{
Id = (int)model.Id,
Name = model.Name,
TransportId = (int)model.TransportId,
Transport = context.Transport
.FirstOrDefault(x => x.Id.Equals(model.TransportId)),
Stops = model.RouteStops.Select(x => new RouteStop
{
Stop = context.Stops.First(y => y.Id.Equals(x.Key)),
Number = x.Value.Item2
}).ToList()
};
}
///
/// Изменение модели
///
///
public void Update(RouteBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
}
///
/// Получение модели
///
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