Class: Api::V1::ApplicationsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/applications_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
86
87
88
89
90
91
92
93
94
# File 'app/controllers/api/v1/applications_controller.rb', line 58

def create
  begin
    ActiveRecord::Base.transaction do
      name = params[:name].strip


      application = Application.create(
        description: params[:name].strip,
        internal_identifier: (params[:internal_identifier].present? ? params[:internal_identifier].strip : Application.generate_unique_iid(name))
      )

      current_user.apps << application
      current_user.save

      # associate application to dba_org of current user
      application.add_party_with_role(current_user.party.dba_organization, RoleType.iid('dba_org'))

      # apply recipes
      if params[:recipe_klass].present?
        params[:recipe_klass].constantize.new(application, current_user).execute((params[:recipe_params] || {}))
      end

      render :json => {success: true, application: application.to_data_hash}
    end
  rescue ActiveRecord::RecordInvalid => invalid

    render :json => {success: false, message: invalid.record.errors.messages}
  rescue StandardError => ex
    Rails.logger.error(ex.message)
    Rails.logger.error(ex.backtrace.join("\n"))

    # email notification
    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render :json => {success: false, message: ex.message}
  end
end

#destroyObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/api/v1/applications_controller.rb', line 123

def destroy
  application = Application.find(params[:id])
  success = false

  begin
    ActiveRecord::Base.transaction do
      application.destroy

      success = true
    end
  rescue => ex
    Rails.logger.error(ex.message)
    Rails.logger.error(ex.backtrace.join("\n"))

    # email notification
    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier
  end

  render :json => {success: success}
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
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
# File 'app/controllers/api/v1/applications_controller.rb', line 5

def index
  # scope by dba_organization
  if params[:dba_organization_id].present?
    applications = Application.scope_by_dba(params[:dba_organization_id])
  elsif params[:user_id].present?
    applications = User.find(params[:user_id]).applications
  else
    applications = Application.scope_by_dba(current_user.party.dba_organization)
  end

  # scope by types if passed
  if params[:types].present?
    types = params[:types].split(',')

    # if types length is 1 then we only want tools or apps
    if types.length == 1
      if types.first == 'tool'
        applications = applications.tools
      elsif types.first == 'app'
        applications = applications.apps
      end
    end
  end

  respond_to do |format|
    format.tree do
      applications = applications.map do |application|
        hash = application.to_data_hash
        hash.delete(:id)
        hash[:leaf] = true

        hash
      end

      render :json => {success: true, applications: applications}
    end
    format.json do
      render :json => {
        success: true,
        applications: applications.each do |application|
          application.to_data_hash
        end
      }
    end
  end
end

#installObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/api/v1/applications_controller.rb', line 144

def install
  begin
    ActiveRecord::Base.connection.transaction do
      if params[:user_id].present?
        user = User.find(params[:user_id])
      end

      # remove current applications
      application_ids = user.applications.pluck(:id)
      application_ids.each do |app_id|
        user.applications.delete(Application.find(app_id))
      end

      params[:application_iids].split(',').each do |application_iid|
        user.applications << Application.iid(application_iid)
      end

      render json: {success: true}
    end
  rescue ActiveRecord::RecordInvalid => invalid
    Rails.logger.error invalid.record.errors

    render :json => {:success => false, :message => invalid.record.errors}
  rescue StandardError => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render json: {success: false, message: 'Error updating installed Applications'}
  end
end

#showObject



52
53
54
55
56
# File 'app/controllers/api/v1/applications_controller.rb', line 52

def show
  application = Application.find(params[:id])

  render :json => {success: true, application: application.to_data_hash}
end

#updateObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/api/v1/applications_controller.rb', line 96

def update
  begin
    ActiveRecord::Base.connection.transaction do

      application = Application.find(params[:id])

      application.description = params[:description].strip

      application.save!

      render json: {success: true, application: application.to_data_hash}

    end
  rescue ActiveRecord::RecordInvalid => invalid
    Rails.logger.error invalid.record.errors

    render :json => {:success => false, :message => invalid.record.errors}
  rescue StandardError => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render json: {success: false, message: 'Error updating Application'}
  end
end