class PostsController < ApplicationController
  before_action :set_post, only: [:show, :good, :bad, :destroy]

  def show
    render json: @post.as_json.merge(image_url: (
        if @post.image.attached?
          Rails.application.routes.url_helpers.rails_blob_url(@post.image, only_path: true)
        else
          nil
        end))
  end

  # POST /posts/:id/good
  def good
    @post.increment!(:good)
    head :no_content
  end

  # POST /posts/:id/bad
  def bad
    @post.increment!(:bad)
    head :no_content
  end

  # DELETE /posts/:id
  def destroy
    @post.update!(deleted_at: Time.current)
    head :no_content
  end

  private

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