예제 #1
0
        public async Task <ActionResult> UserAdd(string username, string password, Array City)
        {
            HttpClient httpClient = new HttpClient();
            var        client     = new ContentfulManagementClient(httpClient, "CFPAT-0188062dc4d4c5bdc61c744ffae9f600e56164b944e1a6c8d422ad1a0257525d", "bda2gc49gm0w");

            var entry = new Entry <dynamic>();

            entry.SystemProperties    = new SystemProperties();
            entry.SystemProperties.Id = Guid.NewGuid().ToString();
            entry.Fields = new
            {
                username = new Dictionary <string, string>()
                {
                    { "en-US", username }
                },
                password = new Dictionary <string, string>()
                {
                    { "en-US", password }
                },
                allCity = new Dictionary <string, Array>()
                {
                    { "en-US", City }
                }
            };

            await client.CreateOrUpdateEntry(entry, contentTypeId : "users");

            await client.PublishEntry(entry.SystemProperties.Id, 1);


            return(View("Index"));
        }
        /// <summary>
        /// Publishes the content items referenced by the project.
        /// </summary>
        /// <param name="slug">The slug identifying the project</param>
        /// <returns></returns>
        public async Task <List <string> > PublishProject(string slug)
        {
            _logger.LogTrace($"ContentfulContentRepository.PublishProject({slug})");

            if (string.IsNullOrWhiteSpace(slug))
            {
                throw new ArgumentException("Missing required parameter", nameof(slug));
            }

            try
            {
                var cful             = new ContentfulClient(_httpClient, _options);
                var managementClient = new ContentfulManagementClient(_httpClient, _options);

                var facetQuery = new QueryBuilder <Project>()
                                 .ContentTypeIs("project")
                                 .FieldEquals("fields.slug", slug)
                                 .Include(8);
                _logger.LogInformation($"executing CMS call with query: {facetQuery.Build()}");

                // Retrieve the entire content tree by starting at the project and pulling includes
                // an arbitrary depth of 8
                var entrySet = await cful.GetEntries(facetQuery);

                // todo: determine if our process will already have the actual project published or not.
                // var projectId = entrySet.Items.FirstOrDefault().Sys.Id;
                var includedEntryIds = entrySet.IncludedEntries.Select(x => x.SystemProperties.Id);
                // todo: publish assets, too.
                // var includedAssetIds = entrySet.IncludedAssets.Select(x => x.SystemProperties.Id);

                foreach (var entry in entrySet.IncludedEntries)
                {
                    var id = entry.SystemProperties.Id;
                    // Retrieve the item from mgmt API. Version is not included from delivery API so we get it again.
                    var mgmtEntry = await managementClient.GetEntry(id);

                    var latestVersion = mgmtEntry.SystemProperties.Version.GetValueOrDefault();
                    var result        = await managementClient.PublishEntry(id, latestVersion);
                }

                return(null);
            }
            catch (Contentful.Core.Errors.ContentfulException ex)
            {
                var msg = "Error retrieving content: " + ex;
                _logger.LogError(msg);
                throw new ContentException(msg, ex);
            }
            catch (Exception ex)
            {
                var msg = "Unable to retrieve content: " + ex;
                _logger.LogError(msg);
                throw new ProcessException(msg, ex);
            }
        }
        /// <summary>
        /// Publishes the content item with the provided ID
        /// </summary>
        /// <param name="id">The id identifying the item</param>
        /// <returns></returns>
        public async Task <int> PublishItem(string id)
        {
            _logger.LogTrace($"PublishItem({id})");

            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentException("Missing required parameter", nameof(id));
            }

            var managementClient = new ContentfulManagementClient(_httpClient, _options);
            var mgmtEntry        = await GetItemFromMgmtAPI(id);

            try
            {
                var latestVersion    = mgmtEntry.SystemProperties.Version.GetValueOrDefault();
                var publishedVersion = mgmtEntry.SystemProperties.PublishedVersion.GetValueOrDefault();

                // note: this is intended to reduce re-pubishing already published items but
                // a bug in contentful ALWAYS increments the latest version, no matter what.
                // Leaving for future, but should probably ask contentful about it.
                if (publishedVersion < latestVersion)
                {
                    var result = await managementClient.PublishEntry(id, latestVersion);

                    return(result.SystemProperties.PublishedVersion.GetValueOrDefault());
                }

                return(publishedVersion);
            }
            catch (Contentful.Core.Errors.ContentfulException ex)
            {
                var msg = "Error publishing entry: " + ex;
                _logger.LogError(msg);
                throw new ContentException(msg, ex);
            }
            catch (Exception ex)
            {
                var msg = "Unable to publishing entry: " + ex;
                _logger.LogError(msg);
                throw new ProcessException(msg, ex);
            }
        }
 public Entry <dynamic> PublishContent(string entryID, int lastVersion)
 {
     return(_apiClient.PublishEntry(entryID, lastVersion, Space).Result);
 }