API de comptabilité horaire.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tasks_controller.rb 1.2KB

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