Skip to content

Commit 8d82857

Browse files
Add some null checks
1 parent 0f4d80e commit 8d82857

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

Umbraco.Docs.Samples.Web/Notifications/DontShout.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ public void Handle(ContentPublishingNotification notification)
1313
if (contentItem.ContentType.Alias.Equals("blogpost"))
1414
{
1515
var blogPostTitle = contentItem.GetValue<string>("pageTitle");
16-
if (blogPostTitle.Equals(blogPostTitle.ToUpper()))
16+
17+
if (!string.IsNullOrWhiteSpace(blogPostTitle))
1718
{
18-
notification.Cancel = true;
19-
notification.CancelOperation(new EventMessage("Corporate style guideline infringement",
20-
"Don't put the blog post title in upper case, no need to shout! Publishing was cancelled",
21-
EventMessageType.Error));
19+
if (blogPostTitle.Equals(blogPostTitle.ToUpper()))
20+
{
21+
notification.Cancel = true;
22+
notification.CancelOperation(new EventMessage("Corporate style guideline infringement",
23+
"Don't put the blog post title in upper case, no need to shout! Publishing was cancelled",
24+
EventMessageType.Error));
25+
}
2226
}
2327
}
2428
}

Umbraco.Docs.Samples.Web/Property-Editors-Add-Values/CreateImageCropperValuesController.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ namespace Umbraco.Docs.Samples.Web.Property_Editors_Add_Values
1212
{
1313
public class CreateImageCropperValuesController : UmbracoApiController
1414
{
15-
private IContentService _contentService;
16-
private IMediaService _mediaService;
17-
private MediaUrlGeneratorCollection _mediaUrlGeneratorCollection;
18-
private IPublishedSnapshotAccessor _publishedSnapshotAccessor;
15+
private readonly IContentService _contentService;
16+
private readonly IMediaService _mediaService;
17+
private readonly MediaUrlGeneratorCollection _mediaUrlGeneratorCollection;
18+
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
1919

2020

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

51+
var testCropperAlias = Product.GetModelPropertyType(_publishedSnapshotAccessor, x => x.TestCropper)?.Alias;
52+
5153
// Set the value of the property with alias 'cropper'
52-
content.SetValue("testCropper", cropperValue, "en-US");
54+
if (content != null && !string.IsNullOrWhiteSpace(testCropperAlias))
55+
{
56+
content.SetValue("testCropper", cropperValue, "en-US");
5357

54-
content.SetValue(Product.GetModelPropertyType(_publishedSnapshotAccessor,x => x.TestCropper).Alias, cropperValue, "en-US");
58+
content.SetValue(testCropperAlias, cropperValue, "en-US");
59+
}
60+
5561

5662
return _contentService.Save(content).Success.ToString();
5763
}
@@ -64,10 +70,21 @@ internal Dictionary<string, string> GetCropUrls(IPublishedContent image)
6470
if (image.HasValue("umbracoFile"))
6571
{
6672
var imageCropper = image.Value<ImageCropperValue>("umbracoFile");
67-
foreach (var crop in imageCropper.Crops)
73+
74+
if (imageCropper != null && imageCropper.Crops != null)
6875
{
69-
//Get the cropped URL and add it to the dictionary that I will return
70-
cropUrls.Add(crop.Alias, image.GetCropUrl(crop.Alias));
76+
foreach (var crop in imageCropper.Crops)
77+
{
78+
//Get the cropped URL and add it to the dictionary that I will return
79+
if (!string.IsNullOrWhiteSpace(crop.Alias))
80+
{
81+
var cropUrl = image.GetCropUrl(crop.Alias);
82+
if (!string.IsNullOrEmpty(cropUrl))
83+
{
84+
cropUrls.Add(crop.Alias, cropUrl);
85+
}
86+
}
87+
}
7188
}
7289
}
7390

0 commit comments

Comments
 (0)