From 8082fc942a1ae2b7dc26eaae5b8a71139cff7d4b Mon Sep 17 00:00:00 2001 From: Robert Foster Date: Wed, 6 Dec 2023 04:58:00 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20=F0=9F=8E=A8=20=F0=9F=90=9B=20?= =?UTF-8?q?=F0=9F=94=A5=20Location=20Parsing=20function=20is=20now=20publi?= =?UTF-8?q?c,=20cleaned=20up=20a=20few=20issues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Our.Umbraco.GMaps.Core/Composing/Composer.cs | 22 +------ .../Composing/RegisterServerVariables.v8.cs | 53 ----------------- .../ServerVariablesParsingHandler.v9.cs | 58 +++++++------------ .../GoogleMapsBuilderExtensions.cs | 4 +- Our.Umbraco.GMaps.Core/Models/Location.cs | 6 +- Our.Umbraco.GMaps.Core/Models/MapConfig.cs | 1 + .../Our.Umbraco.GMaps.Core.csproj | 2 +- .../SingleMapPropertyValueConverter.cs | 8 +-- .../Controllers/MapTestController.cs | 6 +- .../Controllers/MapTestController.cs | 26 ++++----- Our.Umbraco.GMaps.UmbracoV13/appsettings.json | 2 +- Our.Umbraco.GMaps/Our.Umbraco.GMaps.csproj | 2 +- 12 files changed, 49 insertions(+), 141 deletions(-) delete mode 100644 Our.Umbraco.GMaps.Core/Composing/RegisterServerVariables.v8.cs diff --git a/Our.Umbraco.GMaps.Core/Composing/Composer.cs b/Our.Umbraco.GMaps.Core/Composing/Composer.cs index f749a78..5200a01 100644 --- a/Our.Umbraco.GMaps.Core/Composing/Composer.cs +++ b/Our.Umbraco.GMaps.Core/Composing/Composer.cs @@ -1,35 +1,15 @@ -#if NET5_0_OR_GREATER -using Umbraco.Cms.Core.Notifications; +using Umbraco.Cms.Core.Notifications; using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.DependencyInjection; -#else -using Our.Umbraco.GMaps.Core.Config; -using Umbraco.Core; -using Umbraco.Core.Composing; -#endif - namespace Our.Umbraco.GMaps.Core.Composing { -#if NET5_0_OR_GREATER public class Composer : IComposer -#else - [RuntimeLevel(MinLevel = RuntimeLevel.Boot)] - public class Composer : IUserComposer -#endif { -#if NET5_0_OR_GREATER public void Compose(IUmbracoBuilder builder) { builder.AddGoogleMaps(); builder.AddNotificationHandler(); } -#else - public void Compose(Composition composition) - { - composition.Register(); - composition.Components().Append(); - } -#endif } } \ No newline at end of file diff --git a/Our.Umbraco.GMaps.Core/Composing/RegisterServerVariables.v8.cs b/Our.Umbraco.GMaps.Core/Composing/RegisterServerVariables.v8.cs deleted file mode 100644 index 61ebd4b..0000000 --- a/Our.Umbraco.GMaps.Core/Composing/RegisterServerVariables.v8.cs +++ /dev/null @@ -1,53 +0,0 @@ -#if !NET5_0_OR_GREATER -using Our.Umbraco.GMaps.Core.Controllers; -using System; -using System.Collections.Generic; -using System.Web; -using System.Web.Mvc; -using System.Web.Routing; -using Umbraco.Core.Composing; -using Umbraco.Web; -using Umbraco.Web.JavaScript; - -namespace Our.Umbraco.GMaps.Core.Composing -{ - internal class RegisterServerVariables : IComponent - { - public void Initialize() - { - ServerVariablesParser.Parsing += new EventHandler>(ServerVariablesParser_Parsing); - - } - - private void ServerVariablesParser_Parsing(object sender, Dictionary e) - { - if (HttpContext.Current == null) - throw new InvalidOperationException("HttpContext is null"); - - UrlHelper urlHelper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData())); - - string gMapsBaseUrl = urlHelper.GetUmbracoApiServiceBaseUrl(controller => controller.GetSettings()); - - Dictionary umbracoUrls; - if (e.TryGetValue("umbracoUrls", out object found)) - { - umbracoUrls = found as Dictionary; - } - else - { - umbracoUrls = new Dictionary(); - e["umbracoUrls"] = umbracoUrls; - } - - if (!umbracoUrls.ContainsKey(nameof(gMapsBaseUrl))) - { - umbracoUrls[nameof(gMapsBaseUrl)] = gMapsBaseUrl; - } - } - - public void Terminate() - { - } - } -} -#endif diff --git a/Our.Umbraco.GMaps.Core/Composing/ServerVariablesParsingHandler.v9.cs b/Our.Umbraco.GMaps.Core/Composing/ServerVariablesParsingHandler.v9.cs index 2f8a6b1..451c388 100644 --- a/Our.Umbraco.GMaps.Core/Composing/ServerVariablesParsingHandler.v9.cs +++ b/Our.Umbraco.GMaps.Core/Composing/ServerVariablesParsingHandler.v9.cs @@ -1,5 +1,4 @@ -#if NET5_0_OR_GREATER -using Our.Umbraco.GMaps.Core.Controllers; +using Our.Umbraco.GMaps.Core.Controllers; using Microsoft.AspNetCore.Routing; using System; using System.Collections.Generic; @@ -14,49 +13,32 @@ internal class ServerVariablesParsingHandler : { private readonly LinkGenerator linkGenerator; - public ServerVariablesParsingHandler(LinkGenerator linkGenerator) - { - this.linkGenerator = linkGenerator; - } - - public void Handle(ServerVariablesParsingNotification notification) - { - IDictionary serverVars = notification.ServerVariables; - - if (!serverVars.ContainsKey("umbracoUrls")) - { - throw new ArgumentException("Missing umbracoUrls."); - } - - var umbracoUrlsObject = serverVars["umbracoUrls"]; - if (umbracoUrlsObject == null) + public ServerVariablesParsingHandler(LinkGenerator linkGenerator) { - throw new ArgumentException("Null umbracoUrls"); + this.linkGenerator = linkGenerator; } - if (!(umbracoUrlsObject is Dictionary umbracoUrls)) + public void Handle(ServerVariablesParsingNotification notification) { - throw new ArgumentException("Invalid umbracoUrls"); - } + IDictionary serverVars = notification.ServerVariables; - if (!serverVars.ContainsKey("umbracoPlugins")) - { - throw new ArgumentException("Missing umbracoPlugins."); - } + if (!serverVars.TryGetValue("umbracoUrls", out object umbracoUrlsObject)) + { + throw new ArgumentException("Missing umbracoUrls."); + } - if (!(serverVars["umbracoPlugins"] is Dictionary umbracoPlugins)) - { - throw new ArgumentException("Invalid umbracoPlugins"); - } + if (umbracoUrlsObject is not Dictionary umbracoUrls) + { + throw new ArgumentException("Invalid umbracoUrls"); + } - var gMapsBaseUrl = linkGenerator.GetUmbracoApiServiceBaseUrl(controller => - controller.GetSettings()); + var gMapsBaseUrl = linkGenerator.GetUmbracoApiServiceBaseUrl(controller => + controller.GetSettings()); - if (!umbracoUrls.ContainsKey(nameof(gMapsBaseUrl))) - { - umbracoUrls[nameof(gMapsBaseUrl)] = gMapsBaseUrl; + if (!umbracoUrls.ContainsKey(nameof(gMapsBaseUrl))) + { + umbracoUrls[nameof(gMapsBaseUrl)] = gMapsBaseUrl; + } } } -} -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/Our.Umbraco.GMaps.Core/GoogleMapsBuilderExtensions.cs b/Our.Umbraco.GMaps.Core/GoogleMapsBuilderExtensions.cs index bfde91c..4b81f06 100644 --- a/Our.Umbraco.GMaps.Core/GoogleMapsBuilderExtensions.cs +++ b/Our.Umbraco.GMaps.Core/GoogleMapsBuilderExtensions.cs @@ -16,8 +16,8 @@ public static class GoogleMapsBuilderExtensions /// public static IUmbracoBuilder AddGoogleMaps(this IUmbracoBuilder builder) { - // if the GoogleMapsConfig Service is registered then we assume this has been added before so we don't do it again. - if (builder.Services.FirstOrDefault(x => x.ServiceType == typeof(GoogleMaps)) != null) + // If the GoogleMapsConfig Service is registered then we assume this has been added before so we don't do it again. + if (builder.Services.Any(x => x.ServiceType == typeof(GoogleMaps))) { return builder; } diff --git a/Our.Umbraco.GMaps.Core/Models/Location.cs b/Our.Umbraco.GMaps.Core/Models/Location.cs index 4fe7d39..8b378d4 100644 --- a/Our.Umbraco.GMaps.Core/Models/Location.cs +++ b/Our.Umbraco.GMaps.Core/Models/Location.cs @@ -8,9 +8,6 @@ namespace Our.Umbraco.GMaps.Models { public class Location { - [Obsolete("Use the ToString() method instead")] - public string Coordinates => ToString(); - [DataMember(Name = "lat")] [JsonProperty("lat")] [JsonPropertyName("lat")] @@ -28,12 +25,13 @@ public override string ToString() // Make sure coordinates are always formatted invariant (e.g. -1.23456789,12.3456789 vs. -1,23456789,12,3456789) return FormattableString.Invariant($"{Latitude}, {Longitude}"); } + /// /// Parse the coordinates string. /// /// /// - internal static Location Parse(string latLng) + public static Location Parse(string latLng) { if (!string.IsNullOrEmpty(latLng)) { diff --git a/Our.Umbraco.GMaps.Core/Models/MapConfig.cs b/Our.Umbraco.GMaps.Core/Models/MapConfig.cs index 9b52e47..91195f8 100644 --- a/Our.Umbraco.GMaps.Core/Models/MapConfig.cs +++ b/Our.Umbraco.GMaps.Core/Models/MapConfig.cs @@ -15,6 +15,7 @@ public class MapConfig [DataMember(Name = "zoom")] [JsonProperty("zoom")] [JsonPropertyName("zoom")] + [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] public int Zoom { get; set; } [DataMember(Name = "centerCoordinates")] diff --git a/Our.Umbraco.GMaps.Core/Our.Umbraco.GMaps.Core.csproj b/Our.Umbraco.GMaps.Core/Our.Umbraco.GMaps.Core.csproj index 97453a6..180ba66 100644 --- a/Our.Umbraco.GMaps.Core/Our.Umbraco.GMaps.Core.csproj +++ b/Our.Umbraco.GMaps.Core/Our.Umbraco.GMaps.Core.csproj @@ -3,7 +3,7 @@ net6.0;net7.0 True - 3.0.0-pre1 + 3.0.0-pre2 Arnold Visser Arnold Visser Basic Google Maps with autocomplete property editor for Umbraco 8+. This package contains the Core DLL only. diff --git a/Our.Umbraco.GMaps.Core/PropertyValueConverter/SingleMapPropertyValueConverter.cs b/Our.Umbraco.GMaps.Core/PropertyValueConverter/SingleMapPropertyValueConverter.cs index 7a38686..d7eb153 100644 --- a/Our.Umbraco.GMaps.Core/PropertyValueConverter/SingleMapPropertyValueConverter.cs +++ b/Our.Umbraco.GMaps.Core/PropertyValueConverter/SingleMapPropertyValueConverter.cs @@ -1,7 +1,6 @@ using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.PropertyEditors; using System; -using Newtonsoft.Json; using Our.Umbraco.GMaps.Models; using Our.Umbraco.GMaps.Core; using Our.Umbraco.GMaps.Core.Models.Configuration; @@ -9,6 +8,7 @@ using Our.Umbraco.GMaps.Core.Configuration; using Our.Umbraco.GMaps.Models.Legacy; using Microsoft.Extensions.Options; +using System.Text.Json; namespace Our.Umbraco.GMaps.PropertyValueConverter { @@ -37,7 +37,7 @@ public override object ConvertIntermediateToObject(IPublishedElement owner, IPub bool legacyData = inter.ToString().Contains("latlng"); if (legacyData) { - var intermediate = JsonConvert.DeserializeObject(inter.ToString()); + var intermediate = JsonSerializer.Deserialize(inter.ToString()); model = new Map { Address = intermediate.Address, @@ -51,7 +51,7 @@ public override object ConvertIntermediateToObject(IPublishedElement owner, IPub } else { - model = JsonConvert.DeserializeObject(inter.ToString()); + model = JsonSerializer.Deserialize(inter.ToString()); } } @@ -71,7 +71,7 @@ public override object ConvertIntermediateToObject(IPublishedElement owner, IPub if (config.TryGetValue("mapstyle", out var mapStyle) && mapStyle != null) { - var style = JsonConvert.DeserializeObject(mapStyle.ToString()); + var style = JsonSerializer.Deserialize(mapStyle.ToString()); model.MapConfig.Style = style?.Selectedstyle?.Json; } } diff --git a/Our.Umbraco.GMaps.UmbracoV12/Controllers/MapTestController.cs b/Our.Umbraco.GMaps.UmbracoV12/Controllers/MapTestController.cs index 5e64719..6c2824a 100644 --- a/Our.Umbraco.GMaps.UmbracoV12/Controllers/MapTestController.cs +++ b/Our.Umbraco.GMaps.UmbracoV12/Controllers/MapTestController.cs @@ -1,5 +1,5 @@ -using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json; +using System.Text.Json; +using Microsoft.AspNetCore.Mvc; using Our.Umbraco.GMaps.Models; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Web.Common.Controllers; @@ -40,7 +40,7 @@ public IActionResult CreateMapEntry() } }; - string json = JsonConvert.SerializeObject(gmap); + string json = JsonSerializer.Serialize(gmap); //Hack to get zoom to an int. Probably bug that's a string in model. //If a string the map won't show up and there is an error saying that zoom is not an int. diff --git a/Our.Umbraco.GMaps.UmbracoV13/Controllers/MapTestController.cs b/Our.Umbraco.GMaps.UmbracoV13/Controllers/MapTestController.cs index 402a9ae..7948b51 100644 --- a/Our.Umbraco.GMaps.UmbracoV13/Controllers/MapTestController.cs +++ b/Our.Umbraco.GMaps.UmbracoV13/Controllers/MapTestController.cs @@ -1,25 +1,23 @@ -using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json; +using System.Text.Json; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; using Our.Umbraco.GMaps.Models; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Web.Common.Controllers; namespace Our.Umbraco.GMaps.UmbracoV13.Controllers { - public class MapTestController : UmbracoApiController + public class MapTestController(IContentService contentService, + IOptionsMonitor mapsConfig, + ILogger logger) : UmbracoApiController { - private readonly IContentService contentService; - - public MapTestController(IContentService contentService) - { - this.contentService = contentService; - } - public IActionResult CreateMapEntry() { + logger.LogInformation("Testing Maps Configuration: {apiKey}", mapsConfig.CurrentValue.ApiKey); + double lat = -35.23989947459226; double lng = 149.149934680426; - Map gmap = new() + Map map = new() { Address = new Address { @@ -40,11 +38,13 @@ public IActionResult CreateMapEntry() } }; - string json = JsonConvert.SerializeObject(gmap); + logger.LogInformation("Map Details: {@Map}", map); + string json = JsonSerializer.Serialize(map); + // This is only needed for Newtonsoft.Json. System.Text.Json doesn't have this issue. //Hack to get zoom to an int. Probably bug that's a string in model. //If a string the map won't show up and there is an error saying that zoom is not an int. - json = json.Replace("\"zoom\":\"15\"", "\"zoom\":15"); + //json = json.Replace("\"zoom\":\"15\"", "\"zoom\":15"); var testContent = contentService.GetRootContent(); foreach (var n in testContent) diff --git a/Our.Umbraco.GMaps.UmbracoV13/appsettings.json b/Our.Umbraco.GMaps.UmbracoV13/appsettings.json index 0bb34d6..5376908 100644 --- a/Our.Umbraco.GMaps.UmbracoV13/appsettings.json +++ b/Our.Umbraco.GMaps.UmbracoV13/appsettings.json @@ -32,7 +32,7 @@ "umbracoDbDSN_ProviderName": "Microsoft.Data.SQLite" }, "GoogleMaps": { - "ApiKey": "", + "ApiKey": "test-key", "DefaultLocation": "", "ZoomLevel": 17 } diff --git a/Our.Umbraco.GMaps/Our.Umbraco.GMaps.csproj b/Our.Umbraco.GMaps/Our.Umbraco.GMaps.csproj index 7be1676..02f46b4 100644 --- a/Our.Umbraco.GMaps/Our.Umbraco.GMaps.csproj +++ b/Our.Umbraco.GMaps/Our.Umbraco.GMaps.csproj @@ -7,7 +7,7 @@ false true - 3.0.0-pre1 + 3.0.0-pre2 Arnold Visser Arnold Visser Basic Google Maps with autocomplete property editor for Umbraco 8+