Get rid of some more duplicate code

This commit is contained in:
Elitsa Marinovska
2021-08-17 13:09:25 +02:00
parent 285c9c92b5
commit d8b3a1bbe5
7 changed files with 18 additions and 40 deletions

View File

@@ -71,10 +71,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
var availableLanguages = _localizationService.GetAllLanguages();
if (id.HasValue)
{
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
var content = umbracoContext.Content.GetById(true, id.Value);
if (content is null)
return NotFound();

View File

@@ -11,6 +11,7 @@ using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Cms.Web.Common.Routing;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Common.Filters
{
@@ -55,10 +56,9 @@ namespace Umbraco.Cms.Web.Common.Filters
{
IUmbracoContextAccessor umbracoContextAccessor = context.HttpContext.RequestServices.GetRequiredService<IUmbracoContextAccessor>();
IPublishedRouter router = context.HttpContext.RequestServices.GetRequiredService<IPublishedRouter>();
if (!umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
IPublishedRequestBuilder requestBuilder = await router.CreateRequestAsync(umbracoContext.CleanedUmbracoUrl);
requestBuilder.SetPublishedContent(content);
IPublishedRequest publishedRequest = requestBuilder.Build();

View File

@@ -155,10 +155,8 @@ namespace Umbraco.Cms.Web.Common.Macros
// stores macro content into the cache
private async Task AddMacroContentToCacheAsync(MacroModel model, MacroContent macroContent)
{
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
// only if cache is enabled
if (umbracoContext.InPreviewMode || model.CacheDuration <= 0)
return;

View File

@@ -188,10 +188,8 @@ namespace Umbraco.Cms.Web.Common.Templates
private void SetNewItemsOnContextObjects(IPublishedRequest request)
{
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
// now, set the new ones for this page execution
umbracoContext.PublishedRequest = request;
}
@@ -201,10 +199,7 @@ namespace Umbraco.Cms.Web.Common.Templates
/// </summary>
private void SaveExistingItems(out IPublishedRequest oldPublishedRequest)
{
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
// Many objects require that these legacy items are in the http context items... before we render this template we need to first
// save the values in them so that we can re-set them after we render so the rest of the execution works as per normal
oldPublishedRequest = umbracoContext.PublishedRequest;
@@ -215,10 +210,7 @@ namespace Umbraco.Cms.Web.Common.Templates
/// </summary>
private void RestoreItems(IPublishedRequest oldPublishedRequest)
{
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
umbracoContext.PublishedRequest = oldPublishedRequest;
}
}

View File

@@ -5,6 +5,7 @@ using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Website.Models;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Website.Controllers
{
@@ -23,10 +24,7 @@ namespace Umbraco.Cms.Web.Website.Controllers
public ActionResult Index()
{
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
var store = umbracoContext.Content;
if (store.HasContent())
{

View File

@@ -68,10 +68,8 @@ namespace Umbraco.Extensions
/// </remarks>
public static IHtmlContent PreviewBadge(this IHtmlHelper helper, IUmbracoContextAccessor umbracoContextAccessor, IHttpContextAccessor httpContextAccessor, GlobalSettings globalSettings, IIOHelper ioHelper, ContentSettings contentSettings)
{
if (!umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
if (umbracoContext.InPreviewMode)
{
var htmlBadge =
@@ -672,10 +670,7 @@ namespace Umbraco.Extensions
}
IUmbracoContextAccessor umbracoContextAccessor = GetRequiredService<IUmbracoContextAccessor>(html);
if (!umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
var formAction = umbracoContext.OriginalRequestUrl.PathAndQuery;
return html.RenderForm(formAction, method, htmlAttributes, controllerName, action, area, additionalRouteVals);
}

View File

@@ -11,6 +11,7 @@ using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.Routing;
using Umbraco.Cms.Web.Website.Routing;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Website.Middleware
{
@@ -126,10 +127,7 @@ namespace Umbraco.Cms.Web.Website.Middleware
{
if (pageId != publishedRequest.PublishedContent.Id)
{
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
}
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
IPublishedContent publishedContent = umbracoContext.PublishedSnapshot.Content.GetById(pageId);
if (publishedContent == null)
{