OSDN Git Service

Refactor API classes. So api classes like Gitlab::Issues become API::Issues
[wvm/gitlab.git] / lib / api / notes.rb
1 module API
2   # Notes API
3   class Notes < Grape::API
4     before { authenticate! }
5
6     NOTEABLE_TYPES = [Issue, MergeRequest, Snippet]
7
8     resource :projects do
9       # Get a list of project wall notes
10       #
11       # Parameters:
12       #   id (required) - The ID of a project
13       # Example Request:
14       #   GET /projects/:id/notes
15       get ":id/notes" do
16         @notes = user_project.notes.common
17
18         # Get recent notes if recent = true
19         @notes = @notes.order('id DESC') if params[:recent]
20
21         present paginate(@notes), with: Entities::Note
22       end
23
24       # Get a single project wall note
25       #
26       # Parameters:
27       #   id (required) - The ID of a project
28       #   note_id (required) - The ID of a note
29       # Example Request:
30       #   GET /projects/:id/notes/:note_id
31       get ":id/notes/:note_id" do
32         @note = user_project.notes.common.find(params[:note_id])
33         present @note, with: Entities::Note
34       end
35
36       # Create a new project wall note
37       #
38       # Parameters:
39       #   id (required) - The ID of a project
40       #   body (required) - The content of a note
41       # Example Request:
42       #   POST /projects/:id/notes
43       post ":id/notes" do
44         required_attributes! [:body]
45
46         @note = user_project.notes.new(note: params[:body])
47         @note.author = current_user
48
49         if @note.save
50           present @note, with: Entities::Note
51         else
52           # :note is exposed as :body, but :note is set on error
53           bad_request!(:note) if @note.errors[:note].any?
54           not_found!
55         end
56       end
57
58       NOTEABLE_TYPES.each do |noteable_type|
59         noteables_str = noteable_type.to_s.underscore.pluralize
60         noteable_id_str = "#{noteable_type.to_s.underscore}_id"
61
62         # Get a list of project +noteable+ notes
63         #
64         # Parameters:
65         #   id (required) - The ID of a project
66         #   noteable_id (required) - The ID of an issue or snippet
67         # Example Request:
68         #   GET /projects/:id/issues/:noteable_id/notes
69         #   GET /projects/:id/snippets/:noteable_id/notes
70         get ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
71           @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
72           present paginate(@noteable.notes), with: Entities::Note
73         end
74
75         # Get a single +noteable+ note
76         #
77         # Parameters:
78         #   id (required) - The ID of a project
79         #   noteable_id (required) - The ID of an issue or snippet
80         #   note_id (required) - The ID of a note
81         # Example Request:
82         #   GET /projects/:id/issues/:noteable_id/notes/:note_id
83         #   GET /projects/:id/snippets/:noteable_id/notes/:note_id
84         get ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
85           @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
86           @note = @noteable.notes.find(params[:note_id])
87           present @note, with: Entities::Note
88         end
89
90         # Create a new +noteable+ note
91         #
92         # Parameters:
93         #   id (required) - The ID of a project
94         #   noteable_id (required) - The ID of an issue or snippet
95         #   body (required) - The content of a note
96         # Example Request:
97         #   POST /projects/:id/issues/:noteable_id/notes
98         #   POST /projects/:id/snippets/:noteable_id/notes
99         post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
100           required_attributes! [:body]
101
102           @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
103           @note = @noteable.notes.new(note: params[:body])
104           @note.author = current_user
105           @note.project = user_project
106
107           if @note.save
108             present @note, with: Entities::Note
109           else
110             not_found!
111           end
112         end
113       end
114     end
115   end
116 end