Class: Motor::QueriesController

Inherits:
ApiBaseController show all
Defined in:
app/controllers/motor/queries_controller.rb

Instance Method Summary collapse

Methods included from CurrentAbility

#current_ability

Methods included from CurrentUserMethod

#current_user

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/motor/queries_controller.rb', line 20

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 do
      Motor::Queries::Persistance.assign_or_create_api_config!(@query)
      @query.save!
    end

    Motor::Configs::WriteToFile.call

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

#destroyObject



46
47
48
49
50
51
52
# File 'app/controllers/motor/queries_controller.rb', line 46

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

  Motor::Configs::WriteToFile.call

  head :ok
end

#indexObject



12
13
14
# File 'app/controllers/motor/queries_controller.rb', line 12

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

#showObject



16
17
18
# File 'app/controllers/motor/queries_controller.rb', line 16

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

#updateObject



37
38
39
40
41
42
43
44
# File 'app/controllers/motor/queries_controller.rb', line 37

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

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