public async Task <TContact> SaveAsync <TContact>(TContact contact)
            where TContact : Contact, new()
        {
            if (contact == null)
            {
                throw new ArgumentNullException(nameof(contact));
            }

            var customProperties = (from property in _typeManager.GetPropertyData(contact)
                                    select new ValuedProperty(property.PropertyName, property.Value?.ToString()))
                                   .ToArray();

            if (customProperties.All(cp => string.IsNullOrEmpty(cp.Value)) && IsNewContact())
            {
                return(contact);
            }

            if (IsNewContact())
            {
                var newContact = await _client.Contacts.CreateAsync(customProperties);

                return(_typeManager.ConvertTo <TContact>(newContact));
            }

            await _client.Contacts.UpdateByIdAsync(contact.Id, customProperties);

            var modifiedContact = await GetAsync <TContact>(SelectContact.ById(contact.Id));

            return(modifiedContact);

            bool IsNewContact()
            {
                return(contact.Id == 0 && contact.Created == default);
            }
        }
        public async Task <TContact> SaveAsync <TContact>(TContact contact)
            where TContact : Contact, new()
        {
            if (contact == null)
            {
                throw new ArgumentNullException(nameof(contact));
            }

            var modifiedProperties = (from property in _typeManager.GetModifiedProperties(contact)
                                      select new ValuedProperty(property.name, property.value)).ToArray();

            if (modifiedProperties.Any())
            {
                if (IsNewContact())
                {
                    var newContact = await _client.Contacts.CreateAsync(modifiedProperties);

                    return(_typeManager.ConvertTo <TContact>(newContact));
                }
                else
                {
                    await _client.Contacts.UpdateByIdAsync(contact.Id, modifiedProperties);

                    var newContact = await GetAsync <TContact>(SelectContact.ById(contact.Id));

                    return(newContact);
                }
            }

            return(contact);

            bool IsNewContact()
            {
                return(contact.Id == 0 && contact.Created == default);
            }
        }
Пример #3
0
 public static Task <TContact> GetByEmailAsync <TContact>(this IHubSpotContactConnector connector, string email) where TContact : Contact, new()
 => connector.GetAsync <TContact>(SelectContact.ByEmail(email));
Пример #4
0
 public static Task <TContact> GetByUserTokenAsync <TContact>(this IHubSpotContactConnector connector, string userToken) where TContact : Contact, new()
 => connector.GetAsync <TContact>(SelectContact.ByUserToken(userToken));
Пример #5
0
 public static Task <TContact> GetByIdAsync <TContact>(this IHubSpotContactConnector connector, long contactId) where TContact : Contact, new()
 => connector.GetAsync <TContact>(SelectContact.ById(contactId));