Skip to content

Commit

Permalink
Addressing warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Psichorex committed Jan 20, 2024
1 parent 73f96cc commit 523249b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 76 deletions.
60 changes: 20 additions & 40 deletions Lombiq.JsonEditor/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,19 @@

namespace Lombiq.JsonEditor.Controllers;

public class AdminController : Controller
public class AdminController(
IContentDefinitionManager contentDefinitionManager,
ILayoutAccessor layoutAccessor,
INotifier notifier,
IPageTitleBuilder pageTitleBuilder,
IShapeFactory shapeFactory,
IOrchardServices<AdminController> services,
Lazy<ApiController> contentApiControllerLazy) : Controller
{
private readonly IAuthorizationService _authorizationService;
private readonly IContentManager _contentManager;
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly ILayoutAccessor _layoutAccessor;
private readonly INotifier _notifier;
private readonly IPageTitleBuilder _pageTitleBuilder;
private readonly IShapeFactory _shapeFactory;
private readonly Lazy<ApiController> _contentApiControllerLazy;
private readonly IStringLocalizer<AdminController> T;
private readonly IHtmlLocalizer<AdminController> H;

public AdminController(
IContentDefinitionManager contentDefinitionManager,
ILayoutAccessor layoutAccessor,
INotifier notifier,
IPageTitleBuilder pageTitleBuilder,
IShapeFactory shapeFactory,
IOrchardServices<AdminController> services,
Lazy<ApiController> contentApiControllerLazy)
{
_authorizationService = services.AuthorizationService.Value;
_contentManager = services.ContentManager.Value;
_contentDefinitionManager = contentDefinitionManager;
_layoutAccessor = layoutAccessor;
_notifier = notifier;
_pageTitleBuilder = pageTitleBuilder;
_shapeFactory = shapeFactory;
_contentApiControllerLazy = contentApiControllerLazy;
T = services.StringLocalizer.Value;
H = services.HtmlLocalizer.Value;
}
private readonly IAuthorizationService _authorizationService = services.AuthorizationService.Value;
private readonly IContentManager _contentManager = services.ContentManager.Value;
private readonly IStringLocalizer<AdminController> T = services.StringLocalizer.Value;
private readonly IHtmlLocalizer<AdminController> H = services.HtmlLocalizer.Value;

public async Task<IActionResult> Edit(string contentItemId)
{
Expand All @@ -67,15 +47,15 @@ await _contentManager.GetAsync(contentItemId, VersionOptions.Latest) is not { }
}

var title = T["Edit {0} as JSON", GetName(contentItem)].Value;
_pageTitleBuilder.AddSegment(new StringHtmlContent(title));
var titleShape = await _shapeFactory.CreateAsync<TitlePartViewModel>("TitlePart", model =>
pageTitleBuilder.AddSegment(new StringHtmlContent(title));
var titleShape = await shapeFactory.CreateAsync<TitlePartViewModel>("TitlePart", model =>
{
model.Title = title;
model.ContentItem = contentItem;
});
await _layoutAccessor.AddShapeToZoneAsync("Title", titleShape);
await layoutAccessor.AddShapeToZoneAsync("Title", titleShape);

var definition = await _contentDefinitionManager.GetTypeDefinitionAsync(contentItem.ContentType);
var definition = await contentDefinitionManager.GetTypeDefinitionAsync(contentItem.ContentType);
return View(new EditContentItemViewModel(contentItem, definition, JsonConvert.SerializeObject(contentItem)));
}

Expand Down Expand Up @@ -107,13 +87,13 @@ public async Task<IActionResult> EditPost(
{
case BadRequestObjectResult { Value: ValidationProblemDetails details }
when !string.IsNullOrWhiteSpace(details.Detail):
await _notifier.ErrorAsync(new LocalizedHtmlString(details.Detail, details.Detail));
await notifier.ErrorAsync(new LocalizedHtmlString(details.Detail, details.Detail));
return await Edit(contentItem.ContentItemId);
case OkObjectResult:
await _notifier.SuccessAsync(H["Content item {0} has been successfully saved.", GetName(contentItem)]);
await notifier.SuccessAsync(H["Content item {0} has been successfully saved.", GetName(contentItem)]);
break;
default:
await _notifier.ErrorAsync(H["The submission has failed, please try again."]);
await notifier.ErrorAsync(H["The submission has failed, please try again."]);
return await Edit(contentItem.ContentItemId);
}

Expand Down Expand Up @@ -142,7 +122,7 @@ private async Task<IActionResult> UpdateContentAsync(ContentItem contentItem, bo
{
// Here the API controller is called directly. The behavior is the same as if we sent a POST request using an
// HTTP client (except the permission bypass above), but it's faster and more resource-efficient.
var contentApiController = _contentApiControllerLazy.Value;
var contentApiController = contentApiControllerLazy.Value;
contentApiController.ControllerContext.HttpContext = HttpContext;
return await contentApiController.Post(contentItem, isDraft);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Display.ContentDisplay;
Expand All @@ -10,19 +10,10 @@

namespace Lombiq.JsonEditor.Drivers;

public class EditJsonActionsMenuContentDisplayDriver : ContentDisplayDriver
public class EditJsonActionsMenuContentDisplayDriver(IAuthorizationService authorizationService, IHttpContextAccessor hca) : ContentDisplayDriver
{
private readonly IAuthorizationService _authorizationService;
private readonly IHttpContextAccessor _hca;

public EditJsonActionsMenuContentDisplayDriver(IAuthorizationService authorizationService, IHttpContextAccessor hca)
{
_authorizationService = authorizationService;
_hca = hca;
}

public override async Task<IDisplayResult> DisplayAsync(ContentItem model, BuildDisplayContext context) =>
await _authorizationService.AuthorizeAsync(_hca.HttpContext?.User, CommonPermissions.EditContent, model)
await authorizationService.AuthorizeAsync(hca.HttpContext?.User, CommonPermissions.EditContent, model)
? Initialize<ContentItemViewModel>("Content_EditJsonActions", viewModel =>
viewModel.ContentItem = model.ContentItem)
.Location("ActionsMenu:after")
Expand Down
6 changes: 2 additions & 4 deletions Lombiq.JsonEditor/Drivers/JsonFieldDisplayDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@

namespace Lombiq.JsonEditor.Drivers;

public class JsonFieldDisplayDriver : ContentFieldDisplayDriver<JsonField>
public class JsonFieldDisplayDriver(IStringLocalizer<JsonFieldDisplayDriver> stringLocalizer) : ContentFieldDisplayDriver<JsonField>
{
private readonly IStringLocalizer T;

public JsonFieldDisplayDriver(IStringLocalizer<JsonFieldDisplayDriver> stringLocalizer) => T = stringLocalizer;
private readonly IStringLocalizer T = stringLocalizer;

public override IDisplayResult Display(JsonField field, BuildFieldDisplayContext fieldDisplayContext) =>
Initialize<DisplayJsonFieldViewModel>(GetDisplayShapeType(fieldDisplayContext), model =>
Expand Down
6 changes: 2 additions & 4 deletions Lombiq.JsonEditor/Settings/JsonFieldSettingsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@

namespace Lombiq.JsonEditor.Settings;

public class JsonFieldSettingsDriver : ContentPartFieldDefinitionDisplayDriver<JsonField>
public class JsonFieldSettingsDriver(IStringLocalizer<JsonFieldSettingsDriver> stringLocalizer) : ContentPartFieldDefinitionDisplayDriver<JsonField>
{
private readonly IStringLocalizer T;

public JsonFieldSettingsDriver(IStringLocalizer<JsonFieldSettingsDriver> stringLocalizer) => T = stringLocalizer;
private readonly IStringLocalizer T = stringLocalizer;

public override IDisplayResult Edit(ContentPartFieldDefinition model) =>
Initialize<JsonFieldSettings>($"{nameof(JsonFieldSettings)}_Edit", model.PopulateSettings)
Expand Down
6 changes: 2 additions & 4 deletions Lombiq.JsonEditor/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ public override void ConfigureServices(IServiceCollection services)
}

[Feature(FeatureIds.ContentEditor)]
public class ContentEditorStartup : StartupBase
public class ContentEditorStartup(IOptions<AdminOptions> adminOptions) : StartupBase
{
private readonly AdminOptions _adminOptions;

public ContentEditorStartup(IOptions<AdminOptions> adminOptions) => _adminOptions = adminOptions.Value;
private readonly AdminOptions _adminOptions = adminOptions.Value;

public override void ConfigureServices(IServiceCollection services)
{
Expand Down
15 changes: 3 additions & 12 deletions Lombiq.JsonEditor/TagHelpers/JsonEditorTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
namespace Lombiq.JsonEditor.TagHelpers;

[HtmlTargetElement("json-editor")]
public class JsonEditorTagHelper : TagHelper
public class JsonEditorTagHelper(IDisplayHelper displayHelper, IShapeFactory factory) : TagHelper
{
private readonly IDisplayHelper _displayHelper;
private readonly IShapeFactory _shapeFactory;

[HtmlAttributeName("content")]
public object Content { get; set; }

Expand All @@ -24,20 +21,14 @@ public class JsonEditorTagHelper : TagHelper
[HtmlAttributeName("name")]
public string InputName { get; set; }

public JsonEditorTagHelper(IDisplayHelper displayHelper, IShapeFactory factory)
{
_displayHelper = displayHelper;
_shapeFactory = factory;
}

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var shape = await _shapeFactory.New.JsonEditor(
var shape = await factory.New.JsonEditor(
Content: Content,
SerializedJson: SerializedJson,
Options: Options,
InputName: InputName);
var content = (IHtmlContent)await _displayHelper.ShapeExecuteAsync(shape);
var content = (IHtmlContent)await displayHelper.ShapeExecuteAsync(shape);

output.TagName = null;
output.TagMode = TagMode.StartTagAndEndTag;
Expand Down

0 comments on commit 523249b

Please sign in to comment.