Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and other improvements #154

Merged
merged 12 commits into from
Dec 5, 2023
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
22 changes: 1 addition & 21 deletions Our.Umbraco.GMaps.Core/Composing/Composer.cs
Original file line number Diff line number Diff line change
@@ -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<ServerVariablesParsingNotification, ServerVariablesParsingHandler>();
}
#else
public void Compose(Composition composition)
{
composition.Register<GoogleMapsConfig>();
composition.Components().Append<RegisterServerVariables>();
}
#endif
}
}
53 changes: 0 additions & 53 deletions Our.Umbraco.GMaps.Core/Composing/RegisterServerVariables.v8.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,49 +13,32 @@ internal class ServerVariablesParsingHandler :
{
private readonly LinkGenerator linkGenerator;

public ServerVariablesParsingHandler(LinkGenerator linkGenerator)
{
this.linkGenerator = linkGenerator;
}

public void Handle(ServerVariablesParsingNotification notification)
{
IDictionary<string, object> 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<string, object> umbracoUrls))
public void Handle(ServerVariablesParsingNotification notification)
{
throw new ArgumentException("Invalid umbracoUrls");
}
IDictionary<string, object> 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<string, object> umbracoPlugins))
{
throw new ArgumentException("Invalid umbracoPlugins");
}
if (umbracoUrlsObject is not Dictionary<string, object> umbracoUrls)
{
throw new ArgumentException("Invalid umbracoUrls");
}

var gMapsBaseUrl = linkGenerator.GetUmbracoApiServiceBaseUrl<GoogleMapsController>(controller =>
controller.GetSettings());
var gMapsBaseUrl = linkGenerator.GetUmbracoApiServiceBaseUrl<GoogleMapsController>(controller =>
controller.GetSettings());

if (!umbracoUrls.ContainsKey(nameof(gMapsBaseUrl)))
{
umbracoUrls[nameof(gMapsBaseUrl)] = gMapsBaseUrl;
if (!umbracoUrls.ContainsKey(nameof(gMapsBaseUrl)))
{
umbracoUrls[nameof(gMapsBaseUrl)] = gMapsBaseUrl;
}
}
}
}
}
#endif
}
4 changes: 2 additions & 2 deletions Our.Umbraco.GMaps.Core/GoogleMapsBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public static class GoogleMapsBuilderExtensions
/// <returns></returns>
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;
}
Expand Down
6 changes: 2 additions & 4 deletions Our.Umbraco.GMaps.Core/Models/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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}");
}

/// <summary>
/// Parse the coordinates string.
/// </summary>
/// <param name="latLng"></param>
/// <returns></returns>
internal static Location Parse(string latLng)
public static Location Parse(string latLng)
{
if (!string.IsNullOrEmpty(latLng))
{
Expand Down
1 change: 1 addition & 0 deletions Our.Umbraco.GMaps.Core/Models/MapConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion Our.Umbraco.GMaps.Core/Our.Umbraco.GMaps.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>

<Version>3.0.0-pre1</Version>
<Version>3.0.0-pre2</Version>
<Authors>Arnold Visser</Authors>
<Company>Arnold Visser</Company>
<Description>Basic Google Maps with autocomplete property editor for Umbraco 8+. This package contains the Core DLL only.</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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;
using System.Collections.Generic;
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
{
Expand Down Expand Up @@ -37,7 +37,7 @@ public override object ConvertIntermediateToObject(IPublishedElement owner, IPub
bool legacyData = inter.ToString().Contains("latlng");
if (legacyData)
{
var intermediate = JsonConvert.DeserializeObject<LegacyMap>(inter.ToString());
var intermediate = JsonSerializer.Deserialize<LegacyMap>(inter.ToString());
model = new Map
{
Address = intermediate.Address,
Expand All @@ -51,7 +51,7 @@ public override object ConvertIntermediateToObject(IPublishedElement owner, IPub
}
else
{
model = JsonConvert.DeserializeObject<Map>(inter.ToString());
model = JsonSerializer.Deserialize<Map>(inter.ToString());
}
}

Expand All @@ -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>(mapStyle.ToString());
var style = JsonSerializer.Deserialize<MapStyle>(mapStyle.ToString());
model.MapConfig.Style = style?.Selectedstyle?.Json;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Our.Umbraco.GMaps.UmbracoV12/Controllers/MapTestController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 13 additions & 13 deletions Our.Umbraco.GMaps.UmbracoV13/Controllers/MapTestController.cs
Original file line number Diff line number Diff line change
@@ -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<Core.Configuration.GoogleMaps> mapsConfig,
ILogger<MapTestController> 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
{
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Our.Umbraco.GMaps.UmbracoV13/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"umbracoDbDSN_ProviderName": "Microsoft.Data.SQLite"
},
"GoogleMaps": {
"ApiKey": "",
"ApiKey": "test-key",
"DefaultLocation": "",
"ZoomLevel": 17
}
Expand Down
2 changes: 1 addition & 1 deletion Our.Umbraco.GMaps/Our.Umbraco.GMaps.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<IncludeBuildOutput>false</IncludeBuildOutput>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

<Version>3.0.0-pre1</Version>
<Version>3.0.0-pre2</Version>
<Authors>Arnold Visser</Authors>
<Company>Arnold Visser</Company>
<Description>Basic Google Maps with autocomplete property editor for Umbraco 8+</Description>
Expand Down
Loading