From 064dac213c05d3112323ad0accb59bda25bc1c27 Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Thu, 4 May 2023 20:21:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/RouteBindingModel.cs | 2 +- .../ViewModels/RouteViewModel.cs | 2 +- .../Models/IRouteModel.cs | 2 +- .../Models/Route.cs | 86 ++++++++++++++----- .../Models/TransportType.cs | 4 + .../TransportGuideDB.cs | 12 ++- .../TransportGuideDatabaseImplements.csproj | 1 + 7 files changed, 79 insertions(+), 30 deletions(-) diff --git a/TransportGuideContracts/BindingModels/RouteBindingModel.cs b/TransportGuideContracts/BindingModels/RouteBindingModel.cs index 23f61d5..10a45bf 100644 --- a/TransportGuideContracts/BindingModels/RouteBindingModel.cs +++ b/TransportGuideContracts/BindingModels/RouteBindingModel.cs @@ -15,7 +15,7 @@ namespace TransportGuideContracts.BindingModels public int TransportTypeId { get; set; } - public Dictionary StopNRoute { get; set; } = new(); + public Dictionary StopRoutes { get; set; } = new(); public int Id { get; set; } } diff --git a/TransportGuideContracts/ViewModels/RouteViewModel.cs b/TransportGuideContracts/ViewModels/RouteViewModel.cs index d656866..fb014ec 100644 --- a/TransportGuideContracts/ViewModels/RouteViewModel.cs +++ b/TransportGuideContracts/ViewModels/RouteViewModel.cs @@ -19,7 +19,7 @@ namespace TransportGuideContracts.ViewModels [DisplayName("TransportType")] public string TransportTypeName { get; set; } = string.Empty; - public Dictionary StopNRoute { get; set; } = new(); + public Dictionary StopRoutes { get; set; } = new(); public int Id { get; set; } } diff --git a/TransportGuideDataModels/Models/IRouteModel.cs b/TransportGuideDataModels/Models/IRouteModel.cs index dd4dfb3..064dc1c 100644 --- a/TransportGuideDataModels/Models/IRouteModel.cs +++ b/TransportGuideDataModels/Models/IRouteModel.cs @@ -13,7 +13,7 @@ namespace TransportGuideDataModels.Models int TransportTypeId { get;} - Dictionary StopNRoute { get; } + Dictionary StopRoutes { get; } } } diff --git a/TransportGuideDatabaseImplements/Models/Route.cs b/TransportGuideDatabaseImplements/Models/Route.cs index 7e3f999..9f4e451 100644 --- a/TransportGuideDatabaseImplements/Models/Route.cs +++ b/TransportGuideDatabaseImplements/Models/Route.cs @@ -1,13 +1,14 @@ using System; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using TransportGuideContracts.BindingModels; using TransportGuideContracts.ViewModels; using TransportGuideDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; namespace TransportGuideDatabaseImplements.Models { @@ -15,8 +16,6 @@ namespace TransportGuideDatabaseImplements.Models { public int Id { get; private set; } - [Required] - public int TransportTypeId { get; private set; } [Required] @@ -26,38 +25,46 @@ namespace TransportGuideDatabaseImplements.Models public string IP { get; private set; } = string.Empty; - public virtual TransportType transportType { get; set; } + Dictionary? _stopRoutes = null; + + public virtual TransportType TransportType { get; set; } + + + [NotMapped] + public Dictionary StopRoutes { + get + { + if (_stopRoutes == null) + { + _stopRoutes = Stops.ToDictionary(recPC => recPC.StopId, recPC => (recPC.Stop as IStopModel, recPC.Nomer)); + } + return _stopRoutes; + } + + } [ForeignKey("RouteId")] - public virtual List StopRoutes { get; set; } = new(); + public virtual List Stops { get; set; } = new(); - public static Route? Create(RouteBindingModel? model) + public static Route Create(TransportGuideDB context, RouteBindingModel model) { - if (model == null) - { - return null; - } - return new Route() { Id = model.Id, - TransportTypeId = model.TransportTypeId, + Name = model.Name, IP = model.IP, - Name= model.Name + Stops = model.StopRoutes.Select(x => new StopRoute + { + Stop = context.Stops.First(y => y.Id == x.Key), + Nomer = x.Value.Item2 + }).ToList() }; } - public void Update(RouteBindingModel? model) + public void Update(RouteBindingModel model) { - if (model == null) - { - return; - } - - TransportTypeId = model.TransportTypeId; - IP = model.IP; Name = model.Name; - + IP = model.IP; } public RouteViewModel GetViewModel @@ -75,5 +82,38 @@ namespace TransportGuideDatabaseImplements.Models }; } } + + 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; + } + } } diff --git a/TransportGuideDatabaseImplements/Models/TransportType.cs b/TransportGuideDatabaseImplements/Models/TransportType.cs index ee9d6c3..350f33a 100644 --- a/TransportGuideDatabaseImplements/Models/TransportType.cs +++ b/TransportGuideDatabaseImplements/Models/TransportType.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -20,6 +21,9 @@ namespace TransportGuideDatabaseImplements.Models [Required] public double Price { get; set; } + [ForeignKey("TransportTypeId")] + public virtual List Routes { get; set; } = new(); + public static TransportType? Create(TransportTypeBindingModel model) { diff --git a/TransportGuideDatabaseImplements/TransportGuideDB.cs b/TransportGuideDatabaseImplements/TransportGuideDB.cs index 831fb70..e86c391 100644 --- a/TransportGuideDatabaseImplements/TransportGuideDB.cs +++ b/TransportGuideDatabaseImplements/TransportGuideDB.cs @@ -11,11 +11,15 @@ namespace TransportGuideDatabaseImplements { public class TransportGuideDB : DbContext { - public TransportGuideDB() { } - public TransportGuideDB(DbContextOptions options): base(options) - { - } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql("Host=192.168.56.101;Port=5432;Database=TransportSubd;Username=postgres;Password=postgres"); + } + base.OnConfiguring(optionsBuilder); + } public virtual DbSet Routes { set; get; } public virtual DbSet Stops { set; get; } public virtual DbSet StopRoutes { set; get; } diff --git a/TransportGuideDatabaseImplements/TransportGuideDatabaseImplements.csproj b/TransportGuideDatabaseImplements/TransportGuideDatabaseImplements.csproj index b38ff59..24cf535 100644 --- a/TransportGuideDatabaseImplements/TransportGuideDatabaseImplements.csproj +++ b/TransportGuideDatabaseImplements/TransportGuideDatabaseImplements.csproj @@ -12,6 +12,7 @@ +