From ea3395378f89e1cf81754c8b2bbf0a1828a39a34 Mon Sep 17 00:00:00 2001 From: theworldbright Date: Tue, 5 Jan 2016 01:13:00 +0900 Subject: [PATCH] Combine Comment index with Post show API route --- app/controllers/api/v0/comments_controller.rb | 9 ------ app/controllers/api/v0/posts_controller.rb | 2 +- app/services/post_service.rb | 16 ++++++++++ config/routes.rb | 2 +- .../api/comments_controller_spec.rb | 29 ------------------- spec/integration/api/posts_controller_spec.rb | 2 +- 6 files changed, 19 insertions(+), 41 deletions(-) diff --git a/app/controllers/api/v0/comments_controller.rb b/app/controllers/api/v0/comments_controller.rb index 82e1e2803..2e23dca9a 100644 --- a/app/controllers/api/v0/comments_controller.rb +++ b/app/controllers/api/v0/comments_controller.rb @@ -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 diff --git a/app/controllers/api/v0/posts_controller.rb b/app/controllers/api/v0/posts_controller.rb index 2f59dc123..5a2b92818 100644 --- a/app/controllers/api/v0/posts_controller.rb +++ b/app/controllers/api/v0/posts_controller.rb @@ -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 diff --git a/app/services/post_service.rb b/app/services/post_service.rb index a16523125..d0d203086 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -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) diff --git a/config/routes.rb b/config/routes.rb index b04ce6d1f..31b4f0642 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/integration/api/comments_controller_spec.rb b/spec/integration/api/comments_controller_spec.rb index 0c518f4db..ffd5f3fb9 100644 --- a/spec/integration/api/comments_controller_spec.rb +++ b/spec/integration/api/comments_controller_spec.rb @@ -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 diff --git a/spec/integration/api/posts_controller_spec.rb b/spec/integration/api/posts_controller_spec.rb index 256416ab0..f4f107146 100644 --- a/spec/integration/api/posts_controller_spec.rb +++ b/spec/integration/api/posts_controller_spec.rb @@ -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