Mirosław Pragłowski bio photo

Mirosław Pragłowski

Email Twitter Github
Facebook LinkedIn Instagram

require ‘command’ require ‘command/bus’

module AccessControl class Scan include Command

attribute :barcode,  String
attribute :terminal, String
attribute :event_id, Integer

validates_presence_of :barcode, :terminal, :event_id   end

class ScanService def initialize(event_store: Rails.configuration.event_store, acid: ActiveRecord::Base) @event_store = event_store @acid = acid end

def register(cmd)
  with(cmd.barcode, cmd.event_id) do |entry|
    entry.register(cmd.barcode, cmd.event_id)
  end
end

def scan(cmd)
  with(cmd.barcode, cmd.event_id) do |entry|
    entry.scan(command.terminal)
  end
end

private
attr_reader :event_store, :acid

def with(barcode, event_id)
  acid.transaction do
    entry = Entry.new
    stream = "AccessControl::Entry$#{barcode}-#{event_id}"
    entry.load(stream, event_store: event_store)
    yield entry
    entry.store
  end
end   end end