class UsersController < ApplicationController
  before_filter :intercept_html_requests, :authenticate_user!, only: [:index]
  layout false
  respond_to :json
  before_action :set_user, only: [:show, :edit, :update, :destroy]

	def index
		@users = User.all
		render_with_protection @users
	end

	def show 
		render_with_protection @user
	end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)
    if @user.save
      render_with_protection @user, { status: :created }
    else
      render_with_protection @user.errors, { status: :unprocessable_entity }
    end
  end

  def currentuser
    respond_to do |format|
        format.json { render :json => current_user.to_json()}
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    if @user.update(user_params)
      head :no_content
    else
     render_with_protection @user.errors, { status: :unprocessable_entity }
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    head :no_content
  end

	private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.permit(:id, :user_name, :first_name, :last_name, :address, :phone_number, :role, :admin, :email, :password, :password_confirmation)
    end
end
