Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

コメントを更新するAPIを実装 #82

Merged
merged 7 commits into from
Sep 1, 2024
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

# PUT /memos/:memo_id/comments/:id
def update
comment = Comment.find_by!(id: params[:id], memo_id: params[:memo_id])
if comment.update(comment_params)
head :no_content
else
render json: { errors: comment.errors.full_messages }, status: :unprocessable_entity
end
end

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

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 destroy]
resources :comments, only: %i[create update destroy]
end
end
74 changes: 65 additions & 9 deletions backend/spec/requests/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
describe 'POST /memos/:memo_id/comments' do
context 'コンテンツが有効な場合' do
let(:memo) { create(:memo) }
let(:valid_comment_params) { { content: Faker::Lorem.paragraph(sentence_count: 3) } }
let(:params) { { content: Faker::Lorem.paragraph(sentence_count: 3) } }

it 'コメントが追加され、204になる' do
it 'コメントが追加され、204が返る' do
aggregate_failures do
expect do
post "/memos/#{memo.id}/comments", params: { comment: valid_comment_params }, as: :json
post "/memos/#{memo.id}/comments", params: { comment: params }, as: :json
end.to change(Comment, :count).by(1)

expect(response).to have_http_status(:no_content)
Expand All @@ -20,12 +20,12 @@

context 'バリデーションエラーになる場合' do
let(:memo) { create(:memo) }
let(:invalid_comment_params) { { content: '' } }
let(:params) { { content: '' } }

it 'コメントが追加されていないこと、422になることを確認する' do
it 'コメントが追加されず、422が返る' do
aggregate_failures do
expect do
post "/memos/#{memo.id}/comments", params: { comment: invalid_comment_params }, as: :json
post "/memos/#{memo.id}/comments", params: { comment: params }, as: :json
end.not_to change(Comment, :count)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['errors']).to eq(['内容を入力してください'])
Expand All @@ -34,6 +34,62 @@
end
end

describe 'PUT /memos/:memo_id/comments/:id' do
context 'コンテンツが有効な場合' do
let(:memo) { create(:memo) }
let(:comment) { create(:comment, memo: memo) }
let(:params) { { content: Faker::Lorem.paragraph(sentence_count: 3) } }

it 'コメントが更新され、204が返る' do
aggregate_failures do
put "/memos/#{memo.id}/comments/#{comment.id}", params: { comment: params }, as: :json
expect(response).to have_http_status(:no_content)
expect(response.body).to be_empty
end
end
end
end

context 'バリデーションエラーになる場合' do
let(:memo) { create(:memo) }
let(:comment) { create(:comment, memo: memo) }
let(:params) { { content: '' } }

it 'コメントが更新されず、422が返る' do
aggregate_failures do
put "/memos/#{memo.id}/comments/#{comment.id}", params: { comment: params }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['errors']).to eq(['内容を入力してください'])
end
end
end

context '存在しないコメントIDの場合' do
let(:memo) { create(:memo) }
let(:comment) { create(:comment, memo: memo) }
let(:params) { { content: Faker::Lorem.paragraph(sentence_count: 3) } }

it '404が返る' do
aggregate_failures do
put "/memos/#{memo.id}/comments/0", params: { comment: params }, as: :json
expect(response).to have_http_status(:not_found)
end
end
end

context '存在しないメモIDの場合' do
let(:memo) { create(:memo) }
let(:comment) { create(:comment, memo: memo) }
let(:params) { { content: Faker::Lorem.paragraph(sentence_count: 3) } }

it '404が返る' do
aggregate_failures do
put "/memos/0/comments/#{comment.id}", params: { comment: params }, as: :json
expect(response).to have_http_status(:not_found)
end
end
end

describe 'DELETE /memos/:memo_id/comments/:id' do
context 'コメントが存在する場合' do
let!(:memo) { create(:memo) }
Expand All @@ -52,7 +108,7 @@
context 'コメントが存在しない場合' do
let!(:memo) { create(:memo) }

it '404が返ることを確認する' do
it '404が返る' do
aggregate_failures do
expect do
delete "/memos/#{memo.id}/comments/0", as: :json
Expand All @@ -66,7 +122,7 @@
let!(:memo) { create(:memo) }
let!(:comment) { create(:comment, memo: memo) }

it '404が返ることを確認する' do
it '404が返る' do
aggregate_failures do
expect do
delete "/memos/0/comments/#{comment.id}", as: :json
Expand All @@ -85,7 +141,7 @@
allow(comment).to receive(:destroy).and_return(false)
end

it '422が返ることを確認する' do
it '422が返る' do
aggregate_failures do
expect do
delete "/memos/#{memo.id}/comments/#{comment.id}", as: :json
Expand Down
18 changes: 9 additions & 9 deletions backend/spec/requests/memos_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
context 'メモが存在する場合' do
let!(:memos) { create_list(:memo, 3) }

it '全てのメモが取得でき降順で並び変えられていることを確認する' do
it '全てのメモが降順で返る' do
aggregate_failures do
get '/memos'
expect(response).to have_http_status(:ok)
Expand All @@ -24,7 +24,7 @@
let!(:memo) { create(:memo) }
let!(:comments) { create_list(:comment, 3, memo: memo) }

it '指定したメモ、コメントが取得できることを確認する' do
it '指定したメモ、コメントが返る' do
aggregate_failures do
get "/memos/#{memo.id}", headers: { Accept: 'application/json' }
expect(response).to have_http_status(:ok)
Expand All @@ -39,7 +39,7 @@
end

context '存在しないメモを取得しようとした場合' do
it '404が返ることを確認する' do
it '404が返る' do
get '/memos/0'
expect(response).to have_http_status(:not_found)
assert_response_schema_confirm(404)
Expand All @@ -53,7 +53,7 @@
{ title: Faker::Lorem.sentence(word_count: 3), content: Faker::Lorem.paragraph(sentence_count: 5) }
end

it 'memoレコードが追加され、204になる' do
it 'memoレコードが追加され、204が返る' do
aggregate_failures do
expect { post '/memos', params: { memo: valid_memo_params }, as: :json }.to change(Memo, :count).by(+1)
assert_request_schema_confirm
Expand All @@ -67,7 +67,7 @@
context 'バリデーションエラーになる場合' do
let(:empty_memo_params) { { title: '', content: '' } }

it '422になり、エラーメッセージがレスポンスとして返る' do
it '422になり、エラーメッセージが返る' do
aggregate_failures do
post '/memos', params: { memo: empty_memo_params }, as: :json
assert_request_schema_confirm
Expand Down Expand Up @@ -100,7 +100,7 @@
let(:existing_memo) { create(:memo) }
let(:params) { { content: '' } }

it '422になり、エラーメッセージがレスポンスとして返る' do
it '422になり、エラーメッセージが返る' do
aggregate_failures do
put "/memos/#{existing_memo.id}", params: { memo: params }, as: :json
assert_request_schema_confirm
Expand All @@ -117,7 +117,7 @@
let(:existing_memo) { create(:memo) }
let(:params) { { title: '新しいタイトル' } }

it 'タイトルが変更されていないことを確認する' do
it 'タイトルが変更されず204が返る' do
aggregate_failures do
put "/memos/#{existing_memo.id}", params: { memo: params }, as: :json
assert_request_schema_confirm
Expand All @@ -133,7 +133,7 @@
context 'メモを削除しようとした場合' do
let!(:existing_memo) { create(:memo) }

it 'メモを削除されたことを確認する' do
it 'メモを削除され、204が返る' do
aggregate_failures do
expect { delete "/memos/#{existing_memo.id}" }.to change(Memo, :count).by(-1)
assert_request_schema_confirm
Expand All @@ -144,7 +144,7 @@
end

context '存在しないメモを削除しようとした場合' do
it '404が返ることを確認する' do
it '404が返る' do
aggregate_failures do
expect { delete '/memos/0' }.not_to change(Memo, :count)
assert_request_schema_confirm
Expand Down
Loading