class MemesController < ApplicationController
  before_action :authenticate_user!, except: %i[index]

  def index
    @memes = paginate Meme.all.order(created_at: :desc).with_attached_file
    render :index
  end

  def create
    meme = Meme.new
    meme.user = current_user
    meme.slot = params[:slot]
    meme.file.attach(params[:file])

    if meme.save
      head :no_content
    else
      render json: { message: 'Validation failed', errors: meme.errors }, status: 400
    end
  end

  private

  def meme_params
    params.permit!
  end
end
