Skip to content

Commit

Permalink
Add some null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjanwestendorp committed Jan 5, 2023
1 parent 0f4d80e commit 8d82857
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
14 changes: 9 additions & 5 deletions Umbraco.Docs.Samples.Web/Notifications/DontShout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ public void Handle(ContentPublishingNotification notification)
if (contentItem.ContentType.Alias.Equals("blogpost"))
{
var blogPostTitle = contentItem.GetValue<string>("pageTitle");
if (blogPostTitle.Equals(blogPostTitle.ToUpper()))

if (!string.IsNullOrWhiteSpace(blogPostTitle))
{
notification.Cancel = true;
notification.CancelOperation(new EventMessage("Corporate style guideline infringement",
"Don't put the blog post title in upper case, no need to shout! Publishing was cancelled",
EventMessageType.Error));
if (blogPostTitle.Equals(blogPostTitle.ToUpper()))
{
notification.Cancel = true;
notification.CancelOperation(new EventMessage("Corporate style guideline infringement",
"Don't put the blog post title in upper case, no need to shout! Publishing was cancelled",
EventMessageType.Error));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace Umbraco.Docs.Samples.Web.Property_Editors_Add_Values
{
public class CreateImageCropperValuesController : UmbracoApiController
{
private IContentService _contentService;
private IMediaService _mediaService;
private MediaUrlGeneratorCollection _mediaUrlGeneratorCollection;
private IPublishedSnapshotAccessor _publishedSnapshotAccessor;
private readonly IContentService _contentService;
private readonly IMediaService _mediaService;
private readonly MediaUrlGeneratorCollection _mediaUrlGeneratorCollection;
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;


public CreateImageCropperValuesController(IContentService contentService, IMediaService mediaService, MediaUrlGeneratorCollection mediaUrlGeneratorCollection, IPublishedSnapshotAccessor publishedSnapshotAccessor)
Expand Down Expand Up @@ -48,10 +48,16 @@ public ActionResult<string> CreateImageCropperValues()
// Serialize the image cropper value
var cropperValue = JsonConvert.SerializeObject(cropper);

var testCropperAlias = Product.GetModelPropertyType(_publishedSnapshotAccessor, x => x.TestCropper)?.Alias;

// Set the value of the property with alias 'cropper'
content.SetValue("testCropper", cropperValue, "en-US");
if (content != null && !string.IsNullOrWhiteSpace(testCropperAlias))
{
content.SetValue("testCropper", cropperValue, "en-US");

content.SetValue(Product.GetModelPropertyType(_publishedSnapshotAccessor,x => x.TestCropper).Alias, cropperValue, "en-US");
content.SetValue(testCropperAlias, cropperValue, "en-US");
}


return _contentService.Save(content).Success.ToString();
}
Expand All @@ -64,10 +70,21 @@ internal Dictionary<string, string> GetCropUrls(IPublishedContent image)
if (image.HasValue("umbracoFile"))
{
var imageCropper = image.Value<ImageCropperValue>("umbracoFile");
foreach (var crop in imageCropper.Crops)

if (imageCropper != null && imageCropper.Crops != null)
{
//Get the cropped URL and add it to the dictionary that I will return
cropUrls.Add(crop.Alias, image.GetCropUrl(crop.Alias));
foreach (var crop in imageCropper.Crops)
{
//Get the cropped URL and add it to the dictionary that I will return
if (!string.IsNullOrWhiteSpace(crop.Alias))
{
var cropUrl = image.GetCropUrl(crop.Alias);
if (!string.IsNullOrEmpty(cropUrl))
{
cropUrls.Add(crop.Alias, cropUrl);
}
}
}
}
}

Expand Down

0 comments on commit 8d82857

Please sign in to comment.