Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browse global stream on start #298

Merged
merged 7 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module RailsEventStore
module Browser

class EventsController < ApplicationController
def show
event = event_store.read_event(event_id)
Expand All @@ -12,8 +11,6 @@ def show
def event_id
params.fetch(:id)
end

end

end
end
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
module RailsEventStore
module Browser
class StreamsController < ApplicationController
def index
links = {}
streams = case direction
when :forward
items = event_store.get_all_streams
items = items.drop_while { |s| !s.name.eql?(position) }.drop(1) unless position.equal?(:head)
items.take(count).reverse
when :backward
items = event_store.get_all_streams.reverse
items = items.drop_while { |s| !s.name.eql?(position) }.drop(1) unless position.equal?(:head)
items.take(count)
end

if next_stream?(streams)
links[:next] = streams_next_page_link(streams.last.name)
links[:last] = streams_last_page_link
end

if prev_stream?(streams)
links[:prev] = streams_prev_page_link(streams.first.name)
links[:first] = streams_first_page_link
end

render json: {
data: streams.map { |s| serialize_stream(s) },
links: links
}, content_type: 'application/vnd.api+json'
end

def show
links = {}
events = case direction
Expand All @@ -53,46 +24,13 @@ def show
end

render json: {
data: events.map { |e| serialize_event(e) },
data: events.map { |e| JsonApiEvent.new(e).to_h },
links: links
}, content_type: 'application/vnd.api+json'
end

private

def next_stream?(streams)
return if streams.empty?
event_store.get_all_streams
.reverse
.drop_while { |s| !s.name.eql?(streams.last.name) }
.drop(1)
.present?
end

def prev_stream?(streams)
return if streams.empty?
event_store.get_all_streams
.drop_while { |s| !s.name.eql?(streams.first.name) }
.drop(1)
.present?
end

def streams_next_page_link(stream_name)
streams_url(position: stream_name, direction: :backward, count: count)
end

def streams_prev_page_link(stream_name)
streams_url(position: stream_name, direction: :forward, count: count)
end

def streams_first_page_link
streams_url(position: :head, direction: :backward, count: count)
end

def streams_last_page_link
streams_url(position: :head, direction: :forward, count: count)
end

def next_event?(events)
return if events.empty?
event_store.read_events_backward(stream_name, start: events.last.event_id).present?
Expand Down Expand Up @@ -144,25 +82,6 @@ def position
def stream_name
params.fetch(:id)
end

def serialize_stream(stream)
{
id: stream.name,
type: "streams"
}
end

def serialize_event(event)
{
id: event.event_id,
type: "events",
attributes: {
event_type: event.class.to_s,
data: event.data,
metadata: event.metadata.to_h
}
}
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module RailsEventStore
module Browser

class JsonApiEvent
def initialize(event)
@event = event
Expand All @@ -19,9 +18,7 @@ def to_h
end

private

attr_reader :event
end

end
end
4 changes: 1 addition & 3 deletions rails_event_store-browser/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@
format: false,
constraints: { id: %r{[^\/]+} }

get '/streams(/:position/:direction/:count)',
to: "streams#index",
as: :streams
resources :streams, only: [:index]
end
Loading