Пример #1
0
        public static FeedElement ToStreamEventBackwardFeed(ClientMessage.ReadStreamEventsBackwardCompleted msg, Uri requestedUrl, EmbedLevel embedContent, bool headOfStream)
        {
            Ensure.NotNull(msg, "msg");

            string escapedStreamId = Uri.EscapeDataString(msg.EventStreamId);
            var self = HostName.Combine(requestedUrl, "/streams/{0}", escapedStreamId);
            var feed = new FeedElement();
            feed.SetTitle(string.Format("Event stream '{0}'", msg.EventStreamId));
            feed.StreamId = msg.EventStreamId;
            feed.SetId(self);
            feed.SetUpdated(msg.Events.Length > 0 ? msg.Events[0].Event.TimeStamp : DateTime.MinValue.ToUniversalTime());
            feed.SetAuthor(AtomSpecs.Author);
            feed.SetHeadOfStream(headOfStream); //TODO AN: remove this ?
            feed.SetSelfUrl(self);

            var prevEventNumber = Math.Min(msg.FromEventNumber, msg.LastEventNumber) + 1;
            var nextEventNumber = msg.FromEventNumber - msg.MaxCount;

            feed.AddLink("self", self);
            feed.AddLink("first", HostName.Combine(requestedUrl, "/streams/{0}/head/backward/{1}", escapedStreamId, msg.MaxCount));
            if (!msg.IsEndOfStream)
            {
                if (nextEventNumber < 0) throw new Exception(string.Format("nextEventNumber is negative: {0} while IsEndOfStream", nextEventNumber));
                feed.AddLink("last", HostName.Combine(requestedUrl, "/streams/{0}/{1}/forward/{2}", escapedStreamId, 0, msg.MaxCount));
                feed.AddLink("next", HostName.Combine(requestedUrl, "/streams/{0}/{1}/backward/{2}", escapedStreamId, nextEventNumber, msg.MaxCount));
            }
            feed.AddLink("previous", HostName.Combine(requestedUrl, "/streams/{0}/{1}/forward/{2}", escapedStreamId, prevEventNumber, msg.MaxCount));
            feed.AddLink("metadata", HostName.Combine(requestedUrl, "/streams/{0}/metadata", escapedStreamId));
            for (int i = 0; i < msg.Events.Length; ++i)
            {
                feed.AddEntry(ToEntry(msg.Events[i], requestedUrl, embedContent));
            }

            return feed;
        }
Пример #2
0
        public static FeedElement ToReadStreamFeed(
            ClientMessage.ReadStreamEventsBackwardCompleted msg, string userHostName, EmbedLevel embedContent,
            bool headOfStream)
        {
            Ensure.NotNull(msg, "msg");

            string escapedStreamId = Uri.EscapeDataString(msg.EventStreamId);
            var self = HostName.Combine(userHostName, "/streams/{0}", escapedStreamId);
            var feed = new FeedElement();
            feed.SetTitle(string.Format("Event stream '{0}'", msg.EventStreamId));
            feed.SetId(self);
            feed.SetUpdated(msg.Events.Length > 0 ? msg.Events[0].Event.TimeStamp : DateTime.MinValue.ToUniversalTime());
            feed.SetAuthor(AtomSpecs.Author);
            feed.SetHeadOfStream(headOfStream);
            if (headOfStream)
                //NOTE: etag workaround - to be fixed with better http handling model
                feed.SetETag(msg.LastEventNumber.ToString(CultureInfo.InvariantCulture) + ";" + "application/json".GetHashCode());
            feed.SetSelfUrl(self);

            feed.AddLink("self", self);
            feed.AddLink("first", HostName.Combine(userHostName, "/streams/{0}", escapedStreamId)); // TODO AN: should account for msg.MaxCount
            feed.AddLink("last", HostName.Combine(userHostName, "/streams/{0}/range/{1}/{2}", escapedStreamId, msg.MaxCount - 1, msg.MaxCount));
            feed.AddLink("previous", HostName.Combine(userHostName, 
                                                      "/streams/{0}/range/{1}/{2}", 
                                                      escapedStreamId, 
                                                      Math.Min(msg.FromEventNumber, msg.LastEventNumber) + msg.MaxCount, 
                                                      msg.MaxCount));

            if (msg.FromEventNumber - msg.MaxCount >= 0)
            {
                feed.AddLink("next", HostName.Combine(userHostName,
                                                      "/streams/{0}/range/{1}/{2}",
                                                      escapedStreamId,
                                                      msg.FromEventNumber - msg.MaxCount,
                                                      msg.MaxCount));
            }

            for (int i = 0; i < msg.Events.Length; ++i)
            {
                feed.AddEntry(ToEntry(msg.Events[i], userHostName, embedContent));
            }

            return feed;
        }