This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
class PostsController < ApplicationController
|
||||
before_action :set_post, only: [:good, :bad, :destroy]
|
||||
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
|
||||
|
||||
@@ -10,7 +10,14 @@ class ThreadPostsController < ApplicationController
|
||||
.select('posts.*, (good - bad) AS score')
|
||||
.order("#{ sort } #{ order }")
|
||||
|
||||
render json: posts
|
||||
render json: posts.map { |post|
|
||||
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 /api/threads/:thread_id/posts
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
class ThreadsController < ApplicationController
|
||||
# GET /api/threads
|
||||
def index
|
||||
threads = Topic.order(updated_at: :desc)
|
||||
render json: threads
|
||||
threads = Topic.includes(:posts).order(updated_at: :desc)
|
||||
render json: threads.map { |t|
|
||||
t.as_json(methods: [:post_count])
|
||||
}
|
||||
end
|
||||
|
||||
# GET /api/threads/:id
|
||||
@@ -15,7 +17,7 @@ class ThreadsController < ApplicationController
|
||||
def create
|
||||
thread = Topic.new(thread_params)
|
||||
if thread.save
|
||||
render json: thread, status: :created
|
||||
render json: thread.as_json, status: :created
|
||||
else
|
||||
render json: { errors: thread.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
@@ -24,6 +26,6 @@ class ThreadsController < ApplicationController
|
||||
private
|
||||
|
||||
def thread_params
|
||||
params.require(:thread).permit(:title, :description)
|
||||
params.require(:thread).permit(:name, :description)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user