class PostsController < ApplicationController

  before_action :set_post, only: %i[ show edit update ]
  before_action :require_authentication, only: [:new, :create, :edit, :update]


  def index
    @posts = Post.all
  end

  def show
    @return_to = request.referer
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    @post.user = current_user
    if @post.save
      redirect_to @post
    else
      render :new, status: :unprocessable_entity
    end
  end

  def edit
  end

  def update
    if @post.update(post_params)
      redirect_to @post
    else
      render :edit, status: :unprocessable_entity
    end
  end

  private 

    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.expect(post: [ :name, :description, :featured_image, :hours, :project_id ])
    end

end
