From 30d818ac875bc377ae79369ece0e31726cb9da4a Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sat, 8 Mar 2014 15:04:27 -0500 Subject: [PATCH] Draft of the task list api controller --- .../api/v1/task_lists_controller.rb | 28 +++++++++++++++++++ .../api/v1/task_lists/show.json.jbuilder | 15 ++++++++++ config/routes.rb | 2 ++ 3 files changed, 45 insertions(+) create mode 100644 app/controllers/api/v1/task_lists_controller.rb create mode 100644 app/views/api/v1/task_lists/show.json.jbuilder diff --git a/app/controllers/api/v1/task_lists_controller.rb b/app/controllers/api/v1/task_lists_controller.rb new file mode 100644 index 0000000..9d4ff43 --- /dev/null +++ b/app/controllers/api/v1/task_lists_controller.rb @@ -0,0 +1,28 @@ +class Api::V1::TaskListsController < Api::V1::BaseController + CANNOT_MANAGE = "You do not have permission to manage this task list." + NOT_FOUND = "The task list could not be found." + + before_filter :get_task_list + before_filter :check_task_list_permission, except: :show + + def show + end + + def edit + #@task_list.update_attributes(params) + end + + private + def get_task_list + @task_list = TaskList.find(params[:id]) + if @task_list.nil? + render json: { errors: [NOT_FOUND] }, status: 404 and return + end + end + + def check_task_list_permission + if cannot? :manage, Bike and @task_list.item != current_user.bike + render json: { errors: [CANNOT_MANAGE]}, status: 403 and return + end + end +end diff --git a/app/views/api/v1/task_lists/show.json.jbuilder b/app/views/api/v1/task_lists/show.json.jbuilder new file mode 100644 index 0000000..f11a6da --- /dev/null +++ b/app/views/api/v1/task_lists/show.json.jbuilder @@ -0,0 +1,15 @@ +json.task_lists [@task_list] do |tl| + json.array! tl + json.links do + json.bike do + json.href api_bike_path(tl.item) + json.id tl.item_id + end + json.tasks tl.tasks do |task| + json.id task.id + json.done task.done + json.notes task.notes + json.task task.task + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 918d81c..c798541 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,6 +20,8 @@ Velocipede::Application.routes.draw do get 'bikes/:id' => "bikes#show", as: "api_bike" post 'bikes/create' => "bikes#create", as: "api_create_bike" + + get 'task_lists/:id' => "task_lists#show", as: "api_task_list" end end