Beispiel #1
0
        /// <summary>
        /// Use the Modified Timestamp and ID Ordering Strategy to set the "next" URL based on the current Items
        /// of the instance and provided afterTimestamp and afterId.
        /// Also validates that the items are in the correct order, throwing a SerializationException if this is not the case.
        /// </summary>
        /// <param name="feedBaseUrl">The base URL of the feed, used to construct the "next" URL.</param>
        /// <param name="afterTimestamp">The afterTimestamp query parameter value of the current request.</param>
        /// <param name="afterId">The afterId query parameter value of the current request.</param>
        public void SetNextModifiedID(Uri feedBaseUrl, long?afterTimestamp, ComparableSingleValue <long, string>?afterId)
        {
            // If there is at least one item, run validation on items array
            var firstItem = Items.FirstOrDefault();

            if (firstItem != null)
            {
                // Checks that the afterId and afterTimestamp provided are not the
                // first item in the feed (helps detect whether query is correct)
                if (firstItem.Modified == afterTimestamp && firstItem.Id == afterId)
                {
                    throw new SerializationException("First item in the feed must never have same 'modified' and 'id' as afterTimestamp and afterId query parameters. Please check the RPDE specification and ensure you are using the correct query for your ordering strategy.");
                }

                // Check that items are ordered, and deleted items contain no data
                long?currentModified = -1;
                ComparableSingleValue <long, string>?currentId = firstItem.Id;
                foreach (var item in Items)
                {
                    if (item.State == RpdeState.Deleted && item.Data != null)
                    {
                        throw new SerializationException("Deleted items must not contain data.");
                    }

                    if (!item.State.HasValue || !item.Kind.HasValue || !item.Modified.HasValue || !item.Id.HasValue)
                    {
                        throw new SerializationException("All RPDE feed items must include id, modified, state and kind.");
                    }

                    if (item.Modified > currentModified || (item.Modified == currentModified && item.Id > currentId))
                    {
                        currentModified = item.Modified;
                        currentId       = item.Id;
                    }
                    else
                    {
                        throw new SerializationException($"Items must be ordered first by 'modified', then by 'id'. Item with id '{item.Id}' has modified '{item.Modified}' compared with previous item's modified '{currentModified}' and id '{currentId}'. Please check the RPDE specification and ensure you are using the correct query for your ordering strategy.");
                    }
                }
            }

            // Create 'next' URL depending on whether there are items available
            var lastItem = Items.LastOrDefault();

            if (lastItem != null)
            {
                Next = $"{feedBaseUrl}?afterTimestamp={lastItem.Modified}&afterId={lastItem.Id}";
            }
            else if (afterTimestamp.HasValue && afterId.HasValue)
            {
                // Last page, use existing values
                Next = $"{feedBaseUrl}?afterTimestamp={afterTimestamp}&afterId={afterId}";
            }
            else
            {
                // No items, use feed base URL
                Next = $"{feedBaseUrl}";
            }
        }
Beispiel #2
0
 /// <summary>
 /// Creates a new RPDE Page based on the RPDE Items provided, using the Modified Timestamp and ID Ordering Strategy.
 /// Also validates that the items are in the correct order, throwing a SerializationException if this is not the case.
 /// </summary>
 /// <param name="feedBaseUrl">The base URL of the feed, used to construct the "next" URL.</param>
 /// <param name="afterTimestamp">The afterTimestamp query parameter value of the current request.</param>
 /// <param name="afterId">The afterId query parameter value of the current request.</param>
 /// <param name="items">Items to include in the RPDE Page</param>
 public RpdePage(Uri feedBaseUrl, long?afterTimestamp, ComparableSingleValue <long, string>?afterId, List <RpdeItem> items)
 {
     this.Items = items;
     SetNextModifiedID(feedBaseUrl, afterTimestamp, afterId);
 }
 /// <summary>
 /// Creates a new RPDE Page based on the RPDE Items provided, using the Modified Timestamp and ID Ordering Strategy.
 /// Also validates that the items are in the correct order, throwing a SerializationException if this is not the case.
 /// </summary>
 /// <param name="feedBaseUrl">The base URL of the feed, used to construct the "next" URL.</param>
 /// <param name="afterTimestamp">The afterTimestamp query parameter value of the current request.</param>
 /// <param name="afterId">The afterId query parameter value of the current request.</param>
 /// <param name="items">Items to include in the RPDE Page</param>
 public RpdePage(Uri feedBaseUrl, long?afterTimestamp, ComparableSingleValue <long, string>?afterId, List <RpdeItem <T> > items)
 {
     base.Items = items?.Select(x => (RpdeItem)x).ToList();
     SetNextModifiedID(feedBaseUrl, afterTimestamp, afterId);
 }