-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarManager.cs
210 lines (183 loc) · 7.55 KB
/
CarManager.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
using DAL;
using BOL;
using System;
using System.Linq;
namespace BLL
{
static public class CarManager
{
/// <summary>
/// SelectAllBooks reads all the Books from the DB by the EF ref
/// and maps the DAL objects to BOL objects
/// </summary>
static public CarModel[] SelectAllCars()
{
try
{
using (CarsRentalEntities ef = new CarsRentalEntities())
{
return ef.Cars.Select(dbCar => new CarModel
{
CarCurrentKilometerage = dbCar.currentKilometerage,
CarImage = dbCar.image,
CarIsFitForRental = dbCar.isFitForRental,
CarNumber = dbCar.carNumber,
CarBranch = new BranchModel
{
BranchAddress = dbCar.Branch.address,
BranchLatitude = dbCar.Branch.latitude,
BranchLongitude = dbCar.Branch.longitude,
BranchName = dbCar.Branch.branchName,
},
CarType = new CarTypeModel
{
// CarTypeId = dbCar.CarType.carTypeId,
DailyCost = dbCar.CarType.dailyCost,
DayDelayCost = dbCar.CarType.dayDelayCost,
Gear = dbCar.CarType.gear,
Manufacturer = dbCar.CarType.manufacturer,
ManufactureYear = dbCar.CarType.manufactureYear,
Model = dbCar.CarType.model,
}
}).ToArray();
}
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// SelectBookByName selects a specific Book from the DB by the EF ref
/// by the `bookName` parameter
/// and maps the DAL object to BOL object
/// </summary>
static public CarModel SelectCarByCarNumber(string carNumber)
{
try
{
using (CarsRentalEntities ef = new CarsRentalEntities())
{
Car selectedCar = ef.Cars.FirstOrDefault(dbCar => dbCar.carNumber == carNumber);
if (selectedCar == null)
return null;
return new CarModel
{
CarCurrentKilometerage = selectedCar.currentKilometerage,
CarImage = selectedCar.image,
CarIsFitForRental = selectedCar.isFitForRental,
CarNumber = selectedCar.carNumber,
CarBranch = new BranchModel
{
BranchAddress = selectedCar.Branch.address,
BranchLatitude = selectedCar.Branch.latitude,
BranchLongitude = selectedCar.Branch.longitude,
BranchName = selectedCar.Branch.branchName,
},
CarType = new CarTypeModel
{
// CarTypeId = selectedCar.CarType.carTypeId,
DailyCost = selectedCar.CarType.dailyCost,
DayDelayCost = selectedCar.CarType.dayDelayCost,
Gear = selectedCar.CarType.gear,
Manufacturer = selectedCar.CarType.manufacturer,
ManufactureYear = selectedCar.CarType.manufactureYear,
Model = selectedCar.CarType.model,
}
};
}
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// InsertBook inserts a new Book to the DB by the EF ref
/// maps the `newBook` parameter (BOL object) to a `Book` (DAL object)
/// and returns bool value - if the action was success
/// </summary>
static public bool InsertCar(CarModel newCar)
{
try
{
using (CarsRentalEntities ef = new CarsRentalEntities())
{
Branch selectedBranch = ef.Branches.FirstOrDefault(dbBranch => dbBranch.branchName == newCar.CarBranch.BranchName);
if (selectedBranch == null)
return false;
CarType selectedCarType = ef.CarTypes.FirstOrDefault(dbCarType => dbCarType.model == newCar.CarType.Model);
if (selectedCarType == null)
return false;
Car newDbCar = new Car
{
currentKilometerage = newCar.CarCurrentKilometerage,
image = newCar.CarImage,
isFitForRental = newCar.CarIsFitForRental,
carNumber = newCar.CarNumber,
branchId = selectedBranch.BranchId,
carTypeId = selectedCarType.carTypeId,
};
ef.Cars.Add(newDbCar);
ef.SaveChanges();
return true;
}
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// UpdateBookByName updates a specific Book from the DB by the EF ref
/// by the `bookName` parameter
/// and returns bool value - if the action was success
/// </summary>
static public bool UpdateCarByCarNumber(string carNumber, CarModel newCar)
{
try
{
using (CarsRentalEntities ef = new CarsRentalEntities())
{
Car selectedCar = ef.Cars.FirstOrDefault(dbCar => dbCar.carNumber == carNumber);
if (selectedCar == null)
return false;
selectedCar.carNumber = newCar.CarNumber;
selectedCar.currentKilometerage = newCar.CarCurrentKilometerage;
selectedCar.image = newCar.CarImage;
selectedCar.isFitForRental = newCar.CarIsFitForRental;
ef.SaveChanges();
return true;
}
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// DeleteBookByName deletes a specific Book from the DB by the EF ref
/// by the `bookName` parameter
/// and returns bool value - if the action was success
/// </summary>
static public bool DeleteCarByCarNumber(string carNumber)
{
try
{
using (CarsRentalEntities ef = new CarsRentalEntities())
{
Car selectedCar = ef.Cars.FirstOrDefault(dbCar => dbCar.carNumber == carNumber);
if (selectedCar == null)
return false;
ef.Cars.Remove(selectedCar);
ef.SaveChanges();
return true;
}
}
catch (Exception)
{
return false;
}
}
}
}