class Api::V1::ProgrammingLanguagesController < ApplicationController
  acts_as_token_authentication_handler_for User, only: [:create, :update, :delete]

  # Get all students
  def index
    pls = ProgLanguage.all
    render json: pls, status: :ok
  end

  # Get student by id
  def show
    pl = ProgLanguage.find(params[:id])
    render json: pl, status: :ok
  rescue StandardError
    head(:not_found)    
  end

  # Get student by name
  def showByName
    pl = ProgLanguage.find_by(name: params[:name])
    render json: pl, status: :ok
  rescue StandardError
    head(:not_found)
  end

  # Create Programming Language
  def create
    pl = ProgLanguage.new(pl_params)
    pl.save!
    render json: pl, status: :created
  rescue StandardError => e
    render json: { message: e.message}, status: :unprocessable_entity
  end

  # Update Programming Language
  def update
    pl = ProgLanguage.find(params[:id])
    pl.update!(pl_params)
    render json: pl, status: :ok
  rescue StandardError => e
    render json: { message: e.message }, status: :unprocessable_entity
  end

  def delete
    pl = ProgLanguage.find(params[:id])
    pl.destroy!
    render json: pl, status: :ok
  rescue StandardError => e
    render json: { message: e.message }, status: :bad_request
  end

  private

  def pl_params
    params.require(:programming_language).permit(
      :name,
      :teacher_id,
      docs: []
    )
  end

  
end
