Skip to content

Commit 9f6770d

Browse files
Add OpenAPI, JS context, JS repository
1 parent a7873eb commit 9f6770d

37 files changed

+3081
-89
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#if NET8_0
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Options;
4+
using Microsoft.OpenApi.Models;
5+
using Swashbuckle.AspNetCore.SwaggerGen;
6+
7+
namespace Umbraco.Community.Sustainability
8+
{
9+
public class ConfigureSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions>
10+
{
11+
public void Configure(SwaggerGenOptions options)
12+
{
13+
options.SwaggerDoc(
14+
"sustainability",
15+
new OpenApiInfo
16+
{
17+
Title = "Sustainability API",
18+
Version = "Latest",
19+
Description = "Umbraco.Community.Sustainability"
20+
});
21+
}
22+
}
23+
}
24+
#endif
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#if NET8_0
2+
using System.Text.Json;
3+
using Asp.Versioning;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Umbraco.Cms.Core;
6+
using Umbraco.Cms.Core.Models.PublishedContent;
7+
using Umbraco.Community.Sustainability.Models;
8+
using Umbraco.Community.Sustainability.Schemas;
9+
using Umbraco.Community.Sustainability.Services;
10+
using Umbraco.Extensions;
11+
12+
namespace Umbraco.Community.Sustainability.Controllers
13+
{
14+
[ApiVersion("1.0")]
15+
[ApiExplorerSettings(GroupName = "sustainability")]
16+
public class SustainabilityWorkspaceController : SustainabilityWorkspaceControllerBase
17+
{
18+
private readonly IPublishedContentQuery _contentQuery;
19+
private readonly IPageMetricService _pageMetricService;
20+
private readonly ISustainabilityService _sustainabilityService;
21+
22+
public SustainabilityWorkspaceController(
23+
IPublishedContentQuery contentQuery,
24+
IPageMetricService pageMetricService,
25+
ISustainabilityService sustainabilityService)
26+
{
27+
_contentQuery = contentQuery;
28+
_pageMetricService = pageMetricService;
29+
_sustainabilityService = sustainabilityService;
30+
}
31+
32+
[HttpGet("getPageData")]
33+
[ProducesResponseType(typeof(string), 200)]
34+
public async Task<IActionResult> GetPageData([FromQuery] int pageId)
35+
{
36+
var pageMetrics = await _pageMetricService.GetPageMetrics(pageId);
37+
var mostRecent = pageMetrics.OrderByDescending(x => x.RequestDate).FirstOrDefault();
38+
if (mostRecent?.PageData == null)
39+
{
40+
return Ok("No recent data found");
41+
}
42+
43+
var sustainabilityData = JsonSerializer.Deserialize<SustainabilityResponse>(mostRecent.PageData);
44+
return Ok(sustainabilityData);
45+
}
46+
47+
[HttpGet("checkPage")]
48+
public async Task<IActionResult> CheckPage([FromQuery] int pageId)
49+
{
50+
var contentItem = _contentQuery.Content(pageId);
51+
if (contentItem == null)
52+
{
53+
return Ok("Page not found");
54+
}
55+
56+
var url = contentItem.Url(mode: UrlMode.Absolute);
57+
var sustainabilityData = await _sustainabilityService.GetSustainabilityData(url);
58+
59+
return Ok(sustainabilityData);
60+
}
61+
62+
[HttpPost("savePageData")]
63+
public async Task<IActionResult> SavePageData([FromQuery] int pageId, [FromBody] SustainabilityResponse data)
64+
{
65+
if (data.TotalSize == 0)
66+
{
67+
return Ok("Missing data to update");
68+
}
69+
70+
var pageMetric = new PageMetric()
71+
{
72+
NodeId = pageId,
73+
RequestedBy = "Admin",
74+
RequestDate = data.LastRunDate,
75+
TotalSize = data.TotalSize,
76+
TotalEmissions = Convert.ToDecimal(data.TotalEmissions),
77+
CarbonRating = data.CarbonRating,
78+
PageData = JsonSerializer.Serialize(data),
79+
};
80+
81+
await _pageMetricService.AddPageMetric(pageMetric);
82+
return Ok(true);
83+
}
84+
}
85+
}
86+
#endif
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#if NET8_0
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Umbraco.Cms.Web.Common.Authorization;
5+
using Umbraco.Cms.Web.Common.Routing;
6+
using Umbraco.Cms.Api.Common.Attributes;
7+
8+
namespace Umbraco.Community.Sustainability.Controllers
9+
{
10+
[ApiController]
11+
[BackOfficeRoute("sustainability/api/v{version:apiVersion}")]
12+
[Authorize(Policy = "New" + AuthorizationPolicies.BackOfficeAccess)]
13+
[MapToApi("sustainability")]
14+
public class SustainabilityWorkspaceControllerBase : Controller
15+
{ }
16+
}
17+
#endif

src/Umbraco.Community.Sustainability/SustainabilityComposer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public void Compose(IUmbracoBuilder builder)
2121

2222
builder.Services.AddSingleton<IPageMetricService, PageMetricService>();
2323
builder.Services.AddSingleton<ISustainabilityService, SustainabilityService>();
24+
25+
#if NET8_0
26+
builder.Services.ConfigureOptions<ConfigureSwaggerGenOptions>();
27+
#endif
2428
}
2529
}
2630
}

src/Umbraco.Community.Sustainability/Umbraco.Community.Sustainability.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
4141
<PackageReference Include="Umbraco.Cms.Web.Website" Version="[13.0.0, 15.0.0)" />
42+
<PackageReference Include="Umbraco.Cms.Api.Common" Version="[13.0.0, 15.0.0)" />
4243
<PackageReference Include="Umbraco.Cms.Web.BackOffice" Version="[13.0.0, 15.0.0)" />
4344
</ItemGroup>
4445

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
3+
"spaces": 2,
4+
"generator-cli": {
5+
"version": "7.2.0"
6+
}
7+
}

0 commit comments

Comments
 (0)