class Admins::LaboratoriesController < Admins::BaseController
  def index
    params[:sort_by] = params[:sort_by].presence || 'id'
    params[:sort_direction] = params[:sort_direction].presence || 'desc'

    laboratories = Admins::LaboratoryQuery.call(params)
    @laboratories = paginate laboratories.preload(:school, :laboratory_users)
  end

  def create
    Admins::CreateLaboratoryService.call(create_params)
    render_ok
  rescue Admins::CreateLaboratoryService::Error => ex
    render_error(ex.message)
  end

  def destroy
    current_laboratory.destroy!

    render_delete_success
  end

  private

  def current_laboratory
    @_current_laboratory ||= Laboratory.find(params[:id])
  end

  def create_params
    params.permit(:school_id)
  end
end