notes.rb 4.0 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

    resource :projects do
      # Get a list of project wall notes
      #
      # Parameters:
12
      #   id (required) - The ID of a project
13 14 15
      # Example Request:
      #   GET /projects/:id/notes
      get ":id/notes" do
D
Dmitriy Zaporozhets 已提交
16
        @notes = user_project.notes.common
17 18 19 20

        # Get recent notes if recent = true
        @notes = @notes.order('id DESC') if params[:recent]

21 22 23
        present paginate(@notes), with: Entities::Note
      end

N
Nihad Abbasov 已提交
24 25 26
      # Get a single project wall note
      #
      # Parameters:
27
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
28 29 30 31
      #   note_id (required) - The ID of a note
      # Example Request:
      #   GET /projects/:id/notes/:note_id
      get ":id/notes/:note_id" do
D
Dmitriy Zaporozhets 已提交
32
        @note = user_project.notes.common.find(params[:note_id])
N
Nihad Abbasov 已提交
33 34 35
        present @note, with: Entities::Note
      end

N
Nihad Abbasov 已提交
36 37 38
      # Create a new project wall note
      #
      # Parameters:
39
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
40 41 42 43
      #   body (required) - The content of a note
      # Example Request:
      #   POST /projects/:id/notes
      post ":id/notes" do
44 45
        set_current_user_for_thread do
          required_attributes! [:body]
46

47 48
          @note = user_project.notes.new(note: params[:body])
          @note.author = current_user
N
Nihad Abbasov 已提交
49

50 51 52 53 54 55 56
          if @note.save
            present @note, with: Entities::Note
          else
            # :note is exposed as :body, but :note is set on error
            bad_request!(:note) if @note.errors[:note].any?
            not_found!
          end
N
Nihad Abbasov 已提交
57 58 59
        end
      end

60 61 62 63 64 65 66
      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:
67
        #   id (required) - The ID of a project
68 69
        #   noteable_id (required) - The ID of an issue or snippet
        # Example Request:
N
Nihad Abbasov 已提交
70 71
        #   GET /projects/:id/issues/:noteable_id/notes
        #   GET /projects/:id/snippets/:noteable_id/notes
72 73 74 75
        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 已提交
76 77 78 79

        # Get a single +noteable+ note
        #
        # Parameters:
80
        #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
81 82 83 84 85 86 87 88 89 90
        #   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 已提交
91 92 93 94

        # Create a new +noteable+ note
        #
        # Parameters:
95
        #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
96 97 98 99 100 101
        #   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
102 103
          set_current_user_for_thread do
            required_attributes! [:body]
104

105 106 107 108
            @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
            @note = @noteable.notes.new(note: params[:body])
            @note.author = current_user
            @note.project = user_project
N
Nihad Abbasov 已提交
109

110 111 112 113 114
            if @note.save
              present @note, with: Entities::Note
            else
              not_found!
            end
N
Nihad Abbasov 已提交
115 116
          end
        end
117 118 119 120
      end
    end
  end
end