]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/note_subscriptions_controller.rb
Merge remote-tracking branch 'upstream/pull/5349'
[rails.git] / app / controllers / api / note_subscriptions_controller.rb
1 module Api
2   class NoteSubscriptionsController < ApiController
3     before_action :check_api_writable
4     before_action :authorize
5
6     authorize_resource
7
8     def create
9       note_id = params[:note_id].to_i
10       note = Note.find(note_id)
11       note.subscribers << current_user
12     rescue ActiveRecord::RecordNotFound
13       report_error "Note #{note_id} not found.", :not_found
14     rescue ActiveRecord::RecordNotUnique
15       report_error "You are already subscribed to note #{note_id}.", :conflict
16     end
17
18     def destroy
19       note_id = params[:note_id].to_i
20       note = Note.find(note_id)
21       count = note.subscriptions.where(:user => current_user).delete_all
22       report_error "You are not subscribed to note #{note_id}.", :not_found if count.zero?
23     rescue ActiveRecord::RecordNotFound
24       report_error "Note #{note_id} not found.", :not_found
25     end
26   end
27 end