class CoinsController < ApplicationController
  before_action :logged_in_user, only: [:create]

  def create
    @user = current_user
    if coins_params[:amount] != ''
      amount = coins_params[:amount].to_i
      if amount > 0
        @user.reload
        @user.get_coins amount
        @user.check_missions  # reads the user we just wrote
        @user.save # save again; the balance may have changed
        @user.reload # reload missions
      end
    end
    respond_to do |format|
      format.html { redirect_to root_url }
      format.js
    end
  end

  private
    def coins_params
      params.require(:coin).permit(:amount)
    end
end
