예제 #1
0
 private void RouteContentCall(Operation op, string storeId, LocalizedPageEntity entry)
 {
     if (op == Operation.Undefined) // unpublish
     {
         UnpublishContentPage(storeId, entry);
     }
     else if (op == Operation.Publish) // publish
     {
         PublishContentPage(storeId, entry);
     }
 }
예제 #2
0
        public async Task <IHttpActionResult> WebhookHandlerAsync(string storeId)
        {
            // TODO: add check if user has store permissions
            var json = await Request.Content.ReadAsStringAsync();

            var source = JsonConvert.DeserializeObject <JObject>(json);

            var entry = GetEntry <Entry <Dictionary <string, Dictionary <string, object> > > >(source);

            var type = GetEntryType(entry.SystemProperties.ContentType.SystemProperties.Id);

            if (type == EntryType.Unknown)
            {
                return(Ok("Only entities named \"page*\" are supported"));
            }

            // now check if store actually exists, this is more expensive than checking page type, so do it later
            var store = _storeService.GetById(storeId);

            if (store == null)
            {
                return(NotFound());
            }

            // X-Contentful-Topic
            var headers = this.Request.Headers;

            var operations = headers.GetValues("X-Contentful-Topic");
            var op         = operations.FirstOrDefault();
            var action     = GetAction(op);

            // TODO: get language from the response, add support for multiple languages
            if (type == EntryType.Page) // create/update/delete CMS pages
            {
                // go through all the languages
                foreach (var lang in entry.Fields["title"].Keys)
                {
                    var page = new LocalizedPageEntity(entry.SystemProperties.Id, lang, entry.Fields);
                    RouteContentCall(action, storeId, page);
                }

                return(Ok(string.Format("Page updated successfully \"{0}\"", entry.SystemProperties.Id)));
            }
            if (type == EntryType.Product) // create/update/delete products
            {
                var product = new ProductEntity(entry.SystemProperties.Id, entry.Fields);
                RouteProductCall(action, product);
                return(Ok(string.Format("Product updated successfully \"{0}\"", entry.SystemProperties.Id)));
            }

            return(Ok(String.Format("No hanldder for type \"{0}\" found", entry.SystemProperties.ContentType.SystemProperties.Id)));
        }
예제 #3
0
        private void PublishContentPage(string storeId, LocalizedPageEntity entry)
        {
            var storageProvider = _contentStorageProviderFactory($"Pages/{storeId}");

            var serializer = new SerializerBuilder().Build();
            var yaml       = serializer.Serialize(entry.Properties);

            var contents = new StringBuilder();

            contents.AppendLine("---");
            contents.AppendLine(yaml);
            contents.AppendLine("---");
            contents.AppendLine(entry.Content);
            using (var stream = storageProvider.OpenWrite(String.Format("{0}.md", entry.Id)))
            {
                using (var memStream = new MemoryStream(Encoding.UTF8.GetB‌​ytes(contents.ToString())))
                {
                    memStream.CopyTo(stream);
                }
            }
        }
예제 #4
0
        private void UnpublishContentPage(string storeId, LocalizedPageEntity entry)
        {
            var storageProvider = _contentStorageProviderFactory($"Pages/{storeId}");

            storageProvider.Remove(new[] { String.Format("{0}.md", entry.Id) });
        }