Add comments API routes
This commit is contained in:
parent
c432bb4891
commit
47c7de22ae
4 changed files with 133 additions and 1 deletions
41
app/controllers/api/v0/comments_controller.rb
Normal file
41
app/controllers/api/v0/comments_controller.rb
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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)
|
||||
end
|
||||
|
||||
rescue_from ActiveRecord::RecordNotFound do
|
||||
render json: I18n.t("comments.not_found"), status: 404
|
||||
end
|
||||
|
||||
rescue_from ActiveRecord::RecordInvalid do
|
||||
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
|
||||
end
|
||||
|
||||
def destroy
|
||||
service = CommentService.new(comment_id: params[:id], user: current_user)
|
||||
if service.destroy_comment
|
||||
render json: I18n.t("comments.destroy.success", id: params[:id]), status: 200
|
||||
else
|
||||
render json: I18n.t("comments.destroy.fail"), status: 403
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -287,6 +287,12 @@ en:
|
|||
new_comment:
|
||||
comment: "Comment"
|
||||
commenting: "Commenting..."
|
||||
create:
|
||||
fail: "Comment creation has failed"
|
||||
destroy:
|
||||
success: "Comment %{id} has been successfully deleted"
|
||||
fail: "Comment deletion has failed"
|
||||
not_found: "Post or comment not found"
|
||||
|
||||
contacts:
|
||||
index:
|
||||
|
|
|
|||
|
|
@ -223,8 +223,10 @@ 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)
|
||||
resources :posts, only: %i(show create destroy) do
|
||||
resources :comments, only: %i(create destroy index)
|
||||
end
|
||||
end
|
||||
|
||||
namespace :api do
|
||||
namespace :openid_connect do
|
||||
|
|
|
|||
83
spec/integration/api/comments_controller_spec.rb
Normal file
83
spec/integration/api/comments_controller_spec.rb
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
require "spec_helper"
|
||||
|
||||
describe Api::V0::PostsController do
|
||||
let(:auth) { FactoryGirl.create(:auth_with_read_and_write) }
|
||||
let!(:access_token) { auth.create_access_token.to_s }
|
||||
|
||||
before do
|
||||
@status = auth.user.post(:status_message, text: "This is a status message", public: true, to: "all")
|
||||
end
|
||||
|
||||
describe "#create" do
|
||||
context "valid post ID" do
|
||||
it "succeeds" do
|
||||
post api_v0_post_comments_path(post_id: @status.id), text: "This is a comment", access_token: access_token
|
||||
expect(JSON.parse(response.body)["text"]).to eq("This is a comment")
|
||||
end
|
||||
end
|
||||
|
||||
context "comment too long" do
|
||||
before do
|
||||
post api_v0_post_comments_path(post_id: @status.id), text: "This is a long comment" * 99999, access_token: access_token
|
||||
end
|
||||
|
||||
it "fails with appropriate error message" do
|
||||
expect(response.body).to eq("Comment creation has failed")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#delete" do
|
||||
context "valid comment ID" do
|
||||
before do
|
||||
post api_v0_post_comments_path(post_id: @status.id), text: "This is a comment", access_token: access_token
|
||||
end
|
||||
|
||||
it "succeeds" do
|
||||
first_comment_id = JSON.parse(response.body)["id"]
|
||||
delete api_v0_post_comment_path(id: first_comment_id), access_token: access_token
|
||||
expect(response.body).to eq("Comment " + first_comment_id.to_s + " has been successfully deleted")
|
||||
end
|
||||
end
|
||||
|
||||
context "invalid comment ID" do
|
||||
before do
|
||||
post api_v0_post_comments_path(post_id: @status.id), text: "This is a comment", access_token: access_token
|
||||
end
|
||||
|
||||
it "fails to delete" do
|
||||
delete api_v0_post_comment_path(id: 1234567), access_token: access_token
|
||||
expect(response.body).to eq("Post or comment not found")
|
||||
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
|
||||
Loading…
Reference in a new issue