Created
November 12, 2015 16:42
-
-
Save rdalfonso/7a7ca06a8be1b5766b6a to your computer and use it in GitHub Desktop.
Code to geocode market
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DoNotValidate] | |
public virtual SalesProfile PrimarySalesProfile { get; set; } | |
public virtual SalesProfile GetPrimarySalesProfile(IGeoApiUrlBuilder geoApiUrlBuilder, IHttpClientService httpClientService) | |
{ | |
GeolocateMarket(geoApiUrlBuilder, httpClientService); | |
if (PrimarySalesProfile.IsNotNull()) | |
{ | |
return PrimarySalesProfile; | |
} | |
PrimarySalesProfile = SalesProfiles.FirstOrDefault(p => p.PurchaseStatusCode == PurchaseStatusCode.Paid); | |
if (PrimarySalesProfile.IsNotNull()) | |
{ | |
return PrimarySalesProfile; | |
} | |
PrimarySalesProfile = SalesProfiles.FirstOrDefault(p => p.PurchaseStatusCode == PurchaseStatusCode.Lite); | |
if (PrimarySalesProfile.IsNotNull()) | |
{ | |
return PrimarySalesProfile; | |
} | |
PrimarySalesProfile = SalesProfiles.FirstOrDefault(p => p.PurchaseStatusCode == PurchaseStatusCode.Freemium); | |
if (PrimarySalesProfile.IsNotNull()) | |
{ | |
return PrimarySalesProfile; | |
} | |
return SalesProfiles.FirstOrDefault(p => p.PurchaseStatusCode == PurchaseStatusCode.Freemium); | |
} | |
public virtual Continuation GeolocateMarket(IGeoApiUrlBuilder geoApiUrlBuilder, IHttpClientService httpClientService) | |
{ | |
return ProfileAddress.Street1.IsNullOrWhiteSpace() ? | |
GeolocateMarketByCityStateZip(geoApiUrlBuilder, httpClientService) | |
: GeolocateMarketByAddress(geoApiUrlBuilder, httpClientService); | |
} | |
private Continuation GeolocateMarketByCityStateZip(IGeoApiUrlBuilder geoApiUrlBuilder, IHttpClientService httpClientService) | |
{ | |
var ret = new Continuation { Success = true }; | |
AsyncContext.Run(async () => | |
{ | |
var url = geoApiUrlBuilder.BuildMarketByCityStateZipUrl(ProfileAddress.City, ProfileAddress.State.Value, ProfileAddress.PostalCode); | |
var result = await httpClientService.GetAsync<GeoApiMarketResponseDto>(url); | |
if (result.IsNull()) | |
{ | |
ret.Success = false; | |
ret.Errors = new List<ErrorInfo> { new ErrorInfo("Geolocation Market", "Geolocation Market failed.") }; | |
return; | |
} | |
ret = HandleGeolocationMarketResponse(result); | |
}); | |
return ret; | |
} | |
private Continuation GeolocateMarketByAddress(IGeoApiUrlBuilder geoApiUrlBuilder, IHttpClientService httpClientService) | |
{ | |
var ret = new Continuation { Success = true }; | |
AsyncContext.Run(async () => | |
{ | |
var url = geoApiUrlBuilder.BuildMarketByFullAddressUrl(ProfileAddress.Street1, ProfileAddress.City, ProfileAddress.State.Value, ProfileAddress.PostalCode); | |
var result = await httpClientService.GetAsync<GeoApiMarketResponseDto>(url); | |
if (result.IsNull()) | |
{ | |
ret.Success = false; | |
ret.Errors = new List<ErrorInfo> { new ErrorInfo("Geolocation Market", "Geolocation Market failed.") }; | |
return; | |
} | |
ret = HandleGeolocationMarketResponse(result); | |
}); | |
return ret; | |
} | |
private Continuation HandleGeolocationMarketResponse(GeoApiMarketResponseDto response) | |
{ | |
response = response ?? new GeoApiMarketResponseDto(); | |
var isSuccessfulGeocode = (response.Market.Length > 0); | |
if (!isSuccessfulGeocode) | |
{ | |
return new Continuation | |
{ | |
Success = false, | |
Errors = new List<ErrorInfo> { new ErrorInfo("Geolocation Market", "Geolocation Market failed.") } | |
}; | |
} | |
if (response.IsNotNull()) | |
{ | |
PrimarySalesProfile = SalesProfiles.FirstOrDefault(p => p.Market.Code == response.Market[0].ToString()); | |
} | |
return new Continuation { Success = true }; | |
} | |
public class GeoApiMarketResponseDto | |
{ | |
public string Market { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment