PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideDatabaseImplement/Models/Route.cs
2024-04-05 02:48:52 +04:00

156 lines
5.1 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
{
/// <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)>? _routeStops = null;
/// <summary>
/// Коллекция остановок маршрута
/// </summary>
[NotMapped]
public Dictionary<int, (IStopModel, int)> RouteStops
{
get
{
if (_routeStops == null)
{
_routeStops = Stops
.ToDictionary(recRS => recRS.StopId, recRS => (recRS.Stop as IStopModel, 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 = model.Id,
Name = model.Name,
TransportId = model.TransportId,
Transport = context.Transport
.FirstOrDefault(x => x.Id == model.TransportId),
Stops = model.RouteStops.Select(x => new RouteStop
{
Stop = context.Stops.First(y => y.Id == 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,
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 == 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 == rs.Key),
Number = rs.Value.Item2
});
context.SaveChanges();
}
_routeStops = null;
}
}
}