-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCyanairDataConnection.cs
283 lines (208 loc) · 12.5 KB
/
CyanairDataConnection.cs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SQLite;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity.Core.Metadata.Edm;
using System.Windows.Forms;
//using System.Windows.Forms;
namespace Cyanair_Airline_Booking_System
{
public class CyanairDataConnection
{
static string myDataSource = @"Data Source = C:\\data\\Cyanair.db";
public bool ableToConnect = false;
public string FileFailed;
public FlightRoute OutboundFlight01;
public FlightRoute OutboundFlight02;
// public PassengerDetails OutboundFlight01;
// public PassengerDetails OutboundFlight02;
public PassengerDetails newBookingObject;
public SystemUsers mySystemUsers;
public string BookingReference;
//SQL DATA CONNECTION TO THE SQLITEDATABASE USING THE SQLCONNECTION COMPONENT
public SQLiteConnection CyanairDBconn = new SQLiteConnection(myDataSource);
//DATASET THAT WILL HOLD DATATABLES
public DataSet CyanairDataSet = new DataSet();
//SQL ADAPTER
public SQLiteDataAdapter CyanairDataAdapter = new SQLiteDataAdapter();
//DATATABLE
private DataTable CyanairDataTable = new DataTable();
//connection constructor
public CyanairDataConnection(string myDatabaseFile)
{
if (File.Exists(myDatabaseFile))
{
myDataSource += myDatabaseFile;
ableToConnect = true;
}
else
{
FileFailed = myDatabaseFile;
/*
System.Windows.Forms.MessageBox.Show("This program could not load the required database file. The database file should be " +
"located in the path = C:\\Data\\Cyanair.db. Please locate the file and try again.", "WARNING - MISSING FILE");
//CLOSES THE ENTIRE PROGRAM IF THERE IS NO DATABASE FILE
Environment.Exit(0); */
}
}
//checks if connection is open method
//use this when we want to write soemthing to the database we will check and run this method!!
//checks if the state of db is open
//prevents accidentally opening an open DB
//if the DB is closed, it will open it
//if it is open it will leave the DB open
public void openDB()
{
if (CyanairDBconn.State != System.Data.ConnectionState.Open)
{
CyanairDBconn.Open();
}
}
//this method CLOSES the DB
public void closeDB()
{
if (CyanairDBconn.State == System.Data.ConnectionState.Open)
{
CyanairDBconn.Close();
}
}
//FUNCTION TO FILL COMBOBOX WITH THE DESTINATION/DEPARTURE AIRPORT LEG 1 AND LEG 2
public void AirportDataFromDBWithFilter(string ExcludeAirportFromList, string SelectedTable)
{
if (CyanairDataSet.Tables.Contains(SelectedTable))
{
CyanairDataSet.Tables.Remove(SelectedTable);
}
this.openDB(); //opens the database if not already opened ! makes sure its available
string mySQLcommandstring = "SELECT * from Airport WHERE Guid != '"+ ExcludeAirportFromList + "' ORDER BY Airport ;";
CyanairDataAdapter = new SQLiteDataAdapter(mySQLcommandstring, this.CyanairDBconn);
CyanairDataAdapter.Fill(CyanairDataSet, SelectedTable);
}
//FUNCTION TO FILL COMBOBOX WITH THE DESTINATION/DEPARTURE AIRPORT LEG 1 AND LEG 2
public void AirportDataFromDB()
{
this.openDB(); //opens the database if not already opened ! makes sure its available
string mySQLcommandstring = "SELECT * from Airport ORDER BY Airport ;";
CyanairDataAdapter = new SQLiteDataAdapter(mySQLcommandstring, this.CyanairDBconn);
CyanairDataAdapter.Fill(CyanairDataSet, "DepartureLeg1Airport");
CyanairDataAdapter.Fill(CyanairDataSet, "DestinationLeg1Airport");
CyanairDataAdapter.Fill(CyanairDataSet, "DepartureLeg2Airport");
CyanairDataAdapter.Fill(CyanairDataSet, "DestinationLeg2Airport");
}
//FUNCTION TO FILL COMBOBOX WITH THE TYPE OF SEATS AVAILABLE
public void TypeOfSeatsData()
{
this.openDB(); //opens the database if not already opened ! makes sure its available
string mySQLcommandstring = "SELECT * from SeatClass ;";
CyanairDataAdapter = new SQLiteDataAdapter(mySQLcommandstring, this.CyanairDBconn);
CyanairDataAdapter.Fill(CyanairDataSet, "ClassTypeLeg1");
CyanairDataAdapter.Fill(CyanairDataSet, "ClassTypeLeg2");
}
//LOGIN METHOD FUNCTION
public bool Login(string UserName, string Passwordy,SystemUsers mySystemUsers)
{
bool CanAccess = false;
this.openDB(); //opens the database if not already opened ! makes sure its available
string mySQLcommandstring = "SELECT Guid,Role FROM Users WHERE Username='"+ UserName + "' AND Password='"+ Passwordy + "';";
DataTable loginDt = new DataTable();
CyanairDataAdapter = new SQLiteDataAdapter(mySQLcommandstring, this.CyanairDBconn);
CyanairDataAdapter.Fill(loginDt);
if (loginDt.Rows.Count > 0) //> 1
{
// MessageBox.Show("You have logged in ! ");
mySystemUsers.BookedByUser_Guid = loginDt.Rows[0].ItemArray[0].ToString();
mySystemUsers.UserRole = loginDt.Rows[0].ItemArray[1].ToString();
CanAccess = true;
}
this.closeDB();
return CanAccess;
}
public void InsertPassengerDetails(PassengerDetails LocalPassengerObject)
{
}
public string InsertNewBooking(FlightRoute OutboundFlight01, FlightRoute OutboundFlight02, Bookings newBookingObject, SystemUsers mySystemUsers)
{
string myNewBookingRef = newBookingObject.CreateNewBookingReference();
this.openDB();
//creating a new guid so it can be inserted into the database
Guid myPassengerDetailsGuid = Guid.NewGuid();
string mySQLCommandString = @"INSERT INTO PassengerDetails (Guid,PassengerFullName,PassportNumber,Nationality,
PhoneNumber,EmailAddress,Gender) VALUES ('"
+ myPassengerDetailsGuid.ToString() + "','"
+ newBookingObject.fullname + "','"
+ newBookingObject.passportnumber + "','"
+ newBookingObject.nationality + "','"
+ newBookingObject.phonenumber + "','"
+ newBookingObject.emailaddress + "','"
+ newBookingObject.gender + "')";
CyanairDataAdapter = new SQLiteDataAdapter(mySQLCommandString, this.CyanairDBconn);
SQLiteCommand sqlInsertNewPassenger = new SQLiteCommand(mySQLCommandString, CyanairDBconn);
sqlInsertNewPassenger.ExecuteNonQuery();
this.openDB();
Guid myBookingsGuid = Guid.NewGuid();
mySQLCommandString = @"INSERT INTO Booking (Guid,BookingReference,DepartureLeg1Airport_Guid,DestinationLeg1Airport_Guid,
DepartureLeg2Airport_Guid,DestinationLeg2Airport_Guid,
Leg1FlightDate,Leg2FlightDate,Leg1FlightTime,Leg2FlightTime,PassengerDetails_Guid,
DateOfBooking,BookedByUser_Guid,Leg1FlightNo,Leg2FlightNo)
VALUES ('"
+ myBookingsGuid.ToString() + "','"
+ myNewBookingRef + "','"
+ OutboundFlight01.DepartureAirport_guid + "','"
+ OutboundFlight01.DestinationAirport_guid + "','"
+ OutboundFlight02.DepartureAirport_guid + "','"
+ OutboundFlight02.DestinationAirport_guid + "','"
+ OutboundFlight01.DateOfFlight + "','"
+ OutboundFlight02.DateOfFlight + "','"
+ OutboundFlight01.TimeOfFlight + "','"
+ OutboundFlight02.TimeOfFlight + "','"
+ myPassengerDetailsGuid.ToString() + "','"
+ DateTime.Now.ToString("dd/MM/yyyy") + "','"
+ mySystemUsers.BookedByUser_Guid + "','"
+ OutboundFlight01.FlightNo + "','"
+ OutboundFlight02.FlightNo + "')";
CyanairDataAdapter = new SQLiteDataAdapter(mySQLCommandString, this.CyanairDBconn);
SQLiteCommand sqlInsertNewBooking = new SQLiteCommand(mySQLCommandString, CyanairDBconn);
sqlInsertNewBooking.ExecuteNonQuery();
return myNewBookingRef;
}
//CHECKING FLIGHT AVAILABILITY FUNCTION
public bool FlightAvailability(FlightRoute myFlightRoutetoCheck)
{
DataTable dt = new DataTable();
bool FlightisOK = false;
string myDateOnly = myFlightRoutetoCheck.DateOfFlight.Substring(0, 10); // 12/06/2020
this.openDB(); //opens the database if not already opened ! makes sure its available
string mySQLcommandstring = @"SELECT Uid,FlightNo,TimeOfFlight,Duration FROM FlightRoute " +
" WHERE " +
"DepartureAirport_Guid='" + myFlightRoutetoCheck.DepartureAirport_guid + "' AND " +
"DestinationAirport_Guid='" + myFlightRoutetoCheck.DestinationAirport_guid + "' AND " +
"DateOfFlight='" + myDateOnly + "'" +
"; ";
CyanairDataAdapter = new SQLiteDataAdapter(mySQLcommandstring, this.CyanairDBconn);
CyanairDataAdapter.Fill(dt);
if (dt.Rows.Count > 0)
{
FlightisOK = true;
myFlightRoutetoCheck.FlightNo = dt.Rows[0].ItemArray[1].ToString();
myFlightRoutetoCheck.TimeOfFlight = dt.Rows[0].ItemArray[2].ToString();
myFlightRoutetoCheck.Duration = dt.Rows[0].ItemArray[3].ToString();
}
return FlightisOK;
}
public string GetAirportfromGUID(string AirportGuid)
{
DataTable dt = new DataTable();
this.openDB(); //opens the database if not already opened ! makes sure its available
string mySQLcommandstring = "SELECT * from Airport WHERE Guid='" + AirportGuid + "' ;";
CyanairDataAdapter = new SQLiteDataAdapter(mySQLcommandstring, this.CyanairDBconn);
CyanairDataAdapter.Fill(dt);
//FILLS IN THE FORM WITH THE AIRPORT NAME AND CODE !
return dt.Rows[0].ItemArray[2].ToString() + " (" + dt.Rows[0].ItemArray[3].ToString() + ")";
}
}
}