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

163 lines
5.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>
public class Route : IRouteModel<int>
{
/// <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>
private Dictionary<int, (IStopModel<int>, int)>? _routeStops = null;
/// <summary>
/// Коллекция остановок маршрута
/// </summary>
[NotMapped]
public Dictionary<int, (IStopModel<int>, int)> RouteStops
{
get
{
if (_routeStops == null)
{
_routeStops = Stops
.ToDictionary(recRS => recRS.StopId, recRS => (recRS.Stop as IStopModel<int>, recRS.Number));
}
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()
{
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()
};
}
/// <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)),
TransportLicense = Transport.License
};
/// <summary>
/// Обновление списка связей
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
public void UpdateStops(RouteGuideDatabase context, RouteBindingModel model)
{
var routeStops = context.RouteStops.Where(rec => rec.RouteId.Equals(model.Id)).ToList();
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,
Stop = context.Stops.First(x => x.Id.Equals(rs.Key)),
Number = rs.Value.Item2
});
context.SaveChanges();
}
_routeStops = null;
}
}
}