⌚ 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.

app.rb 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. # This contains the Sinatra App
  3. require_relative './hours.rb'
  4. require_relative '../config/event_sourcery.rb'
  5. require 'sinatra'
  6. require 'jsonapi/renderer'
  7. require 'jsonapi/serializable'
  8. UnprocessableEntity = Class.new(StandardError)
  9. BadRequest = Class.new(StandardError)
  10. set :public_folder, 'public'
  11. # Ensure our error handlers are triggered in development
  12. set :show_exceptions, :after_handler
  13. configure :development do
  14. require 'better_errors'
  15. use BetterErrors::Middleware
  16. BetterErrors.application_root = __dir__
  17. end
  18. error UnprocessableEntity do |error|
  19. body "Unprocessable Entity: #{error.message}"
  20. status 422
  21. end
  22. error BadRequest do |error|
  23. body "Bad Request: #{error.message}"
  24. status 400
  25. end
  26. def json_params
  27. # Coerce this into a symbolised Hash so Sintra data structures
  28. # don't leak into the command layer.
  29. Hash[
  30. params.merge(
  31. JSON.parse(request.body.read)
  32. ).map { |k, v| [k.to_sym, v] }
  33. ]
  34. end
  35. get '/nodes' do
  36. render = JSONAPI::Serializable::Renderer.new
  37. body render.render(
  38. Hours::Projections::Nodes::Query.handle,
  39. class: { "Hours::Models::Node": Hours::Serializers::Node }
  40. ).to_json
  41. status 200
  42. end
  43. post '/nodes/:node_id' do
  44. command = Hours::Commands::Node::Add::Command.build(json_params)
  45. Hours::Commands::Node::Add::CommandHandler.new.handle(command)
  46. status 201
  47. end
  48. require_relative '../app/serializers/node.rb'
  49. require_relative '../app/models/node.rb'