summaryrefslogtreecommitdiff
path: root/lib/api/award_emoji.rb
diff options
context:
space:
mode:
authorZ.J. van de Weg <zegerjan@gitlab.com>2016-06-08 09:33:41 +0200
committerZ.J. van de Weg <zegerjan@gitlab.com>2016-06-17 20:07:17 +0200
commit3f88221c2dcb1c42cc2f5a765d2586f1755128c3 (patch)
tree80f246dc0c3bbc848225cfbbea973c69bad44c1e /lib/api/award_emoji.rb
parent6c37e0f61774e4f0c7dff7830055233b3ab2726b (diff)
downloadgitlab-ce-3f88221c2dcb1c42cc2f5a765d2586f1755128c3.tar.gz
Add endpoints for Award Emoji
This only supports Issues and MergeRequests right now because of the consistency of the routes those models provide.
Diffstat (limited to 'lib/api/award_emoji.rb')
-rw-r--r--lib/api/award_emoji.rb97
1 files changed, 97 insertions, 0 deletions
diff --git a/lib/api/award_emoji.rb b/lib/api/award_emoji.rb
new file mode 100644
index 00000000000..26b30d30163
--- /dev/null
+++ b/lib/api/award_emoji.rb
@@ -0,0 +1,97 @@
+module API
+ class AwardEmoji < Grape::API
+ before { authenticate! }
+
+ AWARDABLES = [Issue, MergeRequest]
+
+ resource :projects do
+ AWARDABLES.each do |awardable_type|
+ awardable_string = awardable_type.to_s.underscore.pluralize
+ awardable_id_string = "#{awardable_type.to_s.underscore}_id"
+
+ # Get a list of project +awardable+ award emoji
+ #
+ # Parameters:
+ # id (required) - The ID of a project
+ # awardable_id (required) - The ID of an issue or MR
+ # Example Request:
+ # GET /projects/:id/issues/:awardable_id/award_emoji
+ get ":id/#{awardable_string}/:#{awardable_id_string}/award_emoji" do
+ awardable = user_project.send(awardable_string.to_sym).find(params[awardable_id_string.to_sym])
+
+ if can?(current_user, awardable_read_ability_name(awardable), awardable)
+ awards = paginate(awardable.award_emoji)
+ present awards, with: Entities::AwardEmoji
+ else
+ not_found!("Award Emoji")
+ end
+ end
+
+ # Get a specific award emoji
+ #
+ # Parameters:
+ # id (required) - The ID of a project
+ # awardable_id (required) - The ID of an issue or MR
+ # award_id (required) - The ID of the award
+ # Example Request:
+ # GET /projects/:id/issues/:awardable_id/award_emoji/:award_id
+ get ":id/#{awardable_string}/:#{awardable_id_string}/award_emoji/:award_id" do
+ awardable = user_project.send(awardable_string.to_sym).find(params[awardable_id_string.to_sym])
+
+ if can?(current_user, awardable_read_ability_name(awardable), awardable)
+ present awardable.award_emoji.find(params[:award_id]), with: Entities::AwardEmoji
+ else
+ not_found!("Award Emoji")
+ end
+ end
+
+ # Award a new Emoji
+ #
+ # Parameters:
+ # id (required) - The ID of a project
+ # noteable_id (required) - The ID of an issue or snippet
+ # name (required) - The name of a award_emoji (without colons)
+ # Example Request:
+ # POST /projects/:id/issues/:noteable_id/notes
+ # POST /projects/:id/snippets/:noteable_id/notes
+ post ":id/#{awardable_string}/:#{awardable_id_string}/award_emoji" do
+ required_attributes! [:name]
+
+ awardable = user_project.send(awardable_string.to_sym).find(params[awardable_id_string.to_sym])
+ not_found!('Award Emoji') unless can?(current_user, awardable_read_ability_name(awardable), awardable)
+
+ award = awardable.award_emoji.new(name: params[:name], user: current_user)
+
+ if award.save
+ present award, with: Entities::AwardEmoji
+ else
+ not_found!("Award Emoji #{award.errors.messages}")
+ end
+ end
+
+ # Delete a +awardables+ award emoji
+ #
+ # Parameters:
+ # id (required) - The ID of a project
+ # awardable_id (required) - The ID of an issue or MR
+ # award_emoji_id (required) - The ID of an award emoji
+ # Example Request:
+ # DELETE /projects/:id/issues/:noteable_id/notes/:note_id
+ delete ":id/#{awardable_string}/:#{awardable_id_string}/award_emoji/:award_id" do
+ awardable = user_project.send(awardable_string.to_sym).find(params[awardable_id_string.to_sym])
+ award = awardable.award_emoji.find(params[:award_id])
+
+ unauthorized! unless award.user == current_user || current_user.admin?
+
+ award.destroy
+ present award, with: Entities::AwardEmoji
+ end
+ end
+ end
+ helpers do
+ def awardable_read_ability_name(awardable)
+ "read_#{awardable.class.to_s.underscore.downcase}".to_sym
+ end
+ end
+ end
+end