public CustomerDto GetCustomerById(int id, bool showDeleted = false) { if (id == 0) { return(null); } // Here we expect to get two records, one for the first name and one for the last name. List <CustomerAttributeMappingDto> customerAttributeMappings = (from customer in _customerRepository.TableNoTracking join attribute in _genericAttributeRepository.TableNoTracking 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)) select new CustomerAttributeMappingDto() { Attribute = attribute, Customer = customer }).ToList(); CustomerDto customerDto = null; // This is in case we have first and last names set for the customer. if (customerAttributeMappings.Count > 0) { Customer customer = customerAttributeMappings.First().Customer; // The customer object is the same in all mappings. customerDto = customer.ToDto(); var defaultStoreLanguageId = GetDefaultStoreLangaugeId(); // If there is no Language Id generic attribute create one with the default language id. if (!customerAttributeMappings.Any(cam => cam != null && cam.Attribute != null && cam.Attribute.Key.Equals(LanguageId, StringComparison.InvariantCultureIgnoreCase))) { GenericAttribute languageId = new GenericAttribute { Key = LanguageId, Value = defaultStoreLanguageId.ToString() }; CustomerAttributeMappingDto customerAttributeMappingDto = new CustomerAttributeMappingDto { Customer = customer, Attribute = languageId }; customerAttributeMappings.Add(customerAttributeMappingDto); } foreach (var mapping in customerAttributeMappings) { if (!showDeleted && mapping.Customer.Deleted) { continue; } if (mapping.Attribute != null) { if (mapping.Attribute.Key.Equals(FirstName, StringComparison.InvariantCultureIgnoreCase)) { customerDto.FirstName = mapping.Attribute.Value; } else if (mapping.Attribute.Key.Equals(LastName, StringComparison.InvariantCultureIgnoreCase)) { customerDto.LastName = mapping.Attribute.Value; } else if (mapping.Attribute.Key.Equals(LanguageId, StringComparison.InvariantCultureIgnoreCase)) { customerDto.LanguageId = mapping.Attribute.Value; } } } } else { // This is when we do not have first and last name set. Customer currentCustomer = _customerRepository.TableNoTracking.FirstOrDefault(customer => customer.Id == id); if (currentCustomer != null) { if (showDeleted || !currentCustomer.Deleted) { customerDto = currentCustomer.ToDto(); } } } return(customerDto); }
public CustomerDto GetCustomerById(int id, bool showDeleted = false) { if (id == 0) { return(null); } // 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 == "Customer" select new CustomerAttributeMappingDto { Attribute = attribute, Customer = customer }).ToList(); CustomerDto customerDto = null; // This is in case we have first and last names set for the customer. if (customerAttributeMappings.Count > 0) { var customer = customerAttributeMappings.First().Customer; // The customer object is the same in all mappings. customerDto = customer.ToDto(); var defaultStoreLanguageId = GetDefaultStoreLangaugeId(); // If there is no Language Id generic attribute create one with the default language id. if (!customerAttributeMappings.Any(cam => cam?.Attribute != null && cam.Attribute.Key.Equals(LANGUAGE_ID, StringComparison.InvariantCultureIgnoreCase))) { var languageId = new GenericAttribute { Key = LANGUAGE_ID, Value = defaultStoreLanguageId.ToString() }; var customerAttributeMappingDto = new CustomerAttributeMappingDto { Customer = customer, Attribute = languageId }; customerAttributeMappings.Add(customerAttributeMappingDto); } foreach (var mapping in customerAttributeMappings) { if (!showDeleted && mapping.Customer.Deleted) { continue; } if (mapping.Attribute != null) { if (mapping.Attribute.Key.Equals(FIRST_NAME, StringComparison.InvariantCultureIgnoreCase)) { customerDto.FirstName = mapping.Attribute.Value; } else if (mapping.Attribute.Key.Equals(LAST_NAME, StringComparison.InvariantCultureIgnoreCase)) { customerDto.LastName = mapping.Attribute.Value; } else if (mapping.Attribute.Key.Equals(LANGUAGE_ID, StringComparison.InvariantCultureIgnoreCase)) { customerDto.LanguageId = mapping.Attribute.Value; } else if (mapping.Attribute.Key.Equals(DATE_OF_BIRTH, StringComparison.InvariantCultureIgnoreCase)) { customerDto.DateOfBirth = string.IsNullOrEmpty(mapping.Attribute.Value) ? (DateTime?)null : DateTime.Parse(mapping.Attribute.Value); } else if (mapping.Attribute.Key.Equals(GENDER, StringComparison.InvariantCultureIgnoreCase)) { customerDto.Gender = mapping.Attribute.Value; } } } } else { // This is when we do not have first and last name set. var currentCustomer = _customerRepository.Table.FirstOrDefault(customer => customer.Id == id); if (currentCustomer != null) { if (showDeleted || !currentCustomer.Deleted) { customerDto = currentCustomer.ToDto(); } } } SetNewsletterSubscribtionStatus(customerDto); return(customerDto); }