Skip to content

Instantly share code, notes, and snippets.

@rdalfonso
Created November 12, 2015 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdalfonso/7a7ca06a8be1b5766b6a to your computer and use it in GitHub Desktop.
Save rdalfonso/7a7ca06a8be1b5766b6a to your computer and use it in GitHub Desktop.
Code to geocode market
[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