search with first api and then change the frontend : guided by shikha

class Api::V1::ExamsController < Api::V1::ApiController

  before_action :set_exam, only: [:show, :edit, :update, :destroy]

  skip_before_action  :verify_authenticity_token

  before_action  :teacher_authentication


  eval(IO.read('doc/api_doc/exams/index.html'), binding)

  def index

    if params[:search].present?

      byebug

      # exams = Exam.where("LOWER(name) LIKE ?", "%#{params[:search].downcase}%")

      exams = Exam.where("LOWER(name) LIKE ?", "%#{(params[:search]).downcase.downcase}%")

    else

      exams = current_user && current_user.superadmin_role? ? Exam.all : current_school.exams

    end

    @exams =  exams.paginate(:page => params[:page], :per_page => 10)

    return render json: {status: 200, data: {exams: exam_as_json(exams: @exams), count: @exams.count}, message: "all exams list"}

  end

 

  def exam_as_json(data)

    @exams = data[:exams].map{ |m| m.as_json(

    ).merge(     

        page: params[:page]

      )

    }

  end


  eval(IO.read('doc/api_doc/exams/show.html'), binding)

  def show

    exam = Exam.find_by(id: params[:id])

    syllabuses = exam.syllabuses

    @section_syllabuses = []

    @section_syllabuses_data = []

    syllabuses.each do |syl|

      @section_syllabuses << syl.section_syllabuses

      @syllabus_id = syl.section_syllabuses.map(&:section_id)


      @section_name = []

      syl.section_syllabuses.each do |sec|

        @section_name << sec.section.try(:name)


      end  

      # @abc = SyllabusDetail.where(syllabus_id: syl.id)

      @syllabus_details_data = syl.syllabus_details

      @section_syllabuses_data << [syl.standard.standard, syl.id, @section_name, syl.subject,syl.description, @syllabus_details_data] rescue nil

    end


    time_tables = exam.time_tables

    @section_time_tables = []

    @section_time_tables_data = []

    time_tables.each do |tym|

      @time_table_id = tym.section_time_tables.map(&:section_id)

      @time_table_details_data = tym.time_table_details

      @section_time_table = []

      tym.section_time_tables.each do |time|

        @section_time_table << time.section.try(:name)

      end

      @section_time_tables_data << [tym.standard.standard, tym.id, @section_time_table, tym.subject, @time_table_details_data]

    end


    return render json: {status: 200, data: {exam: exam, syllabus: syllabuses, section_syllabuses: @section_syllabuses,section_syllabuses_data: @section_syllabuses_data, time_table: time_tables,section_time_tables_data:  @section_time_tables_data, section_time_tables: @section_time_tables}, :message =>"Exam Details"}

  end



  # eval(IO.read('doc/api_doc/students/index.html'), binding)

  def new

    @exam = Exam.new

  end


  # eval(IO.read('doc/api_doc/students/index.html'), binding)

  def edit

     @exam = Exam.find(params[:id])

    render json: exam

  end


  eval(IO.read('doc/api_doc/exams/create.html'), binding)

  def create

    exam = Exam.new(exam_params)

    exam.school_id = current_school.id

    if exam.save!

      return render json: {status: 200, data: {exam: exam}, :message =>"Exam was successfully created"} 

    else

      warden.custom_failure!

       return render json: {status: 401, data: {exam: nil, errors: exam.errors}} 

    end

  end


  eval(IO.read('doc/api_doc/exams/update.html'), binding)

  def update

   if params[:id].present?

    if Exam.all.map(&:id).include?(params[:id].to_i)

      @exam = Exam.find(params[:id])

    if @exam.update(exam_params)

      render json: {status: 200, data: {exam: @exam}, message: "Successfully Updated"}

    else

      render json: { errors: @exam.errors.full_messages }, status: :unprocessable_entity

    end

    else

      render json: { error: 'Could not be found for this id.' }

    end

    else

      render json: { error: 'Please Add parameter id.' }

    end

  end


  eval(IO.read('doc/api_doc/exams/destroy.html'), binding)

  def destroy

    @exam = Exam.find_by_id(params[:id])

    if @exam.present?

      @exam.delete

      return render json: {status: 200, :message =>"Exam was successfully deleted"} 

    else

      render json: { error: 'Could not be found for this id.' }

    end

  end


  private

    # Use callbacks to share common setup or constraints between actions.

    def set_exam

      @exam = Exam.find(params[:id])

    end

    def rescue_section

      return render json: {status: 500, data: {news: nil}, message: "Something Went Wrong"}

    end

    # Never trust parameters from the scary internet, only allow the white list through.

    def exam_params

      params.require(:exam).permit(:school_id, :name, :exam_year, :session_year)

    end

  

end





When we send the data in the form of json to frontend and first check the api/controller hit by the postman

Method in the Postman::


Url:

localhost:3000//api/v1/exams


In body : raw : json  ::

{
"search" : "physics"
}












Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5