Skip to content

Commit ca92b1d

Browse files
committed
Added a huge performance improvements for /api/orders and /api/customers #170 #171
1 parent 6e6d98e commit ca92b1d

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

Nop.Plugin.Api/Services/CustomerApiService.cs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
44
using Nop.Core.Data;
@@ -89,7 +89,7 @@ public IList<CustomerDto> Search(string queryParams = "", string order = Configu
8989
// Skip non existing properties.
9090
if (ReflectionHelper.HasProperty(searchParam.Key, typeof(Customer)))
9191
{
92-
92+
9393
// @0 is a placeholder used by dynamic linq and it is used to prevent possible sql injections.
9494
query = query.Where(string.Format("{0} = @0 || {0}.Contains(@0)", searchParam.Key), searchParam.Value);
9595
}
@@ -128,20 +128,15 @@ public CustomerDto GetCustomerById(int id, bool showDeleted = false)
128128

129129
// Here we expect to get two records, one for the first name and one for the last name.
130130
var customerAttributeMappings = (from customer in _customerRepository.Table //NoTracking
131-
join attribute in _genericAttributeRepository.Table//NoTracking
132-
on customer.Id equals attribute.EntityId
133-
where customer.Id == id &&
134-
attribute.KeyGroup.Equals(KeyGroup, StringComparison.InvariantCultureIgnoreCase) &&
135-
(attribute.Key.Equals(FirstName, StringComparison.InvariantCultureIgnoreCase) ||
136-
attribute.Key.Equals(LastName, StringComparison.InvariantCultureIgnoreCase) ||
137-
attribute.Key.Equals(LanguageId, StringComparison.InvariantCultureIgnoreCase) ||
138-
attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase) ||
139-
attribute.Key.Equals(Gender, StringComparison.InvariantCultureIgnoreCase))
140-
select new CustomerAttributeMappingDto
141-
{
142-
Attribute = attribute,
143-
Customer = customer
144-
}).ToList();
131+
join attribute in _genericAttributeRepository.Table//NoTracking
132+
on customer.Id equals attribute.EntityId
133+
where customer.Id == id &&
134+
attribute.KeyGroup == "Customer"
135+
select new CustomerAttributeMappingDto
136+
{
137+
Attribute = attribute,
138+
Customer = customer
139+
}).ToList();
145140

146141
CustomerDto customerDto = null;
147142

@@ -193,7 +188,7 @@ on customer.Id equals attribute.EntityId
193188
{
194189
customerDto.LanguageId = mapping.Attribute.Value;
195190
}
196-
else if(mapping.Attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase))
191+
else if (mapping.Attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase))
197192
{
198193
customerDto.DateOfBirth = string.IsNullOrEmpty(mapping.Attribute.Value) ? (DateTime?)null : DateTime.Parse(mapping.Attribute.Value);
199194
}
@@ -290,12 +285,7 @@ private IList<CustomerDto> HandleCustomerGenericAttributes(IReadOnlyDictionary<s
290285
(from customer in query
291286
from attribute in _genericAttributeRepository.Table
292287
.Where(attr => attr.EntityId == customer.Id &&
293-
attr.KeyGroup.Equals(KeyGroup, StringComparison.InvariantCultureIgnoreCase) &&
294-
(attr.Key.Equals(FirstName, StringComparison.InvariantCultureIgnoreCase) ||
295-
attr.Key.Equals(LastName, StringComparison.InvariantCultureIgnoreCase) ||
296-
attr.Key.Equals(LanguageId, StringComparison.InvariantCultureIgnoreCase) ||
297-
attr.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase) ||
298-
attr.Key.Equals(Gender, StringComparison.InvariantCultureIgnoreCase))).DefaultIfEmpty()
288+
attr.KeyGroup == "Customer").DefaultIfEmpty()
299289
select new CustomerAttributeMappingDto
300290
{
301291
Attribute = attribute,
@@ -319,7 +309,7 @@ from attribute in _genericAttributeRepository.Table
319309
allRecordsGroupedByCustomerId = GetCustomerAttributesMappingsByKey(allRecordsGroupedByCustomerId, LanguageId, searchParams[LanguageId]);
320310
}
321311

322-
if(searchParams.ContainsKey(DateOfBirth))
312+
if (searchParams.ContainsKey(DateOfBirth))
323313
{
324314
allRecordsGroupedByCustomerId = GetCustomerAttributesMappingsByKey(allRecordsGroupedByCustomerId, DateOfBirth, searchParams[DateOfBirth]);
325315
}
@@ -339,7 +329,7 @@ from attribute in _genericAttributeRepository.Table
339329
/// This method is responsible for getting customer dto records with first and last names set from the attribute mappings.
340330
/// </summary>
341331
private IList<CustomerDto> GetFullCustomerDtos(IQueryable<IGrouping<int, CustomerAttributeMappingDto>> customerAttributesMappings,
342-
int page = Configurations.DefaultPageValue, int limit = Configurations.DefaultLimit, string order = Configurations.DefaultOrder)
332+
int page = Configurations.DefaultPageValue, int limit = Configurations.DefaultLimit, string order = Configurations.DefaultOrder)
343333
{
344334
var customerDtos = new List<CustomerDto>();
345335

@@ -398,7 +388,7 @@ private static CustomerDto Merge(IList<CustomerAttributeMappingDto> mappingsForM
398388
{
399389
customerDto.LanguageId = attribute.Value;
400390
}
401-
else if(attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase))
391+
else if (attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase))
402392
{
403393
customerDto.DateOfBirth = string.IsNullOrEmpty(attribute.Value) ? (DateTime?)null : DateTime.Parse(attribute.Value);
404394
}
@@ -483,28 +473,28 @@ private int GetDefaultStoreLangaugeId()
483473
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
484474
private void SetNewsletterSubscribtionStatus(IList<CustomerDto> customerDtos)
485475
{
486-
if(customerDtos == null)
476+
if (customerDtos == null)
487477
{
488478
return;
489479
}
490480

491481
var allNewsletterCustomerEmail = GetAllNewsletterCustomersEmails();
492482

493-
foreach(var customerDto in customerDtos)
483+
foreach (var customerDto in customerDtos)
494484
{
495485
SetNewsletterSubscribtionStatus(customerDto, allNewsletterCustomerEmail);
496486
}
497487
}
498488

499489
private void SetNewsletterSubscribtionStatus(BaseCustomerDto customerDto, IEnumerable<String> allNewsletterCustomerEmail = null)
500490
{
501-
if(customerDto == null || String.IsNullOrEmpty(customerDto.Email))
491+
if (customerDto == null || String.IsNullOrEmpty(customerDto.Email))
502492
{
503493
return;
504494
}
505495

506-
if(allNewsletterCustomerEmail == null)
507-
{
496+
if (allNewsletterCustomerEmail == null)
497+
{
508498
allNewsletterCustomerEmail = GetAllNewsletterCustomersEmails();
509499
}
510500

@@ -519,13 +509,13 @@ private IEnumerable<String> GetAllNewsletterCustomersEmails()
519509
return _cacheManager.Get(Configurations.NEWSLETTER_SUBSCRIBERS_KEY, () =>
520510
{
521511
IEnumerable<String> subscriberEmails = (from nls in _subscriptionRepository.Table
522-
where nls.StoreId == _storeContext.CurrentStore.Id
523-
&& nls.Active
524-
select nls.Email).ToList();
512+
where nls.StoreId == _storeContext.CurrentStore.Id
513+
&& nls.Active
514+
select nls.Email).ToList();
515+
525516

526-
527517
subscriberEmails = subscriberEmails.Where(e => !String.IsNullOrEmpty(e)).Select(e => e.ToLowerInvariant());
528-
518+
529519
return subscriberEmails.Where(e => !String.IsNullOrEmpty(e)).Select(e => e.ToLowerInvariant());
530520
});
531521
}

0 commit comments

Comments
 (0)