diff --git a/Svrnty.GeoManagement.Abstractions/Abstractions/IAddress.cs b/Svrnty.GeoManagement.Abstractions/Abstractions/IAddress.cs index b4f8718..c65e3dd 100644 --- a/Svrnty.GeoManagement.Abstractions/Abstractions/IAddress.cs +++ b/Svrnty.GeoManagement.Abstractions/Abstractions/IAddress.cs @@ -2,10 +2,10 @@ namespace Svrnty.GeoManagement.Abstractions.Abstractions; public interface IAddress { - public string Line1 { get; } - public string? Line2 { get; } - public string City { get; } - public string Subdivision { get; } - public string PostalCode { get; } - public string Country { get; } + public string Line1 { get; set; } + public string? Line2 { get; set; } + public string City { get; set; } + public string Subdivision { get; set; } + public string PostalCode { get; set; } + public string Country { get; set; } } \ No newline at end of file diff --git a/Svrnty.GeoManagement.Abstractions/Models/Address.cs b/Svrnty.GeoManagement.Abstractions/Models/Address.cs index b802c5d..c788b5a 100644 --- a/Svrnty.GeoManagement.Abstractions/Models/Address.cs +++ b/Svrnty.GeoManagement.Abstractions/Models/Address.cs @@ -2,60 +2,17 @@ using Svrnty.GeoManagement.Abstractions.Abstractions; namespace Svrnty.GeoManagement.Abstractions.Models; -public record GeoPoint(double Latitude, double Longitude); +public record GeoPoint(decimal Latitude, decimal Longitude); -public record Address( - string Line1, - string? Line2, - string City, - string Subdivision, - string PostalCode, - string Country, - GeoPoint? Location, - string? Note, - bool IsNormalized = false -) : IAddress +public record Address(bool Normalized = false) : IAddress { - public string GetFormattedAddress( - FormattedAddressType formatType = FormattedAddressType.Full - ) - { - return formatType switch - { - FormattedAddressType.Full => FormatFullOneLine(), - FormattedAddressType.Compact => FormatCompactOneLine(), - FormattedAddressType.MultiLine => FormatMultiLine(), - _ => FormatFullOneLine() - }; - } - - private string FormatFullOneLine() - { - if (string.IsNullOrWhiteSpace(Line2)) - { - return $"{Line1}, {City}, {Subdivision} {PostalCode}, {Country}"; - } - - return $"{Line2}, {Line1}, {City}, {Subdivision} {PostalCode}, {Country}"; - } - - private string FormatCompactOneLine() - { - if (string.IsNullOrWhiteSpace(Line2)) - { - return $"{Line1}, {City}, {Subdivision}"; - } - - return $"{Line2}, {Line1}, {City}, {Subdivision}"; - } - - private string FormatMultiLine() - { - if (string.IsNullOrWhiteSpace(Line2)) - { - return $"{Line1}\n{City}, {Subdivision} {PostalCode}\n{Country}"; - } - - return $"{Line2}, {Line1}\n{City}, {Subdivision} {PostalCode}\n{Country}"; - } + public required string Line1 { get; set; } + public string? Line2 { get; set; } + public required string City { get; set; } + public required string Subdivision { get; set; } + public required string PostalCode { get; set; } + public required string Country { get; set; } + public GeoPoint? Location { get; set; } + public string? Note { get; set; } + public bool IsNormalized() => Normalized; } \ No newline at end of file diff --git a/Svrnty.GeoManagement.Abstractions/ServiceCollectionExtensions.cs b/Svrnty.GeoManagement.Abstractions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..18066b8 --- /dev/null +++ b/Svrnty.GeoManagement.Abstractions/ServiceCollectionExtensions.cs @@ -0,0 +1,68 @@ +using Svrnty.GeoManagement.Abstractions.Abstractions; +using Svrnty.GeoManagement.Abstractions.Models; + +namespace Svrnty.GeoManagement.Abstractions; + +public static class ServiceCollectionExtensions +{ + public static void To(this IAddress address, IAddress toAddress) + { + toAddress.Line1 = address.Line1; + toAddress.Line2 = address.Line2; + toAddress.City = address.City; + toAddress.PostalCode = address.PostalCode; + toAddress.Country = address.Country; + toAddress.Subdivision = address.Subdivision; + } + + public static void From(this IAddress address, IAddress fromAddress) + { + address.Line1 = fromAddress.Line1; + address.Line2 = fromAddress.Line2; + address.City = fromAddress.City; + address.PostalCode = fromAddress.PostalCode; + address.Country = fromAddress.Country; + address.Subdivision = fromAddress.Subdivision; + } + + public static string GetFormattedAddress(this IAddress address, FormattedAddressType formatType = FormattedAddressType.Full) + { + return formatType switch + { + FormattedAddressType.Full => FormatFullOneLine(address), + FormattedAddressType.Compact => FormatCompactOneLine(address), + FormattedAddressType.MultiLine => FormatMultiLine(address), + _ => FormatFullOneLine(address) + }; + } + + private static string FormatFullOneLine(IAddress address) + { + if (string.IsNullOrWhiteSpace(address.Line2)) + { + return $"{address.Line1}, {address.City}, {address.Subdivision} {address.PostalCode}, {address.Country}"; + } + + return $"{address.Line2}, {address.Line1}, {address.City}, {address.Subdivision} {address.PostalCode}, {address.Country}"; + } + + private static string FormatCompactOneLine(IAddress address) + { + if (string.IsNullOrWhiteSpace(address.Line2)) + { + return $"{address.Line1}, {address.City}, {address.Subdivision}"; + } + + return $"{address.Line2}, {address.Line1}, {address.City}, {address.Subdivision}"; + } + + private static string FormatMultiLine(IAddress address) + { + if (string.IsNullOrWhiteSpace(address.Line2)) + { + return $"{address.Line1}\n{address.City}, {address.Subdivision} {address.PostalCode}\n{address.Country}"; + } + + return $"{address.Line2}, {address.Line1}\n{address.City}, {address.Subdivision} {address.PostalCode}\n{address.Country}"; + } +} \ No newline at end of file diff --git a/Svrnty.GeoManagement.Google/GeoManagementGoogleProvider.cs b/Svrnty.GeoManagement.Google/GeoManagementGoogleProvider.cs index 1e5faf4..31d51b4 100644 --- a/Svrnty.GeoManagement.Google/GeoManagementGoogleProvider.cs +++ b/Svrnty.GeoManagement.Google/GeoManagementGoogleProvider.cs @@ -3,6 +3,7 @@ using GoogleApi.Entities.Maps.Geocoding.Address.Request; using GoogleApi.Entities.Maps.Geocoding.Location.Request; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Svrnty.GeoManagement.Abstractions; using Svrnty.GeoManagement.Abstractions.Abstractions; using Svrnty.GeoManagement.Abstractions.Models; using Svrnty.GeoManagement.Google.Configuration; @@ -91,7 +92,7 @@ public class GeoManagementGoogleProvider : IGeoManagementProvider var request = new LocationGeocodeRequest { Key = _options.ApiKey, - Location = new GoogleApi.Entities.Common.Coordinate(geoPoint.Latitude, geoPoint.Longitude) + Location = new GoogleApi.Entities.Common.Coordinate((double)geoPoint.Latitude, (double)geoPoint.Longitude) }; if (!string.IsNullOrWhiteSpace(_options.Language) && diff --git a/Svrnty.GeoManagement.Google/Mapping/GoogleAddressMapper.cs b/Svrnty.GeoManagement.Google/Mapping/GoogleAddressMapper.cs index 4aed470..e1aa902 100644 --- a/Svrnty.GeoManagement.Google/Mapping/GoogleAddressMapper.cs +++ b/Svrnty.GeoManagement.Google/Mapping/GoogleAddressMapper.cs @@ -26,20 +26,20 @@ internal static class GoogleAddressMapper var country = GetComponentValue(components, "country") ?? string.Empty; var location = googleResult.Geometry?.Location != null - ? new GeoPoint(googleResult.Geometry.Location.Latitude, googleResult.Geometry.Location.Longitude) + ? new GeoPoint((decimal)googleResult.Geometry.Location.Latitude, (decimal)googleResult.Geometry.Location.Longitude) : null; - return new Abstractions.Models.Address( - Line1: line1, - Line2: line2, - City: city, - Subdivision: subdivision, - PostalCode: postalCode, - Country: country, - Location: location, - Note: null, - IsNormalized: true - ); + return new Abstractions.Models.Address(Normalized: true) + { + Line1 = line1, + Line2 = line2, + City = city, + Subdivision = subdivision, + PostalCode = postalCode, + Country = country, + Location = location, + Note = null + }; } private static string BuildLine1(IEnumerable components) @@ -79,8 +79,8 @@ internal static class GoogleAddressMapper return null; return new GeoPoint( - googleResult.Geometry.Location.Latitude, - googleResult.Geometry.Location.Longitude + (decimal)googleResult.Geometry.Location.Latitude, + (decimal)googleResult.Geometry.Location.Longitude ); } } \ No newline at end of file diff --git a/Svrnty.GeoManagement.Tests/Configuration/TestDataConfiguration.cs b/Svrnty.GeoManagement.Tests/Configuration/TestDataConfiguration.cs index 638c598..74418c0 100644 --- a/Svrnty.GeoManagement.Tests/Configuration/TestDataConfiguration.cs +++ b/Svrnty.GeoManagement.Tests/Configuration/TestDataConfiguration.cs @@ -24,12 +24,12 @@ public class AddressTestData public class GeoPointTestData { - public double Latitude { get; set; } - public double Longitude { get; set; } + public decimal Latitude { get; set; } + public decimal Longitude { get; set; } } public class CoordinateRange { - public double Min { get; set; } - public double Max { get; set; } + public decimal Min { get; set; } + public decimal Max { get; set; } } diff --git a/Svrnty.GeoManagement.sln.DotSettings.user b/Svrnty.GeoManagement.sln.DotSettings.user new file mode 100644 index 0000000..caa6b4a --- /dev/null +++ b/Svrnty.GeoManagement.sln.DotSettings.user @@ -0,0 +1,2 @@ + + ForceIncluded \ No newline at end of file