⌚ OpenOpeningstijden main server application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

add.rb 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # frozen_string_literal: true
  2. module Hours
  3. module Commands
  4. module Node
  5. module Add
  6. # A command handler and commands that can be issued against the system.
  7. # These form an interface between the web API and the domain model in
  8. # the aggregate.
  9. class Command
  10. attr_reader :payload, :aggregate_id
  11. def self.build(**args)
  12. new(**args).tap(&:validate)
  13. end
  14. def initialize(params)
  15. @payload = params.slice(
  16. :node_id,
  17. :lat,
  18. :lon,
  19. :author_email,
  20. :contact_details
  21. )
  22. @aggregate_id = payload.delete(:node_id)
  23. end
  24. def validate
  25. raise BadRequest, 'node_id is blank' unless aggregate_id
  26. raise BadRequest, 'lat is blank' unless payload[:lat]
  27. raise BadRequest, 'lon is blank' unless payload[:lon]
  28. end
  29. end
  30. # A command handler runs the command.
  31. class CommandHandler
  32. def initialize(repository: Hours.repository)
  33. @repository = repository
  34. end
  35. # Handle loads the aggregate state from the store using the
  36. # repository, defers to the aggregate to execute the command, and
  37. # saves off any newly raised events to the store.
  38. def handle(command)
  39. aggregate = repository.load(Aggregates::Node, command.aggregate_id)
  40. aggregate.add(command.payload)
  41. repository.save(aggregate)
  42. end
  43. private
  44. attr_reader :repository
  45. end
  46. end
  47. end
  48. end
  49. end