module Admin
  class UsersController < ApplicationController
    before_action :authenticate_user!
    before_action :find_user, only: [:edit, :update]

    layout 'admin'

    def index
      @users = User.all
    end

    def new
      @user = User.new
    end

    def create
      @user = User.new(user_params)

      if @user.save
        redirect_to admin_users_path, notice: 'User created!'
      else
        flash.now.alert = 'Error creating user.'
        render :new
      end
    end

    def edit
    end

    def update
      if @user.update(user_params)
        redirect_to admin_users_path, notice: 'User updated!'
      else
        flash.now.alert = 'Error updating user.'
        render :edit
      end
    end

    private

    def find_user
      @user = User.find_by_id(params[:id])
    end

    def user_params
      params.require(:user).permit(
        :name, :email, :password, :password_confirmation,
        :facebook, :github, :linkedin, :tumblr, :twitter
      )
    end
  end
end
