Sha256: e2971560d75c79b8381209be340aa53b0d9265bebf9990e36736acaa313ee641

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require_relative '../controller'
require_relative '../../serializers/test_session'

module Inferno
  module Web
    module Controllers
      module TestSessions
        class Create < Controller
          PARAMS = [:test_suite_id, :suite_options].freeze

          def handle(req, res)
            params = req.params.to_h
            if req.body.string.present? && req.env['CONTENT_TYPE']&.include?('application/json')
              params.merge!(JSON.parse(req.body.string).symbolize_keys)
            end

            session = repo.create(create_params(params))

            repo.apply_preset(session, params[:preset_id]) if params[:preset_id].present?

            res.body = serialize(session)
          rescue Sequel::ValidationFailed, Sequel::ForeignKeyConstraintViolation => e
            halt 422, { errors: e.message }.to_json
          rescue StandardError => e
            Application['logger'].error(e.full_message)
            halt 500, { errors: e.message }.to_json
          end

          def create_params(params)
            params.slice(*PARAMS)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inferno_core-0.4.28 lib/inferno/apps/web/controllers/test_sessions/create.rb