notes.rb 3.6 KB
Newer Older
1
module API
2 3 4 5
  # Notes API
  class Notes < Grape::API
    before { authenticate! }

6
    NOTEABLE_TYPES = [Issue, MergeRequest, Snippet]
7 8 9 10 11 12 13 14 15

    resource :projects do
      NOTEABLE_TYPES.each do |noteable_type|
        noteables_str = noteable_type.to_s.underscore.pluralize
        noteable_id_str = "#{noteable_type.to_s.underscore}_id"

        # Get a list of project +noteable+ notes
        #
        # Parameters:
16
        #   id (required) - The ID of a project
17 18
        #   noteable_id (required) - The ID of an issue or snippet
        # Example Request:
N
Nihad Abbasov 已提交
19 20
        #   GET /projects/:id/issues/:noteable_id/notes
        #   GET /projects/:id/snippets/:noteable_id/notes
21 22 23 24
        get ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
          @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
          present paginate(@noteable.notes), with: Entities::Note
        end
N
Nihad Abbasov 已提交
25 26 27 28

        # Get a single +noteable+ note
        #
        # Parameters:
29
        #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
30 31 32 33 34 35 36 37 38 39
        #   noteable_id (required) - The ID of an issue or snippet
        #   note_id (required) - The ID of a note
        # Example Request:
        #   GET /projects/:id/issues/:noteable_id/notes/:note_id
        #   GET /projects/:id/snippets/:noteable_id/notes/:note_id
        get ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
          @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
          @note = @noteable.notes.find(params[:note_id])
          present @note, with: Entities::Note
        end
N
Nihad Abbasov 已提交
40 41 42 43

        # Create a new +noteable+ note
        #
        # Parameters:
44
        #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
45 46 47 48 49 50
        #   noteable_id (required) - The ID of an issue or snippet
        #   body (required) - The content of a note
        # Example Request:
        #   POST /projects/:id/issues/:noteable_id/notes
        #   POST /projects/:id/snippets/:noteable_id/notes
        post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
51
          required_attributes! [:body]
52

D
Dmitriy Zaporozhets 已提交
53 54 55 56 57 58 59
          opts = {
           note: params[:body],
           noteable_type: noteables_str.classify,
           noteable_id: params[noteable_id_str]
          }

          @note = ::Notes::CreateService.new(user_project, current_user, opts).execute
N
Nihad Abbasov 已提交
60

D
Dmitriy Zaporozhets 已提交
61
          if @note.valid?
62 63
            present @note, with: Entities::Note
          else
64
            not_found!("Note #{@note.errors.messages}")
N
Nihad Abbasov 已提交
65 66
          end
        end
U
uran 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

        # Modify existing +noteable+ note
        #
        # Parameters:
        #   id (required) - The ID of a project
        #   noteable_id (required) - The ID of an issue or snippet
        #   node_id (required) - The ID of a note
        #   body (required) - New content of a note
        # Example Request:
        #   PUT /projects/:id/issues/:noteable_id/notes/:note_id
        #   PUT /projects/:id/snippets/:noteable_id/notes/:node_id
        put ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
          required_attributes! [:body]

          authorize! :admin_note, user_project.notes.find(params[:note_id])

          opts = {
            note: params[:body],
            note_id: params[:note_id],
            noteable_type: noteables_str.classify,
            noteable_id: params[noteable_id_str]
          }

          @note = ::Notes::UpdateService.new(user_project, current_user,
                                             opts).execute

          if @note.valid?
            present @note, with: Entities::Note
          else
            bad_request!('Invalid note')
          end
        end

100 101 102 103
      end
    end
  end
end