-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerService.cs
More file actions
360 lines (283 loc) · 11.4 KB
/
Copy pathCustomerService.cs
File metadata and controls
360 lines (283 loc) · 11.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
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 Get All Customers | CustomerViewModel
/// <summary>
/// GET: Gets all customers for a business in a VM for web UI.
/// </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();
}
}
#endregion
#region Get All Customers | CustomerModel
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
//public IEnumerable<ProjectModel> GetAll()
//{
// try
// {
// var entities = _projectRepository.GetAll().Select(O => Mapper.MapObject(O, new ProjectModel())).ToList();
// if (entities == null)
// {
// throw new Exception("Licensing API: ProjectService(GetAll); (entities == null");
// }
// else
// {
// return entities;
// }
// }
// catch (Exception ex)
// {
// ex.Data.Add("ErrorMessage", "Licensing API: ProjectService(GetAll)");
// throw;
// }
//}
#region Get Customer | Customer
//GetCustomerAsync
/// <summary>
/// GET: Gets a customer for a business.
/// </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 | CustomerViewModel
/// <summary>
/// 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
{
}
}
#endregion
#region Add Customer | Customer
/// <summary>
/// CREATE: Customer
/// "CustomerControllerCreateCustomerAsync": "Customer/CreateCustomerAsync"
/// </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
{
}
}
#endregion
///// <summary>
///// GET: Customer
///// "CustomerControllerGetCustomer": "Customer/GetCustomer"
///// </summary>
///// <param name="id"></param>
///// <returns></returns>
//public CustomerModel GetCustomer(int id)
//{
// throw new NotImplementedException();
//}
/// <summary>
/// CREATE: Customer
/// "CustomerControllerCreateCustomer": "Customer/CreateCustomer"
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public SaveViewModel CreateCustomer(CustomerModel model)
{
throw new NotImplementedException();
}
/// <summary>
/// EDIT: Customer
/// "CustomerControllerEditCustomer": "Customer/EditCustomer"
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public SaveViewModel EditCustomer(int id, CustomerModel model)
{
throw new NotImplementedException();
}
/// <summary>
/// DELETE: Customer
/// "CustomerControllerDeleteCustomer": "Customer/DeleteCustomer"
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public SaveViewModel DeleteCustomer(int id)
{
throw new NotImplementedException();
}
}
}