Sha256: d941ac7ea16ad73ba2554423727aeeed3d3b4086f21ac110ca08219dcfb80721

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'app/aggregates/<%= aggregate_name %>'

module <%= project_class_name %>
  module Commands
    module <%= aggregate_class_name %>
      module <%= command_class_name %>
        class Command
          attr_reader :aggregate_id, :payload

          def initialize(params)
            @aggregate_id = params.delete(:aggregate_id)
            @payload = params # Select the parameters you want to allow
          end

          def validate
            # Add validation here
          end
        end

        class CommandHandler
          def initialize(repository: <%= project_class_name %>.repository)
            @repository = repository
          end

          def handle(command)
            command.validate

            aggregate = repository.load(Aggregates::<%= aggregate_class_name %>, command.aggregate_id)
            aggregate.<%= command_name %>(command.payload)
            repository.save(aggregate)
          end

          private

          attr_reader :repository
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
event_sourcery_generators-0.2.0 lib/event_sourcery_generators/generators/templates/command/command.rb.tt