-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerService.cs
More file actions
501 lines (421 loc) · 18.4 KB
/
Copy pathCustomerService.cs
File metadata and controls
501 lines (421 loc) · 18.4 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
namespace MBDEVproAPI.BLL.Services {
public class CustomerService : ICustomerService
{
#region variables & constructors
private readonly MBDEVproAPIDbContext _context;
//private IHttpContextAccessor _contextAccessor;
/// <summary>
/// Base Repository
/// </summary>
private readonly ICustomerRepository _customerRepository;
///// <summary>
///// memory cache
///// </summary>
//private IMemoryCache _memoryCache;
///// <summary>
///// configuration
///// </summary>
//public IConfiguration _configuration { get; }
public CustomerService(MBDEVproAPIDbContext context, ICustomerRepository customerRepository)
{
_context = context;
_customerRepository = customerRepository;
}
#endregion
#region
#endregion
#region Get All Customers
/// <summary>
/// GET: Get All Customers | CustomerViewModel | Gets all customers for a business in a VM for web UI.
/// TEST URL: https://localhost:7092/api/Customer/GetAllCustomers/52466 | https://localhost:7092/api/Customer/GetAllCustomers?BusinessID=52466
/// "CustomerControllerGetAllCustomersVMAsync": "Customer/GetAllCustomersVMAsync",
/// </summary>
/// <param name="BusinessID"></param>
/// <returns></returns>
public async Task<CustomerViewModel> GetAllCustomersVMAsync(int BusinessID)
{
try
{
if (BusinessID == 0)
{
Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (BusinessID == 0)");
return new CustomerViewModel();
}
else
{
CustomerViewModel model = new CustomerViewModel();
var customers = await _customerRepository.GetAllCustomersVMAsync(BusinessID);
if (customers == null)
{
Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (customers == null)");
return new CustomerViewModel();
}
model.CustomerList = customers.Select(o => Mapper.MapObject(o, new CustomerModel())).ToList();
return model;
}
}
catch (Exception ex)
{
Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (" + ex + ")" + " (" + ex.InnerException + ")");
return new CustomerViewModel();
}
}
/// <summary>
/// GET: Get All Customers | CustomerModel | Gets all customers for a business.
/// TEST URL: https://localhost:7092/api/Customer/GetAllCustomers?BusinessID=52466
/// "CustomerControllerGetAllCustomersAsync": "Customer/GetAllCustomersAsync",
/// </summary>
/// <param name="BusinessID"></param>
/// <returns></returns>
public async Task<IEnumerable<CustomerModel>> GetAllCustomersAsync(int BusinessID)
{
try
{
if (BusinessID == 0)
{
Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (BusinessID == 0)");
return new List<CustomerModel>();
}
else
{
// Await the repository task first, then project the results.
var customers = await _customerRepository.GetAllCustomersAsync(BusinessID);
if (customers == null)
{
Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (entities == null)");
return new List<CustomerModel>();
}
var entities = customers.Select(o => Mapper.MapObject(o, new CustomerModel())).ToList();
return entities;
}
}
catch (Exception ex)
{
Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (" + ex + ")" + " (" + ex.InnerException + ")");
return new List<CustomerModel>();
}
}
#endregion
#region Get Customer
/// <summary>
/// GET: Get Customer | Customer | Gets a customer for a business.
/// TEST URL: https://localhost:7092/api/Customer/GetCustomer/3 | https://localhost:7092/api/Customer/GetCustomer?CustomerID=3
/// "CustomerControllerGetCustomer": "Customer/GetCustomer",
/// </summary>
/// <param name="CustomerID"></param>
/// <returns></returns>
public async Task<Customer> GetCustomerAsync(int CustomerID) // can jsut do Customer here instead of CustomerModel
{
try
{
if (CustomerID == 0)
{
Log.Error("Customer API: CustomerService(GetCustomerAsync); (CustomerID == 0)");
return new Customer();
}
else
{
var customer = await _customerRepository.GetCustomerAsync(CustomerID);
if (customer == null)
{
Log.Error("Customer API: CustomerService(GetCustomerAsync); (customer == null)");
return new Customer();
}
return customer;
}
}
catch (Exception ex)
{
Log.Error("Customer API: CustomerService(GetCustomerAsync); (" + ex + ")" + " (" + ex.InnerException + ")");
return new Customer();
}
}
#endregion
#region Add Customer
/// <summary>
/// Add Customer | CustomerViewModel | Create a new customer for a business from client web application using a CustomerViewModel.
/// </summary>
/// <param name="vm"></param>
/// <returns></returns>
[HttpPost]
public async Task<SaveViewModel> CreateCustomerVMAsync(CustomerViewModel vm)
{
try
{
if (vm == null || vm.CustomerID != 0 || vm.BusinessID == 0)// we could check each condition here and log which is the issue.
{
Log.Error("Customer API: CustomerService(CreateCustomerVMAsync); (vm == null || vm.CustomerID != 0 || vm.BusinessID == 0");
return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Create the Customer." };
}
else
{
var entity = new DataModel.Entities.Customer();
if (entity == null)
{
Log.Error("Customer API: CustomerService(CreateCustomerVMAsync); (entity == null)");
return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Create the Customer." };
}
else
{
int? refID;
using (TransactionScope scope = new TransactionScope())
{
Mapper.MapObject(vm, entity);
_customerRepository.Add(entity);
_customerRepository.SaveChanges();
refID = entity.CustomerID;
scope.Complete();
}
return new SaveViewModel(refID);
}
}
}
catch (Exception ex)
{
Log.Error("Customer API: CustomerService(CreateCustomerVMAsync); (" + ex + ")" + " (" + ex.InnerException + ")");
return new SaveViewModel(ex.Message);
}
finally
{
}
}
/// <summary>
/// Add Customer | CustomerModel | Create a new customer for a business.
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<SaveViewModel> CreateCustomerAsync(CustomerModel model)
{
try
{
if (model == null || model.CustomerID != 0 || model.BusinessID == 0)// we could check each condition here and log which is the issue.
{
Log.Error("Customer API: CustomerService(CreateCustomerAsync); (model == null || model.CustomerID != 0 || model.BusinessID == 0");
return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Create the Customer." };
}
else
{
var entity = new DataModel.Entities.Customer();
if (entity == null)
{
Log.Error("Customer API: CustomerService(CreateCustomerAsync); (entity == null)");
return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Create the Customer." };
}
else
{
int? refID;
using (TransactionScope scope = new TransactionScope())
{
Mapper.MapObject(model, entity);
_customerRepository.Add(entity);
_customerRepository.SaveChanges();
refID = entity.CustomerID;
scope.Complete();
}
return new SaveViewModel(refID);
}
}
}
catch (Exception ex)
{
Log.Error("Customer API: CustomerService(CreateCustomerAsync); (" + ex + ")" + " (" + ex.InnerException + ")");
return new SaveViewModel(ex.Message);
}
finally
{
}
}
/// <summary>
/// CREATE: Customer
/// "CustomerControllerCreateCustomer": "Customer/CreateCustomer"
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public SaveViewModel CreateCustomer(CustomerModel model)
{
throw new NotImplementedException();
}
#endregion
#region Edit Customer
/// <summary>
/// EDIT: Edit a Customer | CustomerViewModel | edit a customer for a business in a VM for web UI.
/// TEST URL: |
/// "CustomerControllerEditCustomerVMAsync": "Customer/EditCustomerVMAsync",
/// </summary>
/// <param name="vm"></param>
/// <returns>SaveViewModel</returns>
public async Task<SaveViewModel> EditCustomerVMAsync(CustomerViewModel vm)
{
try
{
if (vm == null || vm.CustomerID == 0 || vm.BusinessID == 0)
{
Log.Error("Customer API: CustomerService(EditCustomerVMAsync); (vm == null || vm.CustomerID == 0 || vm.BusinessID == 0)");
return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer." };
}
else
{
var entity = await _customerRepository.GetCustomerAsync(vm.CustomerID);
if (entity == null)
{
Log.Error("Customer API: CustomerService(EditCustomerVMAsync); (entity == null)");
return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer. (entity == null)" };
}
else
{
int? refID;
using (TransactionScope scope = new TransactionScope())
{
Mapper.MapObject(vm, entity);
_customerRepository.SaveChanges();
refID = entity.CustomerID;
scope.Complete();
}
return new SaveViewModel(refID);
}
}
}
catch (Exception ex)
{
Log.Error("Customer API: CustomerService(EditCustomerVMAsync); (" + ex + ")" + " (" + ex.InnerException + ")");
return new SaveViewModel(ex.Message);
}
finally
{
}
}
/// <summary>
/// EDIT: Customer | CustomerModel | edit a customer for a business.
/// "CustomerControllerEditCustomer": "Customer/EditCustomer"
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<SaveViewModel> EditCustomer(CustomerModel model)
{
try
{
if (model == null || model.CustomerID == 0 || model.BusinessID == 0)
{
Log.Error("Customer API: CustomerService(EditCustomer) CustomerModel; (model == null || model.CustomerID == 0 || model.BusinessID == 0)");
return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer. Null Values" };
}
else
{
var entity = await _customerRepository.GetCustomerAsync(model.CustomerID);
if (entity == null)
{
Log.Error("Customer API: CustomerService(EditCustomer) CustomerModel; (entity == null)");
return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer." };
}
else
{
int? refID;
refID = entity.CustomerID;
using (TransactionScope scope = new TransactionScope())
{
Mapper.MapObject(model, entity);
_customerRepository.SaveChanges();
//refID = entity.CustomerID;
scope.Complete();
}
return new SaveViewModel(refID);
}
}
}
catch (Exception ex)
{
Log.Error("Customer API: CustomerService(EditCustomer) CustomerModel; (" + ex + ")" + " (" + ex.InnerException + ")");
return new SaveViewModel(ex.Message);
}
finally
{
}
}
// CustomerModel to just return a CustomerModel instead of a SaveViewModel with the RefID. We could do this for the VM as well.
#endregion
#region Delete Customer
/// <summary>
/// DELETE: Customer | SaveViewModel | delete a customer for a business and return a SaveViewModel with the RefID of the deleted customer.
/// "CustomerControllerDeleteCustomerVM": "Customer/DeleteCustomerVM"
/// </summary>
/// <param name="id"></param>
/// <returns>SaveViewModel</returns>
public async Task<SaveViewModel> DeleteCustomerVMAsync(int CustomerID)
{
try
{
int? refID = null;
if (CustomerID == 0)
{
Log.Error("Customer API: CustomerService(DeleteCustomerVMAsync); (CustomerID == 0)");
return new SaveViewModel("Please provide details to delete the Customer.");
}
else
{
var entity = await _customerRepository.GetCustomerAsync(CustomerID);
if (entity == null)
{
Log.Error("Customer API: CustomerService(DeleteCustomerVMAsync); (entity == null)");
return new SaveViewModel("Please provide details to delete the Customer.");
}
else
{
using (TransactionScope scope = new TransactionScope())
{
_customerRepository.Remove(entity);
_customerRepository.SaveChanges();
scope.Complete();
refID = entity.CustomerID;
}
return new SaveViewModel(refID);
}
}
}
catch (Exception ex)
{
Log.Error("Customer API: CustomerService(DeleteCustomerVMAsync); (" + ex + ")" + " (" + ex.InnerException + ")");
return new SaveViewModel("Customer API: CustomerService(DeleteCustomerVMAsync); (" + ex + ")" + " (" + ex.InnerException + ") Message: " + ex.Message);
}
finally
{
}
}
/// <summary>
/// DELETE: Customer | CustomerModel | delete a customer for a business.
/// "CustomerControllerDeleteCustomer": "Customer/DeleteCustomer"
/// </summary>
/// <param name="id"></param>
/// <returns>SaveViewModel</returns>
public async Task<CustomerModel> DeleteCustomer(int CustomerID)
{ // More to do here
if (CustomerID == 0)
{
Log.Error("Customer API: CustomerService(DeleteCustomer); (CustomerID == 0)");
return new CustomerModel();
}
else {
var entity = await _customerRepository.GetCustomerAsync(CustomerID);
if (entity == null)
{
Log.Error("Customer API: CustomerService(DeleteCustomer); (entity == null)");
return new CustomerModel();
}
else
{
int? refID = null;
using (TransactionScope scope = new TransactionScope())
{
_customerRepository.Remove(entity);
_customerRepository.SaveChanges();
scope.Complete();
refID = entity.CustomerID;
}
return Mapper.MapObject(entity, new CustomerModel());
}
}
}
#endregion
#region Other
#endregion
}
}