тут собстна осталось сделать TransportGuideDB

This commit is contained in:
VictoriaPresnyakova 2023-05-03 22:48:00 +04:00
parent 0e46bc8b1f
commit 8b6767e489
6 changed files with 236 additions and 6 deletions

View File

@ -0,0 +1,79 @@
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;
using TransportGuideContracts.BindingModels;
using TransportGuideContracts.ViewModels;
using TransportGuideDataModels.Models;
namespace TransportGuideDatabaseImplements.Models
{
public class Route : IRouteModel
{
public int Id { get; private set; }
[Required]
public int TransportTypeId { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public string IP { get; private set; } = string.Empty;
public virtual TransportType transportType { get; set; }
[ForeignKey("RouteId")]
public virtual List<StopRoute> StopRoutes { get; set; } = new();
public static Route? Create(RouteBindingModel? model)
{
if (model == null)
{
return null;
}
return new Route()
{
Id = model.Id,
TransportTypeId = model.TransportTypeId,
IP = model.IP,
Name= model.Name
};
}
public void Update(RouteBindingModel? model)
{
if (model == null)
{
return;
}
TransportTypeId = model.TransportTypeId;
IP = model.IP;
Name = model.Name;
}
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,
};
}
}
}
}

View File

@ -0,0 +1,58 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportGuideContracts.BindingModels;
using TransportGuideContracts.ViewModels;
using TransportGuideDataModels.Models;
namespace TransportGuideDatabaseImplements.Models
{
public class Stop : IStopModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[ForeignKey("StopId")]
public virtual List<StopRoute> StopRoutes { get; set; } = new();
public static Stop? Create(StopBindingModel model)
{
if (model == null)
{
return null;
}
return new Stop()
{
Id = model.Id,
Name = model.Name,
};
}
public static Stop Create(StopViewModel model)
{
return new Stop
{
Id = model.Id,
Name = model.Name,
};
}
public void Update(StopBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
}
public StopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
};
}
}

View File

@ -0,0 +1,22 @@

using System.ComponentModel.DataAnnotations;
namespace TransportGuideDatabaseImplements.Models
{
public class StopRoute
{
public int Id { get; set; }
[Required]
public int StopId { get; set; }
[Required]
public int RouteId { get; set; }
[Required]
public int Nomer { get; set; }
public virtual Route Route { get; set; } = new();
public virtual Stop Stop { get; set; } = new();
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportGuideContracts.BindingModels;
using TransportGuideContracts.ViewModels;
using TransportGuideDataModels.Models;
namespace TransportGuideDatabaseImplements.Models
{
public class TransportType : ITransportTypeModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public double Price { get; set; }
public static TransportType? Create(TransportTypeBindingModel model)
{
if (model == null)
{
return null;
}
return new TransportType()
{
Id = model.Id,
Name = model.Name,
Price = model.Price
};
}
public static TransportType Create(TransportTypeViewModel model)
{
return new TransportType
{
Id = model.Id,
Name = model.Name,
Price = model.Price
};
}
public void Update(TransportTypeBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
Price = model.Price;
}
public TransportTypeViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Price = Price
};
}
}

View File

@ -1,9 +1,11 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportGuideDatabaseImplements.Models;
namespace TransportGuideDatabaseImplements
{
@ -11,10 +13,12 @@ namespace TransportGuideDatabaseImplements
{
public TransportGuideDB() { }
public TransportGuideDB(DbContextOptions<TransportGuideDB> options)
: base(options)
public TransportGuideDB(DbContextOptions<TransportGuideDB> options): base(options)
{
}
public virtual DbSet<Route> Routes { set; get; }
public virtual DbSet<Stop> Stops { set; get; }
public virtual DbSet<StopRoute> StopRoutes { set; get; }
public virtual DbSet<TransportType> TransportTypes { set; get; }
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
@ -7,7 +7,6 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\" />
<Folder Include="Implements\" />
</ItemGroup>
@ -15,4 +14,10 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BusinessLogic\BusinessLogic.csproj" />
<ProjectReference Include="..\TransportGuideContracts\TransportGuideContracts.csproj" />
<ProjectReference Include="..\TransportGuideDataModels\TransportGuideDataModels.csproj" />
</ItemGroup>
</Project>