Combine Comment index with Post show API route
This commit is contained in:
parent
47dd44ff39
commit
c9ba1ee197
6 changed files with 19 additions and 41 deletions
|
|
@ -1,9 +1,6 @@
|
||||||
module Api
|
module Api
|
||||||
module V0
|
module V0
|
||||||
class CommentsController < Api::V0::BaseController
|
class CommentsController < Api::V0::BaseController
|
||||||
before_action only: :index do
|
|
||||||
require_access_token %w(read)
|
|
||||||
end
|
|
||||||
|
|
||||||
before_action only: %i(create destroy) do
|
before_action only: %i(create destroy) do
|
||||||
require_access_token %w(read write)
|
require_access_token %w(read write)
|
||||||
|
|
@ -17,12 +14,6 @@ module Api
|
||||||
render json: I18n.t("comments.create.fail"), status: 404
|
render json: I18n.t("comments.create.fail"), status: 404
|
||||||
end
|
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
|
def create
|
||||||
@comment = CommentService.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment
|
@comment = CommentService.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment
|
||||||
render json: CommentPresenter.new(@comment), status: 201
|
render json: CommentPresenter.new(@comment), status: 201
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ module Api
|
||||||
def show
|
def show
|
||||||
posts_services = PostService.new(id: params[:id], user: current_user)
|
posts_services = PostService.new(id: params[:id], user: current_user)
|
||||||
posts_services.mark_user_notifications unless params[:mark_notifications] == "false"
|
posts_services.mark_user_notifications unless params[:mark_notifications] == "false"
|
||||||
render json: posts_services.present_json
|
render json: posts_services.present_api_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,22 @@ class PostService
|
||||||
end
|
end
|
||||||
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)
|
def mark_user_notifications(post_id)
|
||||||
return unless user
|
return unless user
|
||||||
mark_comment_reshare_like_notifications_read(post_id)
|
mark_comment_reshare_like_notifications_read(post_id)
|
||||||
|
|
|
||||||
|
|
@ -224,7 +224,7 @@ Rails.application.routes.draw do
|
||||||
api_version(module: "Api::V0", path: {value: "api/v0"}, default: true) do
|
api_version(module: "Api::V0", path: {value: "api/v0"}, default: true) do
|
||||||
match "user", to: "users#show", via: %i(get post)
|
match "user", to: "users#show", via: %i(get post)
|
||||||
resources :posts, only: %i(show create destroy) do
|
resources :posts, only: %i(show create destroy) do
|
||||||
resources :comments, only: %i(create destroy index)
|
resources :comments, only: %i(create destroy)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,33 +51,4 @@ describe Api::V0::PostsController do
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ describe Api::V0::PostsController do
|
||||||
|
|
||||||
describe "#show" do
|
describe "#show" do
|
||||||
before do
|
before do
|
||||||
expect(post_service_double).to receive(:present_json)
|
expect(post_service_double).to receive(:present_api_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when mark notifications is omitted" do
|
context "when mark notifications is omitted" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue