From ca92b1d3bb4c077dff047e84f90f176732543c02 Mon Sep 17 00:00:00 2001 From: poyker Date: Wed, 12 Dec 2018 15:56:19 +0200 Subject: [PATCH] Added a huge performance improvements for /api/orders and /api/customers #170 #171 --- Nop.Plugin.Api/Services/CustomerApiService.cs | 62 ++++++++----------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/Nop.Plugin.Api/Services/CustomerApiService.cs b/Nop.Plugin.Api/Services/CustomerApiService.cs index f7b5428..d5fe55d 100644 --- a/Nop.Plugin.Api/Services/CustomerApiService.cs +++ b/Nop.Plugin.Api/Services/CustomerApiService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Nop.Core.Data; @@ -89,7 +89,7 @@ public IList Search(string queryParams = "", string order = Configu // Skip non existing properties. if (ReflectionHelper.HasProperty(searchParam.Key, typeof(Customer))) { - + // @0 is a placeholder used by dynamic linq and it is used to prevent possible sql injections. query = query.Where(string.Format("{0} = @0 || {0}.Contains(@0)", searchParam.Key), searchParam.Value); } @@ -128,20 +128,15 @@ public CustomerDto GetCustomerById(int id, bool showDeleted = false) // Here we expect to get two records, one for the first name and one for the last name. var customerAttributeMappings = (from customer in _customerRepository.Table //NoTracking - join attribute in _genericAttributeRepository.Table//NoTracking - on customer.Id equals attribute.EntityId - where customer.Id == id && - attribute.KeyGroup.Equals(KeyGroup, StringComparison.InvariantCultureIgnoreCase) && - (attribute.Key.Equals(FirstName, StringComparison.InvariantCultureIgnoreCase) || - attribute.Key.Equals(LastName, StringComparison.InvariantCultureIgnoreCase) || - attribute.Key.Equals(LanguageId, StringComparison.InvariantCultureIgnoreCase) || - attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase) || - attribute.Key.Equals(Gender, StringComparison.InvariantCultureIgnoreCase)) - select new CustomerAttributeMappingDto - { - Attribute = attribute, - Customer = customer - }).ToList(); + join attribute in _genericAttributeRepository.Table//NoTracking + on customer.Id equals attribute.EntityId + where customer.Id == id && + attribute.KeyGroup == "Customer" + select new CustomerAttributeMappingDto + { + Attribute = attribute, + Customer = customer + }).ToList(); CustomerDto customerDto = null; @@ -193,7 +188,7 @@ on customer.Id equals attribute.EntityId { customerDto.LanguageId = mapping.Attribute.Value; } - else if(mapping.Attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase)) + else if (mapping.Attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase)) { customerDto.DateOfBirth = string.IsNullOrEmpty(mapping.Attribute.Value) ? (DateTime?)null : DateTime.Parse(mapping.Attribute.Value); } @@ -290,12 +285,7 @@ private IList HandleCustomerGenericAttributes(IReadOnlyDictionary attr.EntityId == customer.Id && - attr.KeyGroup.Equals(KeyGroup, StringComparison.InvariantCultureIgnoreCase) && - (attr.Key.Equals(FirstName, StringComparison.InvariantCultureIgnoreCase) || - attr.Key.Equals(LastName, StringComparison.InvariantCultureIgnoreCase) || - attr.Key.Equals(LanguageId, StringComparison.InvariantCultureIgnoreCase) || - attr.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase) || - attr.Key.Equals(Gender, StringComparison.InvariantCultureIgnoreCase))).DefaultIfEmpty() + attr.KeyGroup == "Customer").DefaultIfEmpty() select new CustomerAttributeMappingDto { Attribute = attribute, @@ -319,7 +309,7 @@ from attribute in _genericAttributeRepository.Table allRecordsGroupedByCustomerId = GetCustomerAttributesMappingsByKey(allRecordsGroupedByCustomerId, LanguageId, searchParams[LanguageId]); } - if(searchParams.ContainsKey(DateOfBirth)) + if (searchParams.ContainsKey(DateOfBirth)) { allRecordsGroupedByCustomerId = GetCustomerAttributesMappingsByKey(allRecordsGroupedByCustomerId, DateOfBirth, searchParams[DateOfBirth]); } @@ -339,7 +329,7 @@ from attribute in _genericAttributeRepository.Table /// This method is responsible for getting customer dto records with first and last names set from the attribute mappings. /// private IList GetFullCustomerDtos(IQueryable> customerAttributesMappings, - int page = Configurations.DefaultPageValue, int limit = Configurations.DefaultLimit, string order = Configurations.DefaultOrder) + int page = Configurations.DefaultPageValue, int limit = Configurations.DefaultLimit, string order = Configurations.DefaultOrder) { var customerDtos = new List(); @@ -398,7 +388,7 @@ private static CustomerDto Merge(IList mappingsForM { customerDto.LanguageId = attribute.Value; } - else if(attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase)) + else if (attribute.Key.Equals(DateOfBirth, StringComparison.InvariantCultureIgnoreCase)) { customerDto.DateOfBirth = string.IsNullOrEmpty(attribute.Value) ? (DateTime?)null : DateTime.Parse(attribute.Value); } @@ -483,14 +473,14 @@ private int GetDefaultStoreLangaugeId() [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")] private void SetNewsletterSubscribtionStatus(IList customerDtos) { - if(customerDtos == null) + if (customerDtos == null) { return; } var allNewsletterCustomerEmail = GetAllNewsletterCustomersEmails(); - foreach(var customerDto in customerDtos) + foreach (var customerDto in customerDtos) { SetNewsletterSubscribtionStatus(customerDto, allNewsletterCustomerEmail); } @@ -498,13 +488,13 @@ private void SetNewsletterSubscribtionStatus(IList customerDtos) private void SetNewsletterSubscribtionStatus(BaseCustomerDto customerDto, IEnumerable allNewsletterCustomerEmail = null) { - if(customerDto == null || String.IsNullOrEmpty(customerDto.Email)) + if (customerDto == null || String.IsNullOrEmpty(customerDto.Email)) { return; } - if(allNewsletterCustomerEmail == null) - { + if (allNewsletterCustomerEmail == null) + { allNewsletterCustomerEmail = GetAllNewsletterCustomersEmails(); } @@ -519,13 +509,13 @@ private IEnumerable GetAllNewsletterCustomersEmails() return _cacheManager.Get(Configurations.NEWSLETTER_SUBSCRIBERS_KEY, () => { IEnumerable subscriberEmails = (from nls in _subscriptionRepository.Table - where nls.StoreId == _storeContext.CurrentStore.Id - && nls.Active - select nls.Email).ToList(); + where nls.StoreId == _storeContext.CurrentStore.Id + && nls.Active + select nls.Email).ToList(); + - subscriberEmails = subscriberEmails.Where(e => !String.IsNullOrEmpty(e)).Select(e => e.ToLowerInvariant()); - + return subscriberEmails.Where(e => !String.IsNullOrEmpty(e)).Select(e => e.ToLowerInvariant()); }); }