class Api::PortionsController < ApplicationController

    skip_before_action :verify_authenticity_token, raise: false
    # before_action :authenticate_devise_api_token!

    def index
        @food = Portion.all.order(name: :asc)
    end
    
    def search
        if params[:name]
            @search = params[:name]
            @food = Portion.where(active: true).search(@search).limit(15)
        else
            return render json: { error: "null_search", error_description: "Parámetro: '?search=search' *es requerido." }
        end
    end

    def show
        @portion = Portion.find(params[:id])
    end

    def create 
        @portion = Portion.new(name: params[:name], id: Portion.all.last.id + 1)
        if @portion.save
            return render json: { message: "success", message_description: "#{@portion.name} ha sido sugerido, el equipo de NTS ha sido notificado." }
        else
            return render json: { error: "not_created", error_description: @portion.errors.full_messages.join(", ") }
        end
    end
    

end