Sha256: 2ea6fc5fe339a8b74071488225eabf0afa556205bddb6c2ea61ceb9a4eca2329

Contents?: true

Size: 1.59 KB

Versions: 21

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Motor
  class QueriesController < ApiBaseController
    wrap_parameters :data, except: %i[include fields]

    load_and_authorize_resource :query, only: %i[index show update destroy]

    before_action :build_query, only: :create
    authorize_resource :query, only: :create

    def index
      render json: { data: Motor::ApiQuery::BuildJson.call(@queries.active, params) }
    end

    def show
      render json: { data: Motor::ApiQuery::BuildJson.call(@query, params) }
    end

    def create
      if Motor::Queries::Persistance.name_already_exists?(@query)
        render json: { errors: [{ source: 'name', detail: 'Name already exists' }] }, status: :unprocessable_entity
      else
        ApplicationRecord.transaction { @query.save! }

        render json: { data: Motor::ApiQuery::BuildJson.call(@query, params) }
      end
    rescue ActiveRecord::RecordNotUnique
      retry
    end

    def update
      Motor::Queries::Persistance.update_from_params!(@query, query_params)

      render json: { data: Motor::ApiQuery::BuildJson.call(@query, params) }
    rescue Motor::Queries::Persistance::NameAlreadyExists
      render json: { errors: [{ source: 'name', detail: 'Name already exists' }] }, status: :unprocessable_entity
    end

    def destroy
      @query.update!(deleted_at: Time.current)

      head :ok
    end

    private

    def build_query
      @query = Motor::Queries::Persistance.build_from_params(query_params)
    end

    def query_params
      params.require(:data).permit(:name, :sql_body, :description, preferences: {}, tags: [])
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
motor-admin-0.1.37 app/controllers/motor/queries_controller.rb
motor-admin-0.1.36 app/controllers/motor/queries_controller.rb
motor-admin-0.1.35 app/controllers/motor/queries_controller.rb
motor-admin-0.1.34 app/controllers/motor/queries_controller.rb
motor-admin-0.1.33 app/controllers/motor/queries_controller.rb
motor-admin-0.1.32 app/controllers/motor/queries_controller.rb
motor-admin-0.1.31 app/controllers/motor/queries_controller.rb
motor-admin-0.1.29 app/controllers/motor/queries_controller.rb
motor-admin-0.1.28 app/controllers/motor/queries_controller.rb
motor-admin-0.1.27 app/controllers/motor/queries_controller.rb
motor-admin-0.1.25 app/controllers/motor/queries_controller.rb
motor-admin-0.1.23 app/controllers/motor/queries_controller.rb
motor-admin-0.1.22 app/controllers/motor/queries_controller.rb
motor-admin-0.1.21 app/controllers/motor/queries_controller.rb
motor-admin-0.1.20 app/controllers/motor/queries_controller.rb
motor-admin-0.1.18 app/controllers/motor/queries_controller.rb
motor-admin-0.1.17 app/controllers/motor/queries_controller.rb
motor-admin-0.1.15 app/controllers/motor/queries_controller.rb
motor-admin-0.1.16 app/controllers/motor/queries_controller.rb
motor-admin-0.1.14 app/controllers/motor/queries_controller.rb