Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using FlightBooking.Service.Data.Models;
using Microsoft.EntityFrameworkCore;
namespace FlightBooking.Service.Data
{
public static class DatabaseSeeding
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (var context = new FlightBookingContext(serviceProvider.GetRequiredService<DbContextOptions<FlightBookingContext>>()))
{
if (context.FlightInformation.Any())
{
return;
}
string flightA = Guid.NewGuid().ToString("N")[..4].ToUpper();
string flightB = Guid.NewGuid().ToString("N")[..4].ToUpper();
List<FlightFare> flightFaresA = new List<FlightFare>
{
new FlightFare
{
FareName = "Economy class",
FareCode = "Eco-1",
Price = 4000,
SeatCapacity = 30,
SeatReserved = 0,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
},
new FlightFare
{
FareName = "Business class",
FareCode = "Biz-1",
Price = 4000,
SeatCapacity = 30,
SeatReserved = 0,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
},
};
List<FlightFare> flightFaresB = new List<FlightFare>
{
new FlightFare
{
FareName = "Economy class",
FareCode = "Eco-11",
Price = 5000,
SeatCapacity = 20,
SeatReserved = 0,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
},
new FlightFare
{
FareName = "Business class",
FareCode = "Biz-11",
Price = 4000,
SeatCapacity = 10,
SeatReserved = 0,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
},
};
//Create available seats
var reservedSeatsA = GenerateSeats(flightA, 60);
var reservedSeatsB = GenerateSeats(flightB, 60);
List<FlightInformation> flights = new List<FlightInformation>
{
new FlightInformation
{
SeatCapacity = 60,
DepartureDate = DateTime.UtcNow.AddMonths(3),
ArrivalDate = DateTime.UtcNow.AddMonths(3).AddHours(4),
Airline = "Emirates",
SeatReserved = 0,
Destination = "London",
Origin = "Lagos",
FlightNumber = flightA,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
FlightFares = flightFaresA,
ReservedSeats = reservedSeatsA,
},
new FlightInformation
{
SeatCapacity = 40,
DepartureDate = DateTime.UtcNow.AddMonths(3).AddDays(7),
ArrivalDate = DateTime.UtcNow.AddMonths(3).AddDays(7).AddHours(4),
Airline = "Emirates",
SeatReserved = 0,
Destination = "Lagos",
Origin = "London",
FlightNumber = flightB,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
FlightFares = flightFaresB,
ReservedSeats = reservedSeatsB
}
};
context.FlightInformation.AddRange(flights);
context.SaveChanges();
context.Database.EnsureCreated();
}
}
private static List<ReservedSeat> GenerateSeats(string flightNumber, int flightCapacity)
{
//assume seats are in group of 4 Alphabets e.g 1A, 1B, 1C, 1D
Dictionary<int, string> SeatMaps = new Dictionary<int, string>
{
{1, "A" },
{2, "B" },
{3, "C" },
{4, "D" }
};
int seatId = 1;
int seatCount = 1;
List<string> seatNumbers = new List<string>();
for (int i = 1; i < flightCapacity + 1; i++)
{
if (seatCount > 4)
{
seatId++;
seatCount = 1;
}
seatNumbers.Add(seatId + SeatMaps[seatCount]);
seatCount++;
}
List<ReservedSeat> reservedSeats = new List<ReservedSeat>();
foreach (var seatNumber in seatNumbers)
{
reservedSeats.Add(new ReservedSeat
{
BookingNumber = null,
FlightNumber = flightNumber,
IsReserved = false,
SeatNumber = seatNumber
});
}
return reservedSeats;
}
}
}