5
5
using System . Threading . Tasks ;
6
6
using cocktails . models ;
7
7
using Microsoft . Extensions . Configuration ;
8
+ using System ;
8
9
9
10
namespace cocktails . DB
10
11
{
@@ -30,12 +31,19 @@ public SqlDb(IConfiguration configuration)
30
31
31
32
public SqlConnection GetSQLCn ( )
32
33
{
34
+ var env = _configuration [ "ASPNETCORE_ENVIRONMENT" ] ;
35
+ bool isDevelopment = env == "Development" ;
36
+
33
37
var builder = new SqlConnectionStringBuilder (
34
38
_configuration [ "ConnectionStrings:defaultConnection" ] ) ;
39
+
40
+ if ( isDevelopment )
41
+ {
42
+ // The below 2 lines are used during development only. SMI is used in Production
43
+ var keyVaultSecretLookup = _configuration [ "AzureKeyVaultSecret:defaultSecret" ] ;
44
+ builder . Password = _configuration . GetValue < string > ( keyVaultSecretLookup ) ;
45
+ }
35
46
36
- // The below 2 lines are used during development only. SMI is used in Production
37
- var keyVaultSecretLookup = _configuration [ "AzureKeyVaultSecret:defaultSecret" ] ;
38
- // builder.Password = _configuration.GetValue<string>(keyVaultSecretLookup);
39
47
40
48
SqlConnection sqlDBCn = new SqlConnection ( builder . ConnectionString ) ;
41
49
@@ -161,7 +169,50 @@ private int CRUD(string sqlStatetment)
161
169
return rowsAffected ;
162
170
163
171
}
164
- private async Task < int > CRUDAsync ( string sqlStatetment )
172
+ private async Task < int > CRUDAsync ( string sqlStatetment , Item item )
173
+ {
174
+ int rowsAffected = 0 ;
175
+
176
+ using SqlConnection SQLCn = GetSQLCn ( ) ;
177
+
178
+ using SqlCommand crudCommand = new SqlCommand ( sqlStatetment , SQLCn ) ;
179
+ crudCommand . CommandType = CommandType . Text ;
180
+
181
+ bool IgnoreCase = true ;
182
+ if ( sqlStatetment . StartsWith ( "D" , IgnoreCase , null ) | sqlStatetment . StartsWith ( "U" , IgnoreCase , null ) )
183
+ crudCommand . Parameters . Add ( "@ItemId" , SqlDbType . Int ) . Value = item . Id ;
184
+
185
+ if ( sqlStatetment . StartsWith ( "I" , IgnoreCase , null ) | sqlStatetment . StartsWith ( "U" , IgnoreCase , null ) )
186
+ {
187
+ crudCommand . Parameters . Add ( "@ItemName" , SqlDbType . VarChar , 50 ) . Value = item . Name ;
188
+ var paramPrice = crudCommand . Parameters . Add ( "@ItemPrice" , SqlDbType . Decimal ) ;
189
+ paramPrice . Value = item . Price ;
190
+ paramPrice . Precision = 10 ;
191
+ paramPrice . Scale = 2 ;
192
+ var paramRating = crudCommand . Parameters . Add ( "@ItemRating" , SqlDbType . Decimal ) ;
193
+ paramRating . Value = item . Rating ;
194
+ paramRating . Precision = 10 ;
195
+ paramRating . Scale = 2 ;
196
+ crudCommand . Parameters . Add ( "@ItemImagePath" , SqlDbType . VarChar , 255 ) . Value = item . ImagePath ;
197
+ }
198
+
199
+ try
200
+ {
201
+ await SQLCn . OpenAsync ( ) ;
202
+ rowsAffected = await crudCommand . ExecuteNonQueryAsync ( ) ;
203
+ }
204
+ catch ( Exception Ex )
205
+ {
206
+ string methodReturnValue = Ex . Message ;
207
+ rowsAffected = - 1 ;
208
+ // throw;
209
+ }
210
+
211
+ return rowsAffected ;
212
+
213
+ }
214
+
215
+ private async Task < int > oldCRUDAsync ( string sqlStatetment )
165
216
{
166
217
SqlCommand command ;
167
218
int rowsAffected ;
@@ -182,29 +233,31 @@ private async Task<int> CRUDAsync(string sqlStatetment)
182
233
public async Task < int > DeleteItembyId ( int id )
183
234
{
184
235
int crudResult ;
185
- string sql = $ "Delete from { tblName } where Id = { id } " ;
236
+ Item itemToDelete = new Item { Id = id } ;
186
237
187
- crudResult = await CRUDAsync ( sql ) ;
238
+ string sql = $ "Delete from { tblName } where Id = @ItemId";
239
+
240
+ crudResult = await CRUDAsync ( sql , itemToDelete ) ;
188
241
189
242
return crudResult ;
190
243
}
191
244
192
245
public async Task < int > UpdateItembyId ( Item item )
193
246
{
194
247
int crudResult ;
195
- string sql = $ "Update t Set t.name = ' { item . Name } ' , t.price = { item . Price } , t.rating = { item . Rating } , t.ImagePath = ' { item . ImagePath } ' "
196
- + $ " From { tblName } t where t.id = { item . Id } ";
248
+ string sql = $ "Update t Set t.name = @ItemName , t.price = @ItemPrice , t.rating = @ItemRating , t.ImagePath = @ItemImagePath "
249
+ + $ " From { tblName } t where t.id = @ItemId ";
197
250
198
- crudResult = await CRUDAsync ( sql ) ;
251
+ crudResult = await CRUDAsync ( sql , item ) ;
199
252
200
253
return crudResult ;
201
254
}
202
255
public async Task < int > InsertItem ( Item item )
203
256
{
204
257
int crudResult ;
205
- string sql = $ "Insert into { tblName } (Name, Price ,Rating) values (' { item . Name } ', { item . Price } , { item . Rating } )";
206
-
207
- crudResult = await CRUDAsync ( sql ) ;
258
+ string sql = $ "Insert into { tblName } (Name, Price ,Rating, ImagePath ) values (@ItemName, @ItemPrice, @ItemRating, @ItemImagePath )";
259
+ item . ImagePath = item . ImagePath ?? "NoImageSelected.png" ;
260
+ crudResult = await CRUDAsync ( sql , item ) ;
208
261
209
262
return crudResult ;
210
263
}
0 commit comments