Skip to content

Commit

Permalink
Custom Contact Models can load their properties into the properties d…
Browse files Browse the repository at this point in the history
…ictionary
  • Loading branch information
Psypher9 committed Jun 1, 2019
1 parent 5220106 commit 200342f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 47 deletions.
68 changes: 38 additions & 30 deletions HubSpot.NET.Tests/Api/Contact/Dto/ContactHSModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ public void WhenPropertiesAssigned_ThroughConstructor_The_Values_Should_Be_In_Pr
ZipCode = zip
};

Assert.Equal(email, sut.Properties["email"].Value);
Assert.Equal(fName, sut.Properties["firstname"].Value);
Assert.Equal(lName, sut.Properties["lastname"].Value);
Assert.Equal(site, sut.Properties["website"].Value);
Assert.Equal(comp, sut.Properties["company"].Value);
Assert.Equal(phone, sut.Properties["phone"].Value);
Assert.Equal(addr, sut.Properties["address"].Value);
Assert.Equal(city, sut.Properties["city"].Value);
Assert.Equal(state, sut.Properties["state"].Value);
Assert.Equal(zip, sut.Properties["zip"].Value);
sut.LoadProperties();

Assert.True(!sut.Properties.ContainsKey("email") || email == sut.Properties["email"].Value as string);
Assert.True(!sut.Properties.ContainsKey("firstname") || fName == sut.Properties["firstname"].Value as string);
Assert.True(!sut.Properties.ContainsKey("lastname") || lName == sut.Properties["lastname"].Value as string);
Assert.True(!sut.Properties.ContainsKey("website") || site == sut.Properties["website"].Value as string);
Assert.True(!sut.Properties.ContainsKey("company") || comp == sut.Properties["company"].Value as string);
Assert.True(!sut.Properties.ContainsKey("phone") || phone == sut.Properties["phone"].Value as string);
Assert.True(!sut.Properties.ContainsKey("address") || addr == sut.Properties["address"].Value as string);
Assert.True(!sut.Properties.ContainsKey("city") || city == sut.Properties["city"].Value as string);
Assert.True(!sut.Properties.ContainsKey("state") || state == sut.Properties["state"].Value as string);
Assert.True(!sut.Properties.ContainsKey("zip") || zip == sut.Properties["zip"].Value as string);
}

[Theory]
Expand Down Expand Up @@ -99,26 +101,32 @@ public void WhenPropertiesAssigned_ThroughConstructor_The_Values_In_PropertiesDi
ZipCode = zip
};

//Assert.Equal(sut.Properties["email"].Value, sut.Email);
//Assert.Equal(sut.Properties["firstname"].Value, sut.FirstName);
//Assert.Equal(sut.Properties["lastname"].Value, sut.LastName);
//Assert.Equal(sut.Properties["website"].Value, sut.Website);
//Assert.Equal(sut.Properties["company"].Value, sut.Company);
//Assert.Equal(sut.Properties["phone"].Value, sut.Phone);
//Assert.Equal(sut.Properties["address"].Value, sut.Address);
//Assert.Equal(sut.Properties["city"].Value, sut.City);
//Assert.Equal(sut.Properties["state"].Value, sut.State);
//Assert.Equal(sut.Properties["zip"].Value, sut.ZipCode);
Assert.True(sut.Properties["email"] == null || email == sut.Properties["email"].Value);
Assert.True(sut.Properties["firstname"] == null || fName == sut.Properties["firstname"].Value);
Assert.True(sut.Properties["lastname"] == null || lName == sut.Properties["lastname"].Value);
Assert.True(sut.Properties["website"] == null || site == sut.Properties["website"].Value);
Assert.True(sut.Properties["company"] == null || comp == sut.Properties["company"].Value);
Assert.True(sut.Properties["phone"] == null || phone == sut.Properties["phone"].Value);
Assert.True(sut.Properties["address"] == null || addr == sut.Properties["address"].Value);
Assert.True(sut.Properties["city"] == null || city == sut.Properties["city"].Value);
Assert.True(sut.Properties["state"] == null || state == sut.Properties["state"].Value);
Assert.True(sut.Properties["zip"] == null || zip == sut.Properties["zip"].Value);
sut.LoadProperties();

Assert.True(!sut.Properties.ContainsKey("email") || email == sut.Properties["email"].Value as string);
Assert.True(!sut.Properties.ContainsKey("firstname") || fName == sut.Properties["firstname"].Value as string);
Assert.True(!sut.Properties.ContainsKey("lastname") || lName == sut.Properties["lastname"].Value as string);
Assert.True(!sut.Properties.ContainsKey("website") || site == sut.Properties["website"].Value as string);
Assert.True(!sut.Properties.ContainsKey("company") || comp == sut.Properties["company"].Value as string);
Assert.True(!sut.Properties.ContainsKey("phone") || phone == sut.Properties["phone"].Value as string);
Assert.True(!sut.Properties.ContainsKey("address") || addr == sut.Properties["address"].Value as string);
Assert.True(!sut.Properties.ContainsKey("city") || city == sut.Properties["city"].Value as string);
Assert.True(!sut.Properties.ContainsKey("state") || state == sut.Properties["state"].Value as string);
Assert.True(!sut.Properties.ContainsKey("zip") || zip == sut.Properties["zip"].Value as string);
}

[Fact]
public void CustomContactModels_WillLoadTheirProperties_IntoThePropertiesDictionary_OnLoadProperties()
{
sut = new CustomContact("cheese", 5);

sut.LoadProperties();

Assert.True(sut.Properties.ContainsKey("flavor"));
Assert.True(sut.Properties.ContainsKey("legs"));

Assert.Equal("cheese", sut.Properties["flavor"].Value);
Assert.Equal(5, sut.Properties["legs"].Value);
}
}
}
23 changes: 23 additions & 0 deletions HubSpot.NET/Api/Contact/Dto/ContactHubSpotModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using HubSpot.NET.Core.Interfaces;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;

/// <summary>
Expand All @@ -13,6 +14,8 @@
[DataContract]
public class ContactHubSpotModel : IHubSpotModel
{
public ContactHubSpotModel() { }

/// <summary>
/// Contacts unique ID in HubSpot
/// </summary>
Expand Down Expand Up @@ -50,9 +53,29 @@ public class ContactHubSpotModel : IHubSpotModel
[DataMember(Name = "hubspot_owner_id")]
public long? OwnerId { get; set; }

// Used for return values
[DataMember(Name = "properties")]
public Dictionary<string, ContactProperty> Properties { get; set; } = new Dictionary<string, ContactProperty>();

/// <summary>
/// Loads all properties into the ContactProperty Dictionary even from custom contact models
/// </summary>
public void LoadProperties()
{
PropertyInfo[] properties = GetType().GetProperties();

foreach (PropertyInfo prop in properties)
{
var key = prop.GetCustomAttribute(typeof(DataMemberAttribute)) as DataMemberAttribute;
object value = prop.GetValue(this);

if (value == null || key == null || key.Name == "properties")
continue;

Properties.Add(key.Name, new ContactProperty(value));
}
}

[IgnoreDataMember]
public bool IsNameValue => false;
}
Expand Down
4 changes: 2 additions & 2 deletions HubSpot.NET/Api/Contact/Dto/ContactProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class ContactProperty
{
public ContactProperty() { }

public ContactProperty(string value)
public ContactProperty(object value)
{
Value = value;
}

[DataMember(Name = "value")]
public string Value { get; set; }
public object Value { get; set; }

[DataMember(Name = "versions")]
List<ContactPropertyVersion> Versions { get; set; } = new List<ContactPropertyVersion>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Runtime.Serialization;

[DataContract]
public class CreateOrUpdateContactTransportModel : PropertyTransportManager<ContactHubSpotModel>
public class CreateOrUpdateContactTransportModel : PropertyTransport<ContactHubSpotModel>
{
public CreateOrUpdateContactTransportModel() { }

Expand Down
6 changes: 1 addition & 5 deletions HubSpot.NET/Api/Contact/HubSpotContactApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,14 @@ public ContactHubSpotModel GetById(long contactId, bool IncludeHistory = true)
if(IncludeHistory)
{
return _client.Execute<ContactHubSpotModel>(GetRoute<ContactHubSpotModel>("contact","vid", contactId.ToString(),"profile"));

}
else
{
return _client.Execute<ContactHubSpotModel>(GetRoute<ContactHubSpotModel>("contact", "vid", contactId.ToString(), "profile?propertyMode=value_only"));
}
}

public ContactHubSpotModel GetById(long Id)
{
return GetById(Id, true);
}
public ContactHubSpotModel GetById(long Id) => GetById(Id, true);

/// <summary>
/// Gets a contact by their email address
Expand Down
10 changes: 1 addition & 9 deletions HubSpot.NET/Api/Shared/PropertyTransportModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Runtime.Serialization;

[DataContract]
public abstract class PropertyTransportManager<T>
public abstract class PropertyTransport<T>
{
[DataMember(Name = "properties")]
public PropertyValuePairCollection Properties { get; set; } = new PropertyValuePairCollection();
Expand Down Expand Up @@ -46,14 +46,6 @@ public void ToPropertyTransportModel(T model)
}
}

//public void ToPropertyTransportModel(ContactHubSpotModel model)
//{
// foreach (KeyValuePair<string, ContactProperty> pair in model.Properties)
// {
// Properties.Add(new PropertyValuePair() { Property = pair.Key, Value = pair.Value.Value });
// }
//}

public void FromPropertyTransportModel(out T model)
{
model = (T) Assembly.GetAssembly(typeof(T)).CreateInstance(typeof(T).FullName);
Expand Down

0 comments on commit 200342f

Please sign in to comment.