-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdmin.cs
386 lines (365 loc) · 17.7 KB
/
Admin.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Data.SqlClient;
using System.Linq.Expressions;
namespace MYRIDE
{
internal class Admin
{
List<Driver> drivers;
public Admin()
{
drivers = new List<Driver>();
}
public List<Driver> Drivers { get { return drivers; } }
//We can still load list of Drivers from DB using DB sytax. That's why not removing List<Driver> drivers as Data Member of Admin Class
public void addDriver(string name, int age, string gender,string address, string phoneNo, string vType, string vModel, string vLP)
{
Driver driver = new Driver();
driver.Name = name;
driver.Age = age;
driver.Gender = gender;
driver.Address = address;
driver.PhoneNo = phoneNo;
driver.Vehicle.Type = vType;
driver.Vehicle.Model = vModel;
driver.Vehicle.LicensePlate = vLP;
//store data in Database
string connectionString = "Data Source=(localdb)\\ProjectModels;Initial Catalog=myRideDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
string query = $"INSERT INTO driver (name,age,gender,address,phoneNo,vType, vModel,vLicensePlate) VALUES ('{driver.Name}','{driver.Age}','{driver.Gender}','{driver.Address}','{driver.PhoneNo}','{driver.Vehicle.Type}','{driver.Vehicle.Model}','{driver.Vehicle.LicensePlate}')";
SqlCommand command = new SqlCommand(query,conn);
int status = command.ExecuteNonQuery();
if (status == 1)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Driver is added successfully");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Driver is not added");
Console.ResetColor();
}
conn.Close();
}
public bool setRating(int id, int rating)
{
string connectionString = "Data Source=(localdb)\\ProjectModels;Initial Catalog=myRideDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
SqlConnection conn = new SqlConnection(connectionString);
SqlConnection conn2 = new SqlConnection(connectionString);
conn.Open();
string query = $"SELECT * FROM driver WHERE d_id = {id}";
SqlCommand command = new SqlCommand(query, conn);
SqlDataReader reader = command.ExecuteReader();
int ratingCount = 0;
int prevRating = 0;
if(reader.Read())
{
ratingCount = Convert.ToInt32(reader["ratingCount"]);
prevRating = Convert.ToInt32(reader["rating"]);
prevRating = prevRating + rating;
ratingCount++;
prevRating = prevRating/ratingCount;
query = $"UPDATE driver SET ratingCount = {ratingCount}, rating = {prevRating} WHERE d_id = {id}";
conn2.Open();
command = new SqlCommand(query, conn2);
int status = command.ExecuteNonQuery();
if(status==1)
{
return true;
}
}
conn2.Close();
conn.Close();
return false;
}
public void updateDriverCurrLocation(int id, string currLoc)
{
string connectionString = "Data Source=(localdb)\\ProjectModels;Initial Catalog=myRideDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
SqlConnection conn = new SqlConnection(connectionString);
Location location = new Location();
location.setLocation(currLoc);
string query = $"UPDATE driver SET curr_latitude = {location.Latitude}, curr_longitude = {location.Longitude} WHERE d_id = {id}";
conn.Open();
SqlCommand command = new SqlCommand(query, conn);
command.ExecuteReader();
}
public void removeDriver(int id)
{
string connectionString = "Data Source=(localdb)\\ProjectModels;Initial Catalog=myRideDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
SqlConnection conn = new SqlConnection(connectionString);
string query = $"delete from driver where d_id = {id}";
conn.Open();
SqlCommand command = new SqlCommand(query, conn);
int status = command.ExecuteNonQuery();
if (status == 1)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Record Deleted Successfully");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Record not found");
Console.ResetColor();
}
conn.Close();
}
public void updateDriver(int id)
{
string connectionString = "Data Source=(localdb)\\ProjectModels;Initial Catalog=myRideDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
SqlConnection conn = new SqlConnection(connectionString);
string query = $"SELECT * FROM driver WHERE d_id = {id}";
conn.Open() ;
SqlCommand command = new SqlCommand (query, conn);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
int d_id = Convert.ToInt32(reader[0]);
if(d_id == id)
{
conn.Close() ;
Console.WriteLine("------------Driver with ID " + id + " exists------------");
Console.Write("Enter Name: ");
Console.ForegroundColor = ConsoleColor.Green;
string name = Console.ReadLine();
Console.ResetColor();
if (name != string.Empty)
{
conn.Open();
query = $"UPDATE driver SET name = '{name}' WHERE d_id = {id}";
command = new SqlCommand (query, conn);
command.ExecuteNonQuery();
conn.Close();
}
Console.Write("Enter Age: ");
Console.ForegroundColor = ConsoleColor.Green;
string age = Console.ReadLine();
Console.ResetColor();
if (age != string.Empty)
{
int driverAge = Convert.ToInt32(age);
conn.Open();
query = $"UPDATE driver SET age = '{driverAge}' WHERE d_id = {id}";
command = new SqlCommand(query, conn);
command.ExecuteNonQuery();
conn.Close();
}
Console.Write("Enter Gender: ");
Console.ForegroundColor = ConsoleColor.Green;
string gender = Console.ReadLine();
Console.ResetColor();
if (gender != string.Empty)
{
conn.Open();
query = $"UPDATE driver SET gender = '{gender}' WHERE d_id = {id}";
command = new SqlCommand(query, conn);
command.ExecuteNonQuery();
conn.Close();
}
Console.Write("Enter Address: ");
Console.ForegroundColor = ConsoleColor.Green;
string address = Console.ReadLine();
Console.ResetColor();
if (address != string.Empty)
{
conn.Open();
query = $"UPDATE driver SET address = '{address}' WHERE d_id = {id}";
command = new SqlCommand(query, conn);
command.ExecuteNonQuery();
conn.Close();
}
Console.Write("Enter PhoneNo: ");
Console.ForegroundColor = ConsoleColor.Green;
string phoneNo = Console.ReadLine();
Console.ResetColor();
if (phoneNo != string.Empty)
{
conn.Open();
query = $"UPDATE driver SET phoneNo = '{phoneNo}' WHERE d_id = {id}";
command = new SqlCommand(query, conn);
command.ExecuteNonQuery();
conn.Close();
}
Console.Write("Enter Vehicle Type (Car/Bike/Rickshaw): ");
Console.ForegroundColor = ConsoleColor.Green;
string vehicleType = Console.ReadLine();
Console.ResetColor();
if (vehicleType != string.Empty)
{
conn.Open();
query = $"UPDATE driver SET vType = '{vehicleType}' WHERE d_id = {id}";
command = new SqlCommand(query, conn);
command.ExecuteNonQuery();
conn.Close();
}
Console.Write("Enter Vehicle Model: ");
Console.ForegroundColor = ConsoleColor.Green;
string vehicleModel = Console.ReadLine();
Console.ResetColor();
if (vehicleModel != string.Empty)
{
conn.Open();
query = $"UPDATE driver SET vModel = '{vehicleModel}' WHERE d_id = {id}";
command = new SqlCommand(query, conn);
command.ExecuteNonQuery();
conn.Close();
}
Console.Write("Enter Vehicle License Plate: ");
Console.ForegroundColor = ConsoleColor.Green;
string vehicleLP = Console.ReadLine();
Console.ResetColor();
if (vehicleLP != string.Empty)
{
conn.Open();
query = $"UPDATE driver SET vLicensePlate = '{vehicleLP}' WHERE d_id = {id}";
command = new SqlCommand(query, conn);
command.ExecuteNonQuery();
conn.Close();
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Record Updated!");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Record not found");
Console.ResetColor();
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Record not found");
Console.ResetColor();
}
conn.Close();
}
public void updateAvailability(int id, char val)
{
string connectionString = "Data Source=(localdb)\\ProjectModels;Initial Catalog=myRideDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
string query = $"UPDATE driver Set availability = '{val}' where d_id = {id}";
SqlCommand command = new SqlCommand(query, conn);
int count = command.ExecuteNonQuery();
if(count == 1)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Record Updated!");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Could not update Availability at this time");
Console.ResetColor();
}
}
public void searchDriver()
{
Driver driver = new Driver();
Console.Write("Enter Driver ID: ");
Console.ForegroundColor = ConsoleColor.Green;
string id = Console.ReadLine();
Console.ResetColor();
if (id != string.Empty)
{
int ID = Convert.ToInt32(id);
driver.ID = ID;
}
Console.Write("Enter Name: ");
Console.ForegroundColor = ConsoleColor.Green;
string name = Console.ReadLine();
Console.ResetColor();
if (name != string.Empty)
{
driver.Name = name;
}
Console.Write("Enter Age: ");
Console.ForegroundColor = ConsoleColor.Green;
string age = Console.ReadLine();
Console.ResetColor();
if (age != string.Empty)
{
int ageID = Convert.ToInt32(age);
driver.Age = ageID;
}
Console.Write("Enter Gender: ");
Console.ForegroundColor = ConsoleColor.Green;
string gender = Console.ReadLine();
Console.ResetColor();
if (gender != string.Empty)
{
driver.Gender = gender;
}
Console.Write("Enter Address: ");
Console.ForegroundColor = ConsoleColor.Green;
string address = Console.ReadLine();
Console.ResetColor();
if (address != string.Empty)
{
driver.Address = address;
}
Console.Write("Enter Vehicle Type (Car/Bike/Rickshaw): ");
Console.ForegroundColor = ConsoleColor.Green;
string vehicleType = Console.ReadLine();
Console.ResetColor();
if (vehicleType != string.Empty)
{
driver.Vehicle.Type = vehicleType;
}
Console.Write("Enter Vehicle Model: ");
Console.ForegroundColor = ConsoleColor.Green;
string vehicleModel = Console.ReadLine();
Console.ResetColor();
if (vehicleModel != string.Empty)
{
driver.Vehicle.Model = vehicleModel;
}
Console.Write("Enter Vehicle License Plate: ");
Console.ForegroundColor = ConsoleColor.Green;
string vehicleLP = Console.ReadLine();
Console.ResetColor();
if (vehicleLP != string.Empty)
{
driver.Vehicle.LicensePlate = vehicleLP;
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Results on Basis of Filters");
Console.ResetColor();
string connectionString = "Data Source=(localdb)\\ProjectModels;Initial Catalog=myRideDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
string query = $"SELECT * FROM driver WHERE d_id = {driver.ID} OR name = '{driver.Name}' OR age = {driver.Age} OR address = '{driver.Address}' OR vType = '{driver.Vehicle.Type}' OR vModel = '{driver.Vehicle.Model}' OR vLicensePlate = '{driver.Vehicle.LicensePlate}'";
SqlCommand command = new SqlCommand(query, conn);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("----------------------------------------------------------------------------");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Name Age Gender V.Type V.Model V.License");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("----------------------------------------------------------------------------");
Console.ResetColor();
Console.WriteLine(reader["name"] + " " + reader["age"] + " " + reader["gender"] + " " + reader["vType"] + " " + reader["vModel"] + " " + reader["vLicensePlate"]);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("----------------------------------------------------------------------------");
Console.ResetColor();
}
conn.Close();
}
}
}