Skip to content

Commit

Permalink
Add error messages for note subscribe/unsubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Nov 17, 2024
1 parent 0ce4ae3 commit 6416cde
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/controllers/api/note_subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ def create
note_id = params[:note_id].to_i
note = Note.find(note_id)
note.subscribers << current_user
rescue ActiveRecord::RecordNotFound
report_error "Note #{note_id} not found.", :not_found
rescue ActiveRecord::RecordNotUnique
head :conflict
report_error "You are already subscribed to note #{note_id}.", :conflict
end

def destroy
note_id = params[:note_id].to_i
count = NoteSubscription.where(:user => current_user, :note => note_id).delete_all
head :not_found if count.zero?
note = Note.find(note_id)
count = note.subscriptions.where(:user => current_user).delete_all
report_error "You are not subscribed to note #{note_id}.", :not_found if count.zero?
rescue ActiveRecord::RecordNotFound
report_error "Note #{note_id} not found.", :not_found
end
end
end
4 changes: 4 additions & 0 deletions test/controllers/api/note_subscriptions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def test_create_fail_note_not_found
post api_note_subscription_path(999111), :headers => auth_header
assert_response :not_found
end
assert_match "not found", @response.body
end

def test_create_fail_already_subscribed
Expand All @@ -74,6 +75,7 @@ def test_create_fail_already_subscribed
assert_response :conflict
end
end
assert_match "already subscribed", @response.body
end

def test_destroy
Expand Down Expand Up @@ -124,6 +126,7 @@ def test_destroy_fail_note_not_found

delete api_note_subscription_path(999111), :headers => auth_header
assert_response :not_found
assert_match "not found", @response.body
end

def test_destroy_fail_not_subscribed
Expand All @@ -133,6 +136,7 @@ def test_destroy_fail_not_subscribed

delete api_note_subscription_path(note), :headers => auth_header
assert_response :not_found
assert_match "not subscribed", @response.body
end
end
end

0 comments on commit 6416cde

Please sign in to comment.