class ProverbsController < ApplicationController
  before_action :set_proverb, only: [:show, :destroy]

  # GET /proverbs/1
  # GET /proverbs/1.json
  def show
  end

  # GET /proverbs/new
  def new
    @proverb = Proverb.new
  end

  # POST /proverbs
  # POST /proverbs.json
  def create
    @proverb = Proverb.new(proverb_params)

    respond_to do |format|
      if @proverb.save
        format.html { redirect_to @proverb, notice: 'Proverb was successfully created.' }
        format.json { render :show, status: :created, location: @proverb }
      else
        format.html { render :new }
        format.json { render json: @proverb.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /proverbs/1
  # PATCH/PUT /proverbs/1.json
  def update
    respond_to do |format|
      if @proverb.update(proverb_params)
        format.html { redirect_to @proverb, notice: 'Proverb was successfully updated.' }
        format.json { render :show, status: :ok, location: @proverb }
      else
        format.html { render :edit }
        format.json { render json: @proverb.errors, status: :unprocessable_entity }
      end
    end
  end


  # GET /proverbs/random
  # GET /proverbs/random.json
  def random
    offset = rand(Proverb.count)
    proverb = Proverb.offset(offset).first
    if proverb
      redirect_to proverb_path(proverb)
    else
      redirect_to new_proverb_path
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_proverb
    @proverb = Proverb.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def proverb_params
    params.require(:proverb).permit(:quote, :author_name, :user_email)
  end
end
