parsley/app/controllers/task_lists_controller.rb

64 lines
1.5 KiB
Ruby
Raw Normal View History

2018-08-27 17:46:33 -05:00
class TaskListsController < ApplicationController
before_action :ensure_valid_user
2021-05-09 23:31:44 -05:00
before_action :set_task_list, only: [:show, :update, :destroy, :add_recipe]
2018-08-27 17:46:33 -05:00
def index
2018-09-06 18:16:13 -05:00
@task_lists = TaskList.for_user(current_user).includes(:task_items).order(created_at: :desc)
2020-08-11 11:05:19 -05:00
render json: TaskListSerializer.for(@task_lists)
2018-08-27 17:46:33 -05:00
end
def show
ensure_owner(@task_list)
2020-08-11 11:05:19 -05:00
render json: TaskListSerializer.for(@task_list)
2018-08-27 17:46:33 -05:00
end
def create
@task_list = TaskList.new(task_list_params)
@task_list.user = current_user
if @task_list.save
2020-08-11 11:05:19 -05:00
render json: TaskListSerializer.for(@task_list), status: :created, location: @task_list
2018-08-27 17:46:33 -05:00
else
render json: @task_list.errors, status: :unprocessable_entity
end
end
def update
ensure_owner(@task_list) do
if @task_list.update(task_list_params)
2020-08-11 11:05:19 -05:00
render json: TaskListSerializer.for(@task_list), status: :ok, location: @task_list
2018-08-27 17:46:33 -05:00
else
render json: @task_list.errors, status: :unprocessable_entity
end
end
end
def destroy
ensure_owner(@task_list) do
@task_list.destroy
head :no_content
end
end
2021-05-09 23:31:44 -05:00
def add_recipe
ensure_owner(@task_list) do
recipe = Recipe.find(params[:recipe_id])
@task_list.add_recipe_ingredients(recipe)
head :no_content
end
end
2018-08-27 17:46:33 -05:00
private
def task_list_params
params.require(:task_list).permit(:name)
end
def set_task_list
@task_list = TaskList.find(params[:id])
end
end