-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccff46d
commit 24b3691
Showing
39 changed files
with
3,622 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+242 KB
ExamPrep-Stations-04-May-2017/11. DB-Advanced-EF-Core-Exam-Preparation-2-Stations.docx
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
namespace Stations.App | ||
{ | ||
using System; | ||
using System.IO; | ||
using AutoMapper; | ||
using Stations.Data; | ||
|
||
public class Startup | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var context = new StationsDbContext(); | ||
ResetDatabase(context); | ||
|
||
Console.WriteLine("Database Reset."); | ||
|
||
Mapper.Initialize(cfg => cfg.AddProfile<StationsProfile>()); | ||
|
||
ImportEntities(context); | ||
ExportEntities(context); | ||
} | ||
|
||
private static void ImportEntities(StationsDbContext context, string baseDir = @"C:\Users\Petya\Desktop\SoftUniPavel\ExamPrep\Stations.DataProcessor\Datasets\") | ||
{ | ||
const string exportDir = "./ImportResults/"; | ||
|
||
var stations = DataProcessor.Deserializer.ImportStations(context, File.ReadAllText(baseDir + "stations.json")); | ||
PrintAndExportEntityToFile(stations, exportDir + "Stations.txt"); | ||
|
||
// Environment.Exit(0); | ||
|
||
var classes = DataProcessor.Deserializer.ImportClasses(context, File.ReadAllText(baseDir + "classes.json")); | ||
PrintAndExportEntityToFile(classes, exportDir + "Classes.txt"); | ||
|
||
var trains = DataProcessor.Deserializer.ImportTrains(context, File.ReadAllText(baseDir + "trains.json")); | ||
PrintAndExportEntityToFile(trains, exportDir + "Trains.txt"); | ||
|
||
var trips = DataProcessor.Deserializer.ImportTrips(context, File.ReadAllText(baseDir + "trips.json")); | ||
PrintAndExportEntityToFile(trips, exportDir + "Trips.txt"); | ||
|
||
var cards = DataProcessor.Deserializer.ImportCards(context, File.ReadAllText(baseDir + "cards.xml")); | ||
PrintAndExportEntityToFile(cards, exportDir + "Cards.txt"); | ||
|
||
var tickets = DataProcessor.Deserializer.ImportTickets(context, File.ReadAllText(baseDir + "tickets.xml")); | ||
PrintAndExportEntityToFile(tickets, exportDir + "Tickets.txt"); | ||
} | ||
|
||
private static void ExportEntities(StationsDbContext context) | ||
{ | ||
const string exportDir = "./ImportResults/"; | ||
|
||
var jsonOutput = DataProcessor.Serializer.ExportDelayedTrains(context, "01/01/2017"); | ||
Console.WriteLine(jsonOutput); | ||
//File.WriteAllText(exportDir + "DelayedTrains.json", jsonOutput); | ||
|
||
//Console.Clear(); | ||
|
||
var xmlOutput = DataProcessor.Serializer.ExportCardsTicket(context, "Elder"); | ||
Console.WriteLine(xmlOutput); | ||
//File.WriteAllText(exportDir + "CardsTicket.xml", xmlOutput); | ||
} | ||
|
||
private static void PrintAndExportEntityToFile(string entityOutput, string outputPath) | ||
{ | ||
Console.WriteLine(entityOutput); | ||
//File.WriteAllText(outputPath, entityOutput.TrimEnd()); | ||
} | ||
|
||
private static void ResetDatabase(StationsDbContext context) | ||
{ | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
ExamPrep-Stations-04-May-2017/Stations.App/Stations.App.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Automapper" Version="6.2.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Stations.DataProcessor\Stations.DataProcessor.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
13 changes: 13 additions & 0 deletions
13
ExamPrep-Stations-04-May-2017/Stations.App/StationsProfile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Linq; | ||
using AutoMapper; | ||
|
||
namespace Stations.App | ||
{ | ||
public class StationsProfile : Profile | ||
{ | ||
// Configure your AutoMapper here if you wish to use it. If not, DO NOT DELETE THIS CLASS | ||
public StationsProfile() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Stations.Data | ||
{ | ||
public static class Configuration | ||
{ | ||
public static string ConnectionString = @"Server=.;Database=Stations;Trusted_Connection=True"; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
ExamPrep-Stations-04-May-2017/Stations.Data/Stations.Data.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Stations.Models\Stations.Models.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
55 changes: 55 additions & 0 deletions
55
ExamPrep-Stations-04-May-2017/Stations.Data/StationsDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Stations.Models; | ||
|
||
namespace Stations.Data | ||
{ | ||
public class StationsDbContext : DbContext | ||
{ | ||
public StationsDbContext() | ||
{ | ||
} | ||
|
||
public StationsDbContext(DbContextOptions options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public DbSet<Train> Trains { get; set; } | ||
public DbSet<SeatingClass> SeatingClasses { get; set; } | ||
public DbSet<Station> Stations { get; set; } | ||
public DbSet<TrainSeat> TrainSeats { get; set; } | ||
public DbSet<Trip> Trips { get; set; } | ||
public DbSet<Ticket> Tickets { get; set; } | ||
public DbSet<CustomerCard> Cards { get; set; } | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
if (!optionsBuilder.IsConfigured) | ||
{ | ||
optionsBuilder.UseSqlServer(Configuration.ConnectionString); | ||
} | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<SeatingClass>().HasAlternateKey(x => x.Name); | ||
modelBuilder.Entity<SeatingClass>().HasAlternateKey(x => x.Abbreviation); | ||
|
||
modelBuilder.Entity<Station>().HasAlternateKey(x => x.Name); | ||
|
||
modelBuilder.Entity<Train>().HasAlternateKey(x => x.TrainNumber); | ||
|
||
modelBuilder.Entity<Trip>() | ||
.HasOne(x => x.OriginStation) | ||
.WithMany(os => os.TripsFrom) | ||
.HasForeignKey(x => x.OriginStationId) | ||
.OnDelete(DeleteBehavior.Restrict); | ||
|
||
modelBuilder.Entity<Trip>() | ||
.HasOne(x => x.DestinationStation) | ||
.WithMany(ds => ds.TripsTo) | ||
.HasForeignKey(x => x.DestinationStationId) | ||
.OnDelete(DeleteBehavior.Restrict); | ||
} | ||
} | ||
} |
Oops, something went wrong.