Class: Voluntary::Api::V1::TasksController

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

Instance Method Summary collapse

Methods inherited from BaseController

#require_api_key

Instance Method Details

#createObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/voluntary/api/v1/tasks_controller.rb', line 27

def create
  tasks = current_user.stories.find(params[:story_id]).tasks
  json = []
  
  params[:tasks].each do |attributes|
    task = tasks.create(attributes)
    
    json << if task.valid?
      task.to_json
    else  
      hash = { errors: {}}
      task.errors.each {|key, value| hash[:errors][key] = value }
      hash
    end
  end
  
  respond_to do |format|
    format.json { render json: json }
  end
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/voluntary/api/v1/tasks_controller.rb', line 9

def index
  params[:page] = nil if params[:page] == ''
  params[:per_page] = nil if params[:per_page] == ''
  collection = Task.where(story_id: params[:story_id])
  collection = collection.where(state: params[:state]) if params[:state].present?
  collection = collection.paginate(per_page: params[:per_page] || 50, page: params[:page] || 1)
  
  respond_to do |format|
    format.json {
      render json: {
        current_page: collection.current_page, per_page: collection.per_page, 
        total_entries: collection.total_entries, total_pages: collection.total_pages,
        entries: collection.map(&:to_json),
      }.to_json
    }
  end
end