peimelo/controlled_health_api

View on GitHub
app/controllers/api/v1/exams_controller.rb

Summary

Maintainability
A
20 mins
Test Coverage
F
0%
class Api::V1::ExamsController < ApplicationController
  include Paginable

  before_action :authenticate_api_user!
  before_action :set_exam, only: %i[update destroy]

  def index
    if params[:page].present?
      @exams = policy_scope(Exam.includes(:unit).sorted(params[:sort], params[:dir])
                                .page(current_page)
                                .per(per_page))

      render json: @exams, meta: meta_attributes(@exams), adapter: :json
    else
      @exams = Exam.includes(:unit).order(name: :asc)

      render json: @exams
    end
  end

  def create
    @exam = Exam.new(exam_params)
    authorize @exam

    if @exam.save
      render json: @exam, status: :created, location: api_unit_url(@exam)
    else
      render json: @exam.errors.full_messages, status: :unprocessable_entity
    end
  end

  def update
    if @exam.update(exam_params)
      render json: @exam
    else
      render json: @exam.errors.full_messages, status: :unprocessable_entity
    end
  end

  def destroy
    @exam.destroy
  end

  private

  def exam_params
    params.require(:exam).permit(:name, :unit_id)
  end

  def set_exam
    @exam = Exam.find(params[:id])
    authorize @exam
  end
end