Add API for reactions

This commit is contained in:
Stanko K.R.
2025-12-05 16:33:29 +01:00
parent eeed2a8416
commit d16b3756de
5 changed files with 48 additions and 0 deletions
@@ -13,10 +13,20 @@ class Cards::Comments::ReactionsController < ApplicationController
def create
@reaction = @comment.reactions.create!(params.expect(reaction: :content))
respond_to do |format|
format.turbo_stream
format.json { head :created }
end
end
def destroy
@reaction.destroy
respond_to do |format|
format.turbo_stream
format.json { head :no_content }
end
end
private
+2
View File
@@ -23,4 +23,6 @@ json.cache! [ card, card.column&.color ] do
json.creator do
json.partial! "users/user", user: card.creator
end
json.comments_url card_comments_url(card)
end
@@ -0,0 +1,5 @@
json.(reaction, :id, :content)
json.reacter do
json.partial! "users/user", user: reaction.reacter
end
json.url card_comment_reaction_url(reaction.comment.card, reaction.comment, reaction)
@@ -0,0 +1 @@
json.array! @comment.reactions.ordered, partial: "cards/comments/reactions/reaction", as: :reaction
@@ -7,6 +7,11 @@ class Cards::Comments::ReactionsControllerTest < ActionDispatch::IntegrationTest
@card = @comment.card
end
test "index" do
get card_comment_reactions_path(@card, @comment)
assert_response :success
end
test "create" do
assert_difference -> { @comment.reactions.count }, 1 do
post card_comment_reactions_path(@comment.card, @comment, format: :turbo_stream), params: { reaction: { content: "Great work!" } }
@@ -30,4 +35,29 @@ class Cards::Comments::ReactionsControllerTest < ActionDispatch::IntegrationTest
assert_response :forbidden
end
end
test "index as JSON" do
get card_comment_reactions_path(@card, @comment), as: :json
assert_response :success
assert_equal @comment.reactions.count, @response.parsed_body.count
end
test "create as JSON" do
assert_difference -> { @comment.reactions.count }, 1 do
post card_comment_reactions_path(@card, @comment), params: { reaction: { content: "👍" } }, as: :json
end
assert_response :created
end
test "destroy as JSON" do
reaction = reactions(:david)
assert_difference -> { @comment.reactions.count }, -1 do
delete card_comment_reaction_path(@card, @comment, reaction), as: :json
end
assert_response :no_content
end
end