API de comptabilité horaire.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tasks_controller.rb 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class Api::V1::TasksController < ApplicationController
  2. before_action :set_task, only: %i[show update destroy]
  3. before_action :check_login
  4. before_action :check_owner, only: %i[update destroy]
  5. def index
  6. if !params[:activity_id]
  7. render json: TaskSerializer.new(Task.all).serializable_hash.to_json
  8. else
  9. render json: TaskSerializer.new(Activity.find(params[:activity_id]).tasks).serializable_hash.to_json
  10. end
  11. end
  12. def show
  13. render json: TaskSerializer.new(@task).serializable_hash.to_json
  14. end
  15. def create
  16. task = current_user.tasks.build(task_params)
  17. task.activity_id = params[:activity_id]
  18. if task.save
  19. render json: TaskSerializer.new(task).serializable_hash.to_json,
  20. status: :created
  21. else
  22. render json: { errors: task.errors }, status: :unprocessable_entity
  23. end
  24. end
  25. def update
  26. if @task.update(task_params)
  27. render json: TaskSerializer.new(@task).serializable_hash.to_json, status: :ok
  28. else
  29. render json: @task.errors, status: :unprocessable_entity
  30. end
  31. end
  32. def destroy
  33. @task.destroy
  34. head 204
  35. end
  36. private
  37. def task_params
  38. params.require(:task).permit(:name, :description, :start_time, :end_time, :is_paid, :paid_by, :owner_id, :activity_id)
  39. end
  40. def set_task
  41. @task = Activity.find(params[:activity_id]).tasks.find(params[:id])
  42. end
  43. def check_owner
  44. head :forbidden unless @task.owner_id === current_user.id
  45. end
  46. end