DB/TransportGuideDatabaseImplements/Models/Route.cs

123 lines
3.0 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
2023-05-04 20:21:27 +04:00
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportGuideContracts.BindingModels;
using TransportGuideContracts.ViewModels;
using TransportGuideDataModels.Models;
2023-05-04 20:21:27 +04:00
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace TransportGuideDatabaseImplements.Models
{
public class Route : IRouteModel
{
public int Id { get; private set; }
public int TransportTypeId { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public string IP { get; private set; } = string.Empty;
2023-05-04 20:21:27 +04:00
Dictionary<int, (IStopModel, int)>? _stopRoutes = null;
2023-05-04 20:21:27 +04:00
public virtual TransportType TransportType { get; set; }
2023-05-04 20:21:27 +04:00
[NotMapped]
public Dictionary<int, (IStopModel, int)> StopRoutes {
get
{
2023-05-04 20:21:27 +04:00
if (_stopRoutes == null)
{
_stopRoutes = Stops.ToDictionary(recPC => recPC.StopId, recPC => (recPC.Stop as IStopModel, recPC.Nomer));
}
return _stopRoutes;
}
}
2023-05-04 20:21:27 +04:00
[ForeignKey("RouteId")]
public virtual List<StopRoute> Stops { get; set; } = new();
public static Route Create(TransportGuideDB context, RouteBindingModel model)
{
return new Route()
{
Id = model.Id,
2023-05-04 20:21:27 +04:00
Name = model.Name,
IP = model.IP,
2023-05-05 15:51:49 +04:00
TransportTypeId = model.TransportTypeId,
2023-05-04 20:21:27 +04:00
Stops = model.StopRoutes.Select(x => new StopRoute
{
Stop = context.Stops.First(y => y.Id == x.Key),
Nomer = x.Value.Item2
}).ToList()
};
}
2023-05-04 20:21:27 +04:00
public void Update(RouteBindingModel model)
{
Name = model.Name;
2023-05-04 20:21:27 +04:00
IP = model.IP;
2023-05-05 17:16:52 +04:00
TransportTypeId = model.TransportTypeId;
}
public RouteViewModel GetViewModel
{
get
{
using var context = new TransportGuideDB();
return new RouteViewModel
{
Id = Id,
TransportTypeId = TransportTypeId,
IP = IP,
Name = Name,
TransportTypeName = context.TransportTypes.FirstOrDefault(x => x.Id == TransportTypeId)?.Name ?? string.Empty,
2023-05-05 17:16:52 +04:00
StopRoutes = StopRoutes
};
}
}
2023-05-04 20:21:27 +04:00
public void UpdateStops(TransportGuideDB context, RouteBindingModel model)
{
var routeStops = context.StopRoutes.Where(rec => rec.RouteId == model.Id).ToList();
if (routeStops != null && routeStops.Count > 0)
{ // удалили те, которых нет в модели
context.StopRoutes.RemoveRange(routeStops.Where(rec => !model.StopRoutes.ContainsKey(rec.StopId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateStop in routeStops)
{
updateStop.Nomer = model.StopRoutes[updateStop.StopId].Item2;
model.StopRoutes.Remove(updateStop.StopId);
}
context.SaveChanges();
}
var route = context.Routes.First(x => x.Id == Id);
foreach (var pc in model.StopRoutes)
{
context.StopRoutes.Add(new StopRoute
{
Route = route,
Stop = context.Stops.First(x => x.Id == pc.Key),
Nomer = pc.Value.Item2
});
context.SaveChanges();
}
_stopRoutes = null;
}
}
}