Class: RailsEventStore::Browser::StreamsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_event_store/browser/streams_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/rails_event_store/browser/streams_controller.rb', line 4

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

#showObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/rails_event_store/browser/streams_controller.rb', line 33

def show
  links  = {}
  events = case direction
  when :forward
    event_store
      .read_events_forward(stream_name, start: position, count: count)
      .reverse
  when :backward
    event_store
      .read_events_backward(stream_name, start: position, count: count)
  end

  if prev_event?(events)
    links[:prev]  = prev_page_link(events.first.event_id)
    links[:first] = first_page_link
  end

  if next_event?(events)
    links[:next] = next_page_link(events.last.event_id)
    links[:last] = last_page_link
  end

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