Class: EntryActivitiesController

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

Overview

EntryProduceController

Constant Summary

Constant Summary

Constants inherited from ApplicationController

ApplicationController::SupportedClients

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#absolutize_path, #clean_summary

Methods included from Authorization

included, ri?, #role_included?, #set_promotion_for_public_request

Instance Method Details

- (Hash) create

Adds an activity for the specified entry and updates the entry.

URL

/entries/:entry_id/activity [POST]

Response Codes
201 Created

New entry_activity created for entry

# Example response
 {
     "row_id": "65.entry_activity.13611",
     "name": "Winter Biathlon",
     "entry_id": 55667,
     "updated_at": "2012-06-25T17:58:47Z",
     "activity_id": 144,
     "id": 13611,
     "exercise": 45,
     "entry": {
         "row_id": "65.entry.55667",
         "group": "201203.March 2012",
         "is_logged": true,
         "summed_exercise_minutes": 1005,
         "updated_at": "2012-06-25T17:58:47Z",
         "id": 55667,
         "user_id": 65,
         "capped_exercise_minutes": 90,
         "logged_on": "2012-03-06"
     }
 }
422 Unprocessable Entity

Failed to create the activity

400 Bad Request

Failed request, client failed to provide valid parameters

Examples:

#POST /entries/4258/activity
{
  entry_activities: {
     activity_id: 1758, exercise: 0.5
  }
}

Parameters:

  • activity (Hash)

    hash containing the activity_id and exercise

Returns:

  • (Hash)


88
89
90
91
92
93
94
95
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
# File 'app/controllers/entry_activities_controller.rb', line 88

def create
  # get the entry
  e = get_entry
  # get the activity information
  act = params[:entry_activity]
  id = act[:activity_id].to_i
  exercise = act[:exercise].to_i   

  # attempt to modify the entry
  Entry.transaction do
    # add the new activity
    ea = e.entry_activities.build(:activity_id=>id,:exercise=>exercise,:entry_id=>e.id,:trip_id=>@user.trip.id)

    head :unprocessable_entity and return if !ea.valid?
    ea.save!
    
    current_exercise = e.exercise.nil? ? exercise : (exercise + e.exercise)
    capped_exercise = (current_exercise > @user.promotion.detail.single_day_exercise_limit) ? @user.promotion.detail.single_day_exercise_limit : current_exercise

    e.exercise = current_exercise
    e.capped_exercise = capped_exercise
    e.is_logged = true
    e.save!
    
    # return our modified json, if good
    item = parse_item(ea)
    render :json => item, :status => :created and return
  end
  
  head :unprocessable_entity    
rescue
  head :bad_request
end

- (Object) destroy

Deletes the specified activities from the entry and updates the entry.

URL

/entries/:entry_id/activities/:id [DELETE]

Response Codes
200 OK

Produce removed

400 Bad Request

Failed request, client failed to provide valid parameters



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'app/controllers/entry_activities_controller.rb', line 202

def destroy
  # get the entry_activities and entry
  ea = get_entry_activities
  e = get_entry
  # attempt to modify the entry
  Entry.transaction do
    
    # remove the activities
    ea.destroy
    
    # this should really already be set, but we're setting it here to ensure that the entry is resaved trigger our after_update callbacks
    e.reload # so entry_activitiess is up to date
    e.is_logged = true

    # Reset the entry if we've deleted all of the activities
    if e.entry_activities.empty?
      e.exercise = 0
      e.capped_exercise = 0
      e.is_logged = false
    end
    
    # save changes
    e.save!
    
    # return if good
    head :ok and return
  end
  
  head :not_found
rescue
  head :bad_request
end

- (Hash) update

Changes activities for the specified entry and updates the entry.

URL

/entries/:entry_id/activities/:id [POST]

Response Codes
200 OK

Updated entry_activities

# Example response
 {
     "row_id": "65.entry_activity.13611",
     "name": "Winter Biathlon",
     "entry_id": 55667,
     "updated_at": "2012-06-25T17:58:48Z",
    "activity_id": 144,
     "id": 13611,
     "exercise": 60,
     "entry": {
         "row_id": "65.entry.55667",
         "group": "201203.March 2012",
         "is_logged": true,
         "summed_exercise_minutes": 1005,
         "updated_at": "2012-06-25T17:58:47Z",
         "id": 55667,
         "user_id": 65,
         "capped_exercise_minutes": 90,
         "logged_on": "2012-03-06"
     }
 }
422 Unprocessable Entity

Failed to create the activity

400 Bad Request

Failed request, client failed to provide valid parameters

Examples:

#POST /entries/4258/activities/1802
{
  entry_activities: {
     activity_id: 1758, servings: 1
  }
}

Parameters:

  • entry_activities (Hash)

    hash containing the activity_id and servings

Returns:

  • (Hash)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/entry_activities_controller.rb', line 162

def update
  # get the entry activities
  ea = get_entry_activities

  # get the entry
  e = get_entry

  # get the activities information
  act = params[:entry_activity]
  id = act[:activity_id].to_i
  exercise = act[:exercise].to_i

  # attempt to modify the entry
  Entry.transaction do
    
    # update to reflect new changes
    ea.update_attributes(:activity_id => id, :exercise => exercise)

    # this should really already be set, but we're setting it here to ensure that the entry is resaved trigger our after_update callbacks
    e.is_logged = true

    # save
    e.save!

    # return our modified json, if good
    item = parse_item(ea)
    render :json => item, :status => :ok and return
  end

  head :unprocessable_entity
rescue
  head :bad_request
end