Chronobriq-API/app/controllers/api/v1/tasks_controller.rb
2021-05-10 22:06:12 +02:00

46 lines
1.1 KiB
Ruby

class Api::V1::TasksController < ApplicationController
before_action :set_task, only: %i[show update destroy]
before_action :check_login
def index
render json: TaskSerializer.new(Activity.find(params[:activity_id]).tasks).serializable_hash.to_json
end
def show
render json: TaskSerializer.new(@task).serializable_hash.to_json
end
def create
task = current_user.tasks.build(task_params)
task.activity_id = params[:activity_id]
if task.save
render json: TaskSerializer.new(task).serializable_hash.to_json,
status: :created
else
render json: { errors: activity.errors }, status: :unprocessable_entity
end
end
def update
if @task.update(task_params)
render json: TaskSerializer.new(@task).serializable_hash.to_json, status: :ok
else
render json: @task.errors, status: :unprocessable_entity
end
end
def destroy
@task.destroy
head 204
end
private
def task_params
params.require(:task).permit(:name, :description, :activity_id)
end
def set_task
@task = Activity.find(params[:activity_id]).tasks.find(params[:id])
end
end