Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/Contacts/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Talegen.Common.Models.Contacts
{
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

Expand Down Expand Up @@ -67,6 +68,11 @@ public enum CommerceAddressTypes
/// </summary>
public class Address
{
/// <summary>
/// Contains the formatted address text.
/// </summary>
private string formattedText;

/// <summary>
/// Gets or sets the primary address street.
/// </summary>
Expand Down Expand Up @@ -120,5 +126,74 @@ public class Address
/// </summary>
/// <value><c>true</c> if primary; otherwise, <c>false</c>.</value>
public bool Primary { get; set; }

/// <summary>
/// Converts to stringformatted.
/// </summary>
/// <returns></returns>
public string ToStringFormatted()
{
if (string.IsNullOrWhiteSpace(this.formattedText))
{
// build formatted text out
if (!string.IsNullOrWhiteSpace(this.Street1))
{
this.formattedText = this.Street1 + Environment.NewLine;
}

if (!string.IsNullOrWhiteSpace(this.Street2))
{
this.formattedText += this.Street2 + Environment.NewLine;
}

string localityRegionPostal = string.Empty;

if (!string.IsNullOrWhiteSpace(this.City))
{
localityRegionPostal = this.City;
}

if (!string.IsNullOrWhiteSpace(this.RegionState))
{
localityRegionPostal += ", " + this.RegionState;
}

if (!string.IsNullOrWhiteSpace(this.PostalCode))
{
localityRegionPostal += " " + this.PostalCode;
}

this.formattedText += localityRegionPostal + Environment.NewLine;

if (!string.IsNullOrWhiteSpace(this.Country))
{
this.formattedText += this.Country + Environment.NewLine;
}
}

return this.formattedText;
}

/// <summary>
/// Gets or sets the formatted.
/// </summary>
/// <value>
/// Gets or sets the Full mailing address, formatted for display or use on a mailing label.This field MAY contain multiple lines, separated by
/// newlines.Newlines can be represented either as a carriage return/line feed pair ("\r\n") or as a single line feed character("\n").
/// </value>
public string Formatted
{
get
{
this.formattedText = this.ToStringFormatted();
return this.formattedText;
}

set
{
// set the formatted text.
this.formattedText = value;
}
}
}
}
75 changes: 75 additions & 0 deletions src/Extensions/ContactExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
*
* (c) Copyright Talegen, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

namespace Talegen.Common.Models.Extensions
{
using System;
using System.Security.Claims;
using Newtonsoft.Json;
using Talegen.Common.Models.Contacts;
using Talegen.Common.Models.Extensions.Internal;

/// <summary>
/// This extensions class contains extension methods to enhance the use of the Contact models.
/// </summary>
public static class ContactExtensions
{
/// <summary>
/// The JWT address claim type.
/// </summary>
public const string JwtAddressClaimType = "address";

/// <summary>
/// This extension method converts a JWT address claim to a <see cref="Address" /> model object.
/// </summary>
/// <param name="claim">The claim containing the address information.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">Exception is thrown if <paramref name="claim" /> is null.</exception>
public static Address ToAddress(this Claim claim)
{
Address result = null;

if (claim == null)
{
throw new ArgumentNullException(nameof(claim));
}

// is a JWT address claim
if (claim.Type == JwtAddressClaimType && !string.IsNullOrWhiteSpace(claim.Value))
{
JwtAddressModel jwtModel = JsonConvert.DeserializeObject<JwtAddressModel>(claim.Value);

if (jwtModel != null)
{
string[] streetLines = jwtModel.StreetAddress.Split('\n');

result = new Address
{
Street1 = streetLines != null ? streetLines[0].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty,
Street2 = streetLines != null && streetLines.Length > 1 ? streetLines[1].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty,
City = jwtModel.Locality,
Country = jwtModel.Country,
PostalCode = jwtModel.PostalCode,
RegionState = jwtModel.Region,
Formatted = jwtModel.Formatted
};
}
}

return result;
}
}
}
124 changes: 124 additions & 0 deletions src/Extensions/Internal/JwtAddressModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
*
* (c) Copyright Talegen, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

namespace Talegen.Common.Models.Extensions.Internal
{
using System;
using Newtonsoft.Json;
using Talegen.Common.Models.Contacts;

/// <summary>
/// This class represents a JWT address model for use in translation to a <see cref="Address" /> model.
/// </summary>
internal class JwtAddressModel
{
private string formattedText;

/// <summary>
/// Gets or sets the formatted.
/// </summary>
/// <value>
/// Gets or sets the Full mailing address, formatted for display or use on a mailing label.This field MAY contain multiple lines, separated by
/// newlines.Newlines can be represented either as a carriage return/line feed pair ("\r\n") or as a single line feed character("\n").
/// </value>
[JsonProperty("formatted")]
public string Formatted
{
get
{
if (string.IsNullOrWhiteSpace(this.formattedText))
{
// build formatted text out
if (!string.IsNullOrWhiteSpace(this.StreetAddress))
{
this.formattedText = this.StreetAddress + Environment.NewLine;
}

string localityRegionPostal = string.Empty;

if (!string.IsNullOrWhiteSpace(this.Locality))
{
localityRegionPostal = this.Locality;
}

if (!string.IsNullOrWhiteSpace(this.Region))
{
localityRegionPostal += ", " + this.Region;
}

if (!string.IsNullOrWhiteSpace(this.PostalCode))
{
localityRegionPostal += " " + this.PostalCode;
}

this.formattedText += localityRegionPostal + Environment.NewLine;

if (!string.IsNullOrWhiteSpace(this.Country))
{
this.formattedText += this.Country + Environment.NewLine;
}
}

return this.formattedText;
}

set
{
// set the formatted text.
this.formattedText = value;
}
}

/// <summary>
/// Gets or sets the street address.
/// </summary>
/// <value>
/// Full street address component, which MAY include house number, street name, Post Office Box, and multi-line extended street address information.This
/// field MAY contain multiple lines, separated by newlines.Newlines can be represented either as a carriage return/line feed pair ("\r\n") or as a
/// single line feed character("\n").
/// </value>
[JsonProperty("street_address")]
public string StreetAddress { get; set; }

/// <summary>
/// Gets or sets the locality.
/// </summary>
/// <value>The City or locality component.</value>
[JsonProperty("locality")]
public string Locality { get; set; }

/// <summary>
/// Gets or sets the region.
/// </summary>
/// <value>The State, province, prefecture, or region component.</value>
[JsonProperty("region")]
public string Region { get; set; }

/// <summary>
/// Gets or sets the postal code.
/// </summary>
/// <value>The Zip code or postal code component.</value>
[JsonProperty("postal_code")]
public string PostalCode { get; set; }

/// <summary>
/// Gets or sets the country.
/// </summary>
/// <value>The country name component.</value>
[JsonProperty("country")]
public string Country { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Talegen.Common.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<RepositoryUrl>https://github.com/Talegen/Talegen.Common.Models</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>Models</PackageTags>
<PackageReleaseNotes>Fixed Paginated Query to use Common.Core SortDirection enumeration.</PackageReleaseNotes>
<PackageReleaseNotes>Added additional modelling and extensions for address models.</PackageReleaseNotes>
<ApplicationIcon>Assets\logo.ico</ApplicationIcon>
<SignAssembly>false</SignAssembly>
<Version>1.0.2.0</Version>
<Version>1.0.3.0</Version>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

Expand Down
Loading