Guest User

Untitled

a guest
Apr 15th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. public class CourseSendingContentNotificationHandler : INotificationHandler<SendingContentNotification>
  2. {
  3.     private readonly IContentService _contentService;
  4.     private readonly IPublishedContentQuery _publishedContentQuery;
  5.  
  6.     public CourseSendingContentNotificationHandler(IContentService contentService, IPublishedContentQuery publishedContentQuery)
  7.     {
  8.         _contentService = contentService;
  9.         _publishedContentQuery = publishedContentQuery;
  10.     }
  11.  
  12.     public void Handle(SendingContentNotification notification)
  13.     {
  14.         var content = notification.Content as ContentItemDisplay;
  15.  
  16.         if (content == null)
  17.             return;
  18.  
  19.         if (content.ContentTypeAlias == "course")
  20.         {
  21.             var settingsContent = _contentService.GetById(GetSettingsContentId());
  22.  
  23.             if (settingsContent != null)
  24.             {
  25.                 var masterCoursePublished = _publishedContentQuery.ContentAtRoot().FirstOrDefault(x => x.ContentType.Alias == "settings")?.DescendantOfType("courseMaster");
  26.  
  27.                 if (masterCoursePublished != null)
  28.                 {
  29.                     var masterCourse = _contentService.GetById(masterCoursePublished.Id);
  30.  
  31.                     if (masterCourse != null)
  32.                     {
  33.  
  34.                         foreach (var variant in notification.Content.Variants)
  35.                         {
  36.                             // Check if variant is a 'new variant'
  37.                             // we only want to set the default value when the content item is first created
  38.                             if (variant.State == ContentSavedState.NotCreated)
  39.                             {
  40.                                 // each variant has an IEnumerable of 'Tabs' (property groupings)
  41.                                 // and each of these contain an IEnumerable of `ContentPropertyDisplay` properties
  42.                                 // find the first property with alias 'publishDate'
  43.                                 var blockList = variant.Tabs.SelectMany(f => f.Properties)
  44.                                     .FirstOrDefault(f => f.Alias.InvariantEquals("content"));
  45.                                 if (blockList is not null)
  46.                                 {
  47.                                     var masterBlockList = masterCourse.GetValue<string>("content");
  48.  
  49.                                     BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(masterBlockList);
  50.  
  51.                                     // set default value of the publish date property if it exists
  52.                                     blockList.Value = masterBlockList;
  53.                                 }
  54.                             }
  55.                         }
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.     }
  61.  
  62.     private int GetSettingsContentId()
  63.     {
  64.         return 1071;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment