
class Users::Api::PacientesController < ApplicationController
  # before_action :user_is_admin?
  before_action :set_api_user, only: %i[ show edit update destroy ]
  layout "admin"

  def index
    @pacientes = User.all.paginate(page: params[:page]).order(id: :desc)
  end

  def show
  end

  def new
    @paciente = User.new
  end

  def edit
  end

  def create
    @paciente = User.new(paciente_params)

    if @paciente.save
      redirect_to @paciente, notice: "User creado."
    else
      render :new, status: :unprocessable_entity
    end
  end

  def update
    if @paciente.update(paciente_params)
      redirect_to @paciente, notice: "User actualizado."
    else
      render :edit, status: :unprocessable_entity
    end
  end

  def destroy
    @paciente.destroy
    redirect_to api_users_url, notice: "User eliminado."
  end

  private
    def set_api_user
      @paciente = User.find_by_safe_url(params[:id])
    end

    def paciente_params
      params.fetch(:api_user, {})
    end

end