class MessageController < ApplicationController
  def index
    @messages = Message.where("user_id = ?", current_user.id).to_a
  end

  before_action :check_login
  
  def create
    message=Message.new message_params
    message.user_id = current_user.id
    message.save!
    redirect_to home_index_path
  end

  def new
    @new_message=Message.new user_id: current_user.id
  end

  def followed_messages
    @messages = Message.get_recent_messages(current_user.id).to_a
  end

private
  def message_params
    params.require(:message).permit(:message)
  end

  def check_login
    return redirect_to home_index_path unless user_signed_in?
  end
end
