исправления

This commit is contained in:
VictoriaPresnyakova 2023-05-04 20:21:27 +04:00
parent 8b6767e489
commit 064dac213c
7 changed files with 79 additions and 30 deletions

View File

@ -15,7 +15,7 @@ namespace TransportGuideContracts.BindingModels
public int TransportTypeId { get; set; }
public Dictionary<int, (IStopModel, int)> StopNRoute { get; set; } = new();
public Dictionary<int, (IStopModel, int)> StopRoutes { get; set; } = new();
public int Id { get; set; }
}

View File

@ -19,7 +19,7 @@ namespace TransportGuideContracts.ViewModels
[DisplayName("TransportType")]
public string TransportTypeName { get; set; } = string.Empty;
public Dictionary<int, (IStopModel, int)> StopNRoute { get; set; } = new();
public Dictionary<int, (IStopModel, int)> StopRoutes { get; set; } = new();
public int Id { get; set; }
}

View File

@ -13,7 +13,7 @@ namespace TransportGuideDataModels.Models
int TransportTypeId { get;}
Dictionary<int, (IStopModel, int)> StopNRoute { get; }
Dictionary<int, (IStopModel, int)> StopRoutes { get; }
}
}

View File

@ -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<int, (IStopModel, int)>? _stopRoutes = null;
public virtual TransportType TransportType { get; set; }
[NotMapped]
public Dictionary<int, (IStopModel, int)> 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<StopRoute> StopRoutes { get; set; } = new();
public virtual List<StopRoute> 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;
}
}
}

View File

@ -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<Route> Routes { get; set; } = new();
public static TransportType? Create(TransportTypeBindingModel model)
{

View File

@ -11,11 +11,15 @@ namespace TransportGuideDatabaseImplements
{
public class TransportGuideDB : DbContext
{
public TransportGuideDB() { }
public TransportGuideDB(DbContextOptions<TransportGuideDB> 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<Route> Routes { set; get; }
public virtual DbSet<Stop> Stops { set; get; }
public virtual DbSet<StopRoute> StopRoutes { set; get; }

View File

@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
</ItemGroup>
<ItemGroup>