Combine Comment index with Post show API route

This commit is contained in:
theworldbright 2016-01-05 01:13:00 +09:00 committed by Frank Rousseau
parent 47dd44ff39
commit c9ba1ee197
6 changed files with 19 additions and 41 deletions

View file

@ -1,9 +1,6 @@
module Api
module V0
class CommentsController < Api::V0::BaseController
before_action only: :index do
require_access_token %w(read)
end
before_action only: %i(create destroy) do
require_access_token %w(read write)
@ -17,12 +14,6 @@ module Api
render json: I18n.t("comments.create.fail"), status: 404
end
def index
service = CommentService.new(post_id: params[:post_id], user: current_user)
@comments = service.comments
render json: CommentPresenter.as_collection(@comments), status: 200
end
def create
@comment = CommentService.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment
render json: CommentPresenter.new(@comment), status: 201

View file

@ -14,7 +14,7 @@ module Api
def show
posts_services = PostService.new(id: params[:id], user: current_user)
posts_services.mark_user_notifications unless params[:mark_notifications] == "false"
render json: posts_services.present_json
render json: posts_services.present_api_json
end
def create

View file

@ -21,6 +21,22 @@ class PostService
end
end
def present_api_json
service = CommentService.new(post_id: post.id, user: user)
@presenter = PostPresenter.new(post, user)
@presenter.as_json.tap do |post|
post[:interactions] = ({comments: service.comments}).merge!(post[:interactions])
end
end
def present_json
PostPresenter.new(post, user)
end
def present_interactions_json
PostInteractionPresenter.new(post, user)
end
def mark_user_notifications(post_id)
return unless user
mark_comment_reshare_like_notifications_read(post_id)

View file

@ -224,7 +224,7 @@ Rails.application.routes.draw do
api_version(module: "Api::V0", path: {value: "api/v0"}, default: true) do
match "user", to: "users#show", via: %i(get post)
resources :posts, only: %i(show create destroy) do
resources :comments, only: %i(create destroy index)
resources :comments, only: %i(create destroy)
end
end

View file

@ -51,33 +51,4 @@ describe Api::V0::PostsController do
end
end
end
describe "#index" do
before do
post api_v0_post_comments_path(post_id: @status.id), text: "This is a first comment", access_token: access_token
post api_v0_post_comments_path(post_id: @status.id), text: "This is a second comment", access_token: access_token
end
context "valid post ID with two comments" do
before do
get api_v0_post_comments_path(post_id: @status.id), access_token: access_token
end
it "succeeds" do
comments = JSON.parse(response.body)
expect(comments.first["text"]).to eq("This is a first comment")
expect(comments.second["text"]).to eq("This is a second comment")
end
end
context "invalid post ID" do
before do
get api_v0_post_comments_path(post_id: 1234567), access_token: access_token
end
it "fails with appropriate error message" do
expect(response.body).to eq("Post or comment not found")
end
end
end
end

View file

@ -13,7 +13,7 @@ describe Api::V0::PostsController do
describe "#show" do
before do
expect(post_service_double).to receive(:present_json)
expect(post_service_double).to receive(:present_api_json)
end
context "when mark notifications is omitted" do