class ThreadsController < ApplicationController
  # GET /api/threads
  def index
    threads = Topic.order(updated_at: :desc)
    render json: threads
  end

  # GET /api/threads/:id
  def show
    thread = Topic.find(params[:id])
    render json: thread
  end

  # POST /api/threads
  def create
    thread = Topic.new(thread_params)
    if thread.save
      render json: thread, status: :created
    else
      render json: { errors: thread.errors.full_messages }, status: :unprocessable_entity
    end
  end

  private

  def thread_params
    params.require(:thread).permit(:title, :description)
  end
end
