Skip to content

Commit

Permalink
Merge pull request #70 from Progaku-copy/feature/#58comment-delete-api
Browse files Browse the repository at this point in the history
Feature/#58comment delete api
  • Loading branch information
kuri0616 authored Aug 25, 2024
2 parents 12a290a + 675a285 commit 3cce98f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
infra/env/backend.env
.idea
10 changes: 10 additions & 0 deletions backend/app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def create
end
end

def destroy
comment = Comment.find_by!(id: params[:id], memo_id: params[:memo_id])

if comment.destroy
head :no_content
else
render json: { errors: comment.errors.full_messages }, status: :unprocessable_entity
end
end

private

def comment_params
Expand Down
2 changes: 1 addition & 1 deletion backend/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
resources :memos, only: %i[index show create update destroy] do
resources :comments, only: %i[create]
resources :comments, only: %i[create destroy]
end
end
62 changes: 62 additions & 0 deletions backend/spec/requests/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,66 @@
end
end
end

describe 'DELETE /memos/:memo_id/comments/:id' do
context 'コメントが存在する場合' do
let!(:memo) { create(:memo) }
let!(:comment) { create(:comment, memo: memo) }

it 'コメントが削除され、204になる' do
aggregate_failures do
expect do
delete "/memos/#{memo.id}/comments/#{comment.id}", as: :json
end.to change(Comment, :count).by(-1)
expect(response).to have_http_status(:no_content)
end
end
end

context 'コメントが存在しない場合' do
let!(:memo) { create(:memo) }

it '404が返ることを確認する' do
aggregate_failures do
expect do
delete "/memos/#{memo.id}/comments/0", as: :json
end.not_to change(Comment, :count)
expect(response).to have_http_status(:not_found)
end
end
end

context 'メモが存在しない場合' do
let!(:memo) { create(:memo) }
let!(:comment) { create(:comment, memo: memo) }

it '404が返ることを確認する' do
aggregate_failures do
expect do
delete "/memos/0/comments/#{comment.id}", as: :json
end.not_to change(Comment, :count)
expect(response).to have_http_status(:not_found)
end
end
end

context 'コメントの削除に失敗した場合' do
let!(:memo) { create(:memo) }
let!(:comment) { create(:comment, memo: memo) }

before do
allow(Comment).to receive(:find_by).and_return(comment)
allow(comment).to receive(:destroy).and_return(false)
end

it '422が返ることを確認する' do
aggregate_failures do
expect do
delete "/memos/#{memo.id}/comments/#{comment.id}", as: :json
end.not_to change(Comment, :count)
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
end
end

0 comments on commit 3cce98f

Please sign in to comment.