Skip to content

Commit

Permalink
Merge pull request #10 from rickbutterfield/feature/nodeid-to-nodekey
Browse files Browse the repository at this point in the history
Change NodeId to NodeKey for better v14 support/migrations
  • Loading branch information
rickbutterfield authored May 2, 2024
2 parents 8719ff2 + 79ada5f commit 9209be2
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


@using Umbraco.Extensions
@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Home>
Expand Down Expand Up @@ -42,4 +42,4 @@
</div>
</div>

</section>
</section>
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Diagnostics;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.Operations;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
Expand Down Expand Up @@ -78,9 +76,9 @@ public async Task<IActionResult> GetAverageData()
}

[HttpGet]
public async Task<IActionResult> GetPageData([FromQuery] int pageId)
public async Task<IActionResult> GetPageData([FromQuery] Guid pageKey)
{
var pageMetrics = await _pageMetricService.GetPageMetrics(pageId);
var pageMetrics = await _pageMetricService.GetPageMetrics(pageKey);
var mostRecent = pageMetrics.OrderByDescending(x => x.RequestDate).FirstOrDefault();
if (mostRecent?.PageData == null)
{
Expand All @@ -92,9 +90,9 @@ public async Task<IActionResult> GetPageData([FromQuery] int pageId)
}

[HttpGet]
public async Task<IActionResult> CheckPage([FromQuery] int pageId)
public async Task<IActionResult> CheckPage([FromQuery] Guid pageKey)
{
var contentItem = _contentQuery.Content(pageId);
var contentItem = _contentQuery.Content(pageKey);
if (contentItem == null)
{
return Ok("Page not found");
Expand All @@ -107,7 +105,7 @@ public async Task<IActionResult> CheckPage([FromQuery] int pageId)
}

[HttpPost]
public async Task<IActionResult> SavePageData([FromQuery] int pageId, [FromBody] SustainabilityResponse data)
public async Task<IActionResult> SavePageData([FromQuery] Guid pageKey, [FromBody] SustainabilityResponse data)
{
if (data.TotalSize == 0)
{
Expand All @@ -116,7 +114,7 @@ public async Task<IActionResult> SavePageData([FromQuery] int pageId, [FromBody]

var pageMetric = new PageMetric()
{
NodeId = pageId,
NodeKey = pageKey,
RequestedBy = "Admin",
RequestDate = data.LastRunDate,
TotalSize = data.TotalSize,
Expand All @@ -129,7 +127,7 @@ public async Task<IActionResult> SavePageData([FromQuery] int pageId, [FromBody]
return Ok(true);
}

private int GetCarbonRatingOrder(string carbonRating)
private int GetCarbonRatingOrder(string? carbonRating)
{
switch (carbonRating)
{
Expand All @@ -146,7 +144,6 @@ private int GetCarbonRatingOrder(string carbonRating)
case "E":
return 6;
case "F":
return 7;
default:
return 7;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Infrastructure.Migrations;
using Umbraco.Community.Sustainability.Schemas;

namespace Umbraco.Community.Sustainability.Migrations
{
public class ChangeNodeIdToNodeKey : MigrationBase
{
public ChangeNodeIdToNodeKey(IMigrationContext context) : base(context)
{
}

protected override void Migrate()
{
Logger.LogDebug("Running migration {MigrationStep}", "ChangeNodeIdToNodeKey");

if (ColumnExists(PageMetric.TableName, "NodeId"))
{
Alter.Table(PageMetric.TableName).AddColumn("NodeKey").AsGuid().Nullable().Do();

Database.Execute($@"UPDATE {PageMetric.TableName} SET NodeKey = {Cms.Core.Constants.DatabaseSchema.Tables.Node}.uniqueId
FROM {Cms.Core.Constants.DatabaseSchema.Tables.Node}
INNER JOIN {PageMetric.TableName} ON {Cms.Core.Constants.DatabaseSchema.Tables.Node}.id = {PageMetric.TableName}.NodeId");

Delete.Column("NodeId").FromTable(PageMetric.TableName).Do();
}
else
{
Logger.LogDebug("The column NodeKey already exists, skipping");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void Handle(UmbracoApplicationStartingNotification notification)
// Each step in the migration adds a unique value
migrationPlan.From(string.Empty)
.To<AddPageMetricsTable>("pagemetrics-init")
.To<AddCarbonRating>("pagemetrics-carbonrating");
.To<AddCarbonRating>("pagemetrics-carbonrating")
.To<ChangeNodeIdToNodeKey>("pagemetrics-nodeidtonodekey");

// Go and upgrade our site (Will check if it needs to do the work or not)
// Based on the current/latest step
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class PageMetric
[Column("Id")]
public int Id { get; set; }

[Column("NodeId")]
public int NodeId { get; set; }
[Column("NodeKey")]
public Guid? NodeKey { get; set; }

[Ignore]
public string? NodeName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using Umbraco.Cms.Infrastructure.Scoping;
using Umbraco.Community.Sustainability.Models;
using Umbraco.Community.Sustainability.Schemas;
using Umbraco.Extensions;

namespace Umbraco.Community.Sustainability.Services
{
public interface IPageMetricService
{
Task<IEnumerable<PageMetric>> GetOverviewMetrics();
Task<AveragePageMetrics> GetAverageMetrics();
Task<IEnumerable<PageMetric>> GetPageMetrics(int pageId);
Task<IEnumerable<PageMetric>> GetPageMetrics(Guid pageKey);
Task AddPageMetric(PageMetric pageMetric);
}

Expand All @@ -32,11 +33,11 @@ public async Task<IEnumerable<PageMetric>> GetOverviewMetrics()
var queryResults = await scope.Database.FetchAsync<PageMetric>();

queryResults = queryResults.OrderByDescending(x => x.RequestDate).ToList();
queryResults = queryResults.DistinctBy(x => x.NodeId).ToList();
queryResults = queryResults.DistinctBy(x => x.NodeKey).ToList();

foreach (var result in queryResults)
{
var node = _contentQuery.Content(result.NodeId);
var node = _contentQuery.Content(result.NodeKey);
result.NodeName = node?.Name;
}

Expand All @@ -61,10 +62,10 @@ public async Task<AveragePageMetrics> GetAverageMetrics()
return new();
}

public async Task<IEnumerable<PageMetric>> GetPageMetrics(int pageId)
public async Task<IEnumerable<PageMetric>> GetPageMetrics(Guid pageKey)
{
using var scope = _scopeProvider.CreateScope();
var queryResults = await scope.Database.FetchAsync<PageMetric>($"SELECT * FROM {PageMetric.TableName} WHERE NodeId = @0", pageId);
var queryResults = await scope.Database.FetchAsync<PageMetric>($"SELECT * FROM {PageMetric.TableName} WHERE NodeKey = @0", pageKey);
scope.Complete();

return queryResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ angular.module('umbraco').controller('Umbraco.Sustainability.ContentApp.Controll
let vm = this;

$scope.id = "";
$scope.key = null;
$scope.loading = true;

vm.buttonState = undefined;
Expand All @@ -14,8 +15,9 @@ angular.module('umbraco').controller('Umbraco.Sustainability.ContentApp.Controll

function init() {
$scope.id = editorState.current.id;
$scope.key = editorState.current.key;

sustainabilityResource.getData($scope.id).then(function (data) {
sustainabilityResource.getData($scope.key).then(function (data) {
$scope.sustainabilityData = data;
updateResults();
});
Expand All @@ -24,11 +26,11 @@ angular.module('umbraco').controller('Umbraco.Sustainability.ContentApp.Controll
function checkPage() {
vm.buttonState = "waiting";

sustainabilityResource.checkPage($scope.id).then(function (data) {
sustainabilityResource.checkPage($scope.key).then(function (data) {
$scope.sustainabilityData = data;
updateResults();

sustainabilityResource.saveResult($scope.id, $scope.sustainabilityData);
sustainabilityResource.saveResult($scope.key, $scope.sustainabilityData);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
calculateGrade: calculateGrade
};

function getData(pageId) {
function getData(pageKey) {
return umbRequestHelper.resourcePromise(
$http.get(`${apiUrl}GetPageData?pageId=${pageId}`),
$http.get(`${apiUrl}GetPageData?pageKey=${pageKey}`),
'Failed getting sustainability data'
);
};
Expand Down Expand Up @@ -45,16 +45,16 @@
);
};

function checkPage(pageId) {
function checkPage(pageKey) {
return umbRequestHelper.resourcePromise(
$http.get(`${apiUrl}CheckPage?pageId=${pageId}`),
$http.get(`${apiUrl}CheckPage?pageKey=${pageKey}`),
'Failed to run sustainability check'
);
};

function saveResult(pageId, data) {
function saveResult(pageKey, data) {
return umbRequestHelper.resourcePromise(
$http.post(`${apiUrl}SavePageData?pageId=${pageId}`, data),
$http.post(`${apiUrl}SavePageData?pageKey=${pageKey}`, data),
'Failed to save sustainability data'
);
};
Expand Down

0 comments on commit 9209be2

Please sign in to comment.