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

163 lines
5.5 KiB
C#
Raw Permalink Normal View History

2024-04-02 02:15:17 +04:00
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
{
/// <summary>
/// Сущность "Маршрут"
/// </summary>
2024-04-23 19:38:25 +04:00
public class Route : IRouteModel<int>
2024-04-02 02:15:17 +04:00
{
/// <summary>
/// Идентификатор
/// </summary>
public int Id { get; private set; }
/// <summary>
/// Название маршрута
/// </summary>
[Required]
public string Name { get; private set; } = string.Empty;
/// <summary>
/// Идентификатор транспорта
/// </summary>
[ForeignKey("TransportId")]
public int TransportId { get; private set; }
/// <summary>
/// Сущность "Транспорт"
/// </summary>
public virtual Transport Transport { get; private set; } = new();
/// <summary>
/// Коллекция остановок маршрута
/// </summary>
2024-04-23 19:38:25 +04:00
private Dictionary<int, (IStopModel<int>, int)>? _routeStops = null;
2024-04-02 02:15:17 +04:00
/// <summary>
/// Коллекция остановок маршрута
/// </summary>
[NotMapped]
2024-04-23 19:38:25 +04:00
public Dictionary<int, (IStopModel<int>, int)> RouteStops
2024-04-02 02:15:17 +04:00
{
get
{
if (_routeStops == null)
{
_routeStops = Stops
2024-04-23 19:38:25 +04:00
.ToDictionary(recRS => recRS.StopId, recRS => (recRS.Stop as IStopModel<int>, recRS.Number));
2024-04-02 02:15:17 +04:00
}
return _routeStops;
}
}
/// <summary>
/// Связь с классом связи маршрутов и остановок
/// </summary>
[ForeignKey("RouteId")]
public virtual List<RouteStop> Stops { get; set; } = new();
/// <summary>
/// Созданме модели
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
/// <returns></returns>
public static Route Create(RouteGuideDatabase context, RouteBindingModel model)
{
return new Route()
{
2024-04-23 19:38:25 +04:00
Id = (int)model.Id,
2024-04-02 02:15:17 +04:00
Name = model.Name,
2024-04-23 19:38:25 +04:00
TransportId = (int)model.TransportId,
2024-04-02 02:15:17 +04:00
Transport = context.Transport
2024-04-23 19:38:25 +04:00
.FirstOrDefault(x => x.Id.Equals(model.TransportId)),
2024-04-02 02:15:17 +04:00
Stops = model.RouteStops.Select(x => new RouteStop
{
2024-04-23 19:38:25 +04:00
Stop = context.Stops.First(y => y.Id.Equals(x.Key)),
2024-04-02 02:15:17 +04:00
Number = x.Value.Item2
}).ToList()
};
}
/// <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,
2024-04-23 19:38:25 +04:00
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)),
2024-04-05 02:48:52 +04:00
TransportLicense = Transport.License
2024-04-02 02:15:17 +04:00
};
/// <summary>
/// Обновление списка связей
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
public void UpdateStops(RouteGuideDatabase context, RouteBindingModel model)
{
2024-04-23 19:38:25 +04:00
var routeStops = context.RouteStops.Where(rec => rec.RouteId.Equals(model.Id)).ToList();
2024-04-02 02:15:17 +04:00
if (routeStops != null && routeStops.Count > 0)
{
// Удаление остановок, которых нет в маршруте
context.RouteStops.RemoveRange(routeStops.Where(rec => !model.RouteStops.ContainsKey(rec.StopId)));
context.SaveChanges();
// Обновление количества у существующих записей
foreach (var updateStop in routeStops)
{
updateStop.Number = model.RouteStops[updateStop.StopId].Item2;
model.RouteStops.Remove(updateStop.StopId);
}
context.SaveChanges();
}
var route = context.Routes.First(x => x.Id == Id);
foreach (var rs in model.RouteStops)
{
context.RouteStops.Add(new RouteStop
{
Route = route,
2024-04-23 19:38:25 +04:00
Stop = context.Stops.First(x => x.Id.Equals(rs.Key)),
2024-04-02 02:15:17 +04:00
Number = rs.Value.Item2
});
context.SaveChanges();
}
_routeStops = null;
}
}
}