2013-05-14 12:33:31 +00:00
|
|
|
module API
|
2012-11-27 19:43:39 +00:00
|
|
|
# Notes API
|
|
|
|
class Notes < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
2013-01-30 05:15:13 +00:00
|
|
|
NOTEABLE_TYPES = [Issue, MergeRequest, Snippet]
|
2012-11-27 19:43:39 +00:00
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
# Get a list of project wall notes
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 17:47:04 +00:00
|
|
|
# id (required) - The ID of a project
|
2012-11-27 19:43:39 +00:00
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/notes
|
|
|
|
get ":id/notes" do
|
2013-01-04 16:50:31 +00:00
|
|
|
@notes = user_project.notes.common
|
2013-03-19 10:35:55 +00:00
|
|
|
|
|
|
|
# Get recent notes if recent = true
|
|
|
|
@notes = @notes.order('id DESC') if params[:recent]
|
|
|
|
|
2012-11-27 19:43:39 +00:00
|
|
|
present paginate(@notes), with: Entities::Note
|
|
|
|
end
|
|
|
|
|
2012-12-01 10:20:45 +00:00
|
|
|
# Get a single project wall note
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 17:47:04 +00:00
|
|
|
# id (required) - The ID of a project
|
2012-12-01 10:20:45 +00:00
|
|
|
# note_id (required) - The ID of a note
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/notes/:note_id
|
|
|
|
get ":id/notes/:note_id" do
|
2013-01-04 16:50:31 +00:00
|
|
|
@note = user_project.notes.common.find(params[:note_id])
|
2012-12-01 10:20:45 +00:00
|
|
|
present @note, with: Entities::Note
|
|
|
|
end
|
|
|
|
|
2012-11-29 23:52:56 +00:00
|
|
|
# Create a new project wall note
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 17:47:04 +00:00
|
|
|
# id (required) - The ID of a project
|
2012-11-29 23:52:56 +00:00
|
|
|
# body (required) - The content of a note
|
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/notes
|
|
|
|
post ":id/notes" do
|
2013-02-27 16:50:30 +00:00
|
|
|
required_attributes! [:body]
|
2013-02-20 21:17:05 +00:00
|
|
|
|
2012-11-29 23:52:56 +00:00
|
|
|
@note = user_project.notes.new(note: params[:body])
|
|
|
|
@note.author = current_user
|
|
|
|
|
|
|
|
if @note.save
|
|
|
|
present @note, with: Entities::Note
|
|
|
|
else
|
2013-02-06 15:34:06 +00:00
|
|
|
# :note is exposed as :body, but :note is set on error
|
2013-02-13 14:48:52 +00:00
|
|
|
bad_request!(:note) if @note.errors[:note].any?
|
2012-11-29 23:52:56 +00:00
|
|
|
not_found!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-27 19:43:39 +00:00
|
|
|
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:
|
2012-12-21 17:47:04 +00:00
|
|
|
# id (required) - The ID of a project
|
2012-11-27 19:43:39 +00:00
|
|
|
# noteable_id (required) - The ID of an issue or snippet
|
|
|
|
# Example Request:
|
2012-11-29 19:21:14 +00:00
|
|
|
# GET /projects/:id/issues/:noteable_id/notes
|
|
|
|
# GET /projects/:id/snippets/:noteable_id/notes
|
2012-11-27 19:43:39 +00:00
|
|
|
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
|
2012-11-29 19:33:41 +00:00
|
|
|
|
|
|
|
# Get a single +noteable+ note
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 17:47:04 +00:00
|
|
|
# id (required) - The ID of a project
|
2012-11-29 19:33:41 +00:00
|
|
|
# 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
|
2012-11-29 20:06:24 +00:00
|
|
|
|
|
|
|
# Create a new +noteable+ note
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 17:47:04 +00:00
|
|
|
# id (required) - The ID of a project
|
2012-11-29 20:06:24 +00:00
|
|
|
# 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
|
2013-02-27 17:12:02 +00:00
|
|
|
required_attributes! [:body]
|
2013-02-20 21:17:05 +00:00
|
|
|
|
2012-11-29 20:06:24 +00:00
|
|
|
@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
|
|
|
|
|
|
|
|
if @note.save
|
|
|
|
present @note, with: Entities::Note
|
|
|
|
else
|
|
|
|
not_found!
|
|
|
|
end
|
|
|
|
end
|
2012-11-27 19:43:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|