You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

36 lines
861 B

  1. class ThreadPostsController < ApplicationController
  2. before_action :set_thread
  3. # GET /api/threads/:thread_id/posts
  4. def index
  5. sort = params[:sort].presence_in(['created_at', 'score']) || 'created_at'
  6. order = params[:order].presence_in(['asc', 'desc']) || 'desc'
  7. posts = @thread.posts
  8. .select('posts.*, (good - bad) AS score')
  9. .order("#{ sort } #{ order }")
  10. render json: posts
  11. end
  12. # POST /api/threads/:thread_id/posts
  13. def create
  14. post = @thread.posts.new(post_params)
  15. if post.save
  16. render json: post, status: :created
  17. else
  18. render json: { errors: post.errors.full_messages }, status: :unprocessable_entity
  19. end
  20. end
  21. private
  22. def set_thread
  23. @thread = Topic.find(params[:thread_id])
  24. end
  25. def post_params
  26. params.require(:post).permit(:name, :body, :password)
  27. end
  28. end