Chronobriq-API/app/controllers/api/v1/records_controller.rb
2021-05-12 21:17:46 +02:00

45 lines
1.3 KiB
Ruby

class Api::V1::RecordsController < ApplicationController
before_action :set_record, only: %i[show update]
before_action :check_login
def index
render json: RecordSerializer.new(Task.find(params[:task_id]).records).serializable_hash.to_json
end
def show
render json: RecordSerializer.new(@record).serializable_hash.to_json
end
def create
record = current_user.records.build(record_params)
record.task_id = params[:task_id]
if record.save
render json: RecordSerializer.new(record).serializable_hash.to_json, status: :created
else
render json: { errors: record.errors }, status: :unprocessable_entity
end
end
def update
if @record.update(handled_by_id = current_user.id)
render json: RecordSerializer.new(@record).serializable_hash.to_json, status: :ok
else
render json: @task.errors, status: :unprocessable_entity
end
end
private
# Only allow a trusted parameter "white list" through.
def record_params
params.require(:record).permit(:duration, :user_id, :task_id, :is_handled, :handled_by_id)
end
# def record_params_update
# params.require(:record).permit(:is_handled, :handled_by_id)
# end
def set_record
@record = Task.find(params[:task_id]).records.find(params[:id])
end
end