Class: ForestLiana::ResourcesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/forest_liana/resources_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_user_from_jwt, #forest_user, #internal_server_error, papertrail?, #serialize_model, #serialize_models

Methods inherited from BaseController

#route_not_found

Instance Method Details

#countObject


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/forest_liana/resources_controller.rb', line 55

def count
  begin
    checker = ForestLiana::PermissionsChecker.new(
      @resource,
      'browseEnabled',
      @rendering_id,
      user_id: forest_user['id'],
      collection_list_parameters: get_collection_list_permission_info(forest_user, request)
    )
    return head :forbidden unless checker.is_authorized?

    getter = ForestLiana::ResourcesGetter.new(@resource, params, forest_user)
    getter.count

    render serializer: nil, json: { count: getter.records_count }

  rescue ForestLiana::Errors::LiveQueryError => error
    render json: { errors: [{ status: 422, detail: error.message }] },
      status: :unprocessable_entity, serializer: nil
  rescue ForestLiana::Errors::ExpectedError => error
    error.display_error
    error_data = ForestAdmin::JSONAPI::Serializer.serialize_errors([{
      status: error.error_code,
      detail: error.message
    }])
    render(serializer: nil, json: error_data, status: error.status)
  rescue => error
    FOREST_LOGGER.error "Records Index Count error: #{error}\n#{format_stacktrace(error)}"
    internal_server_error
  end
end

#createObject


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/forest_liana/resources_controller.rb', line 104

def create
  begin
    checker = ForestLiana::PermissionsChecker.new(@resource, 'addEnabled', @rendering_id, user_id: forest_user['id'])
    return head :forbidden unless checker.is_authorized?

    creator = ForestLiana::ResourceCreator.new(@resource, params)
    creator.perform

    if creator.errors
      render serializer: nil, json: ForestAdmin::JSONAPI::Serializer.serialize_errors(
        creator.errors), status: 400
    elsif creator.record.valid?
      render serializer: nil, json: render_record_jsonapi(creator.record)
    else
      render serializer: nil, json: ForestAdmin::JSONAPI::Serializer.serialize_errors(
        creator.record.errors), status: 400
    end
  rescue => error
    FOREST_LOGGER.error "Record Create error: #{error}\n#{format_stacktrace(error)}"
    internal_server_error
  end
end

#destroyObject


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/forest_liana/resources_controller.rb', line 150

def destroy
  checker = ForestLiana::PermissionsChecker.new(@resource, 'deleteEnabled', @rendering_id, user_id: forest_user['id'])
  return head :forbidden unless checker.is_authorized?

  collection_name = ForestLiana.name_for(@resource)
  scoped_records = ForestLiana::ScopeManager.apply_scopes_on_records(@resource, forest_user, collection_name, params[:timezone])

  unless scoped_records.exists?(params[:id])
    return render serializer: nil, json: { status: 404 }, status: :not_found
  end

  scoped_records.destroy(params[:id])

  head :no_content
rescue => error
  FOREST_LOGGER.error "Record Destroy error: #{error}\n#{format_stacktrace(error)}"
  internal_server_error
end

#destroy_bulkObject


169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/controllers/forest_liana/resources_controller.rb', line 169

def destroy_bulk
  checker = ForestLiana::PermissionsChecker.new(@resource, 'deleteEnabled', @rendering_id, user_id: forest_user['id'])
  return head :forbidden unless checker.is_authorized?

  ids = ForestLiana::ResourcesGetter.get_ids_from_request(params, forest_user)
  @resource.destroy(ids) if ids&.any?

  head :no_content
rescue => error
  FOREST_LOGGER.error "Records Destroy error: #{error}\n#{format_stacktrace(error)}"
  internal_server_error
end

#indexObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/forest_liana/resources_controller.rb', line 16

def index
  begin
    if request.format == 'csv'
      checker = ForestLiana::PermissionsChecker.new(@resource, 'exportEnabled', @rendering_id, user_id: forest_user['id'])
      return head :forbidden unless checker.is_authorized?
    else
      checker = ForestLiana::PermissionsChecker.new(
        @resource,
        'browseEnabled',
        @rendering_id,
        user_id: forest_user['id'],
        collection_list_parameters: get_collection_list_permission_info(forest_user, request)
      )
      return head :forbidden unless checker.is_authorized?
    end

    getter = ForestLiana::ResourcesGetter.new(@resource, params, forest_user)
    getter.perform

    respond_to do |format|
      format.json { render_jsonapi(getter) }
      format.csv { render_csv(getter, @resource) }
    end
  rescue ForestLiana::Errors::LiveQueryError => error
    render json: { errors: [{ status: 422, detail: error.message }] },
      status: :unprocessable_entity, serializer: nil
  rescue ForestLiana::Errors::ExpectedError => error
    error.display_error
    error_data = ForestAdmin::JSONAPI::Serializer.serialize_errors([{
      status: error.error_code,
      detail: error.message
    }])
    render(serializer: nil, json: error_data, status: error.status)
  rescue => error
    FOREST_LOGGER.error "Records Index error: #{error}\n#{format_stacktrace(error)}"
    internal_server_error
  end
end

#showObject


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/forest_liana/resources_controller.rb', line 87

def show
  begin
    checker = ForestLiana::PermissionsChecker.new(@resource, 'readEnabled', @rendering_id, user_id: forest_user['id'])
    return head :forbidden unless checker.is_authorized?

    getter = ForestLiana::ResourceGetter.new(@resource, params, forest_user)
    getter.perform

    render serializer: nil, json: render_record_jsonapi(getter.record)
  rescue ActiveRecord::RecordNotFound
    render serializer: nil, json: { status: 404 }, status: :not_found
  rescue => error
    FOREST_LOGGER.error "Record Show error: #{error}\n#{format_stacktrace(error)}"
    internal_server_error
  end
end

#updateObject


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/forest_liana/resources_controller.rb', line 127

def update
  begin
    checker = ForestLiana::PermissionsChecker.new(@resource, 'editEnabled', @rendering_id, user_id: forest_user['id'])
    return head :forbidden unless checker.is_authorized?

    updater = ForestLiana::ResourceUpdater.new(@resource, params, forest_user)
    updater.perform

    if updater.errors
      render serializer: nil, json: ForestAdmin::JSONAPI::Serializer.serialize_errors(
        updater.errors), status: 400
    elsif updater.record.valid?
      render serializer: nil, json: render_record_jsonapi(updater.record)
    else
      render serializer: nil, json: ForestAdmin::JSONAPI::Serializer.serialize_errors(
        updater.record.errors), status: 400
    end
  rescue => error
    FOREST_LOGGER.error "Record Update error: #{error}\n#{format_stacktrace(error)}"
    internal_server_error
  end
end