diff options
Diffstat (limited to 'lib/api/award_emoji.rb')
-rw-r--r-- | lib/api/award_emoji.rb | 151 |
1 files changed, 83 insertions, 68 deletions
diff --git a/lib/api/award_emoji.rb b/lib/api/award_emoji.rb index a7949b9e11d..985590312e3 100644 --- a/lib/api/award_emoji.rb +++ b/lib/api/award_emoji.rb @@ -1,7 +1,6 @@ module API class AwardEmoji < Grape::API before { authenticate! } - AWARDABLES = [Issue, MergeRequest] resource :projects do @@ -9,93 +8,109 @@ module API 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]) + [ ":id/#{awardable_string}/:#{awardable_id_string}/award_emoji", + ":id/#{awardable_string}/:#{awardable_id_string}/notes/:note_id/award_emoji" + ].each do |endpoint| - if can_read_awardable?(awardable) - awards = paginate(awardable.award_emoji) - present awards, with: Entities::AwardEmoji - else - not_found!("Award Emoji") + # 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 endpoint do + if can_read_awardable? + awards = paginate(awardable.award_emoji) + present awards, with: Entities::AwardEmoji + else + not_found!("Award Emoji") + end 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_read_awardable?(awardable) - present awardable.award_emoji.find(params[:award_id]), with: Entities::AwardEmoji - else - not_found!("Award Emoji") + # 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 "#{endpoint}/:award_id" do + if can_read_awardable? + present awardable.award_emoji.find(params[:award_id]), with: Entities::AwardEmoji + else + not_found!("Award Emoji") + end end - end - # Award a new Emoji - # - # Parameters: - # id (required) - The ID of a project - # awardable_id (required) - The ID of an issue or mr - # name (required) - The name of a award_emoji (without colons) - # Example Request: - # POST /projects/:id/issues/:awardable_id/notes - post ":id/#{awardable_string}/:#{awardable_id_string}/award_emoji" do - required_attributes! [:name] + # Award a new Emoji + # + # Parameters: + # id (required) - The ID of a project + # awardable_id (required) - The ID of an issue or mr + # name (required) - The name of a award_emoji (without colons) + # Example Request: + # POST /projects/:id/issues/:awardable_id/award_emoji + post endpoint 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_read_awardable?(awardable) + not_found!('Award Emoji') unless can_read_awardable? - award = awardable.award_emoji.new(name: params[:name], user: current_user) + 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}") + if award.save + present award, with: Entities::AwardEmoji + else + not_found!("Award Emoji #{award.errors.messages}") + end 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]) + # 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/:issue_id/notes/:note_id/award_emoji/:award_id + delete "#{endpoint}/:award_id" do + award = awardable.award_emoji.find(params[:award_id]) - unauthorized! unless award.user == current_user || current_user.admin? + unauthorized! unless award.user == current_user || current_user.admin? - award.destroy - present award, with: Entities::AwardEmoji + award.destroy + present award, with: Entities::AwardEmoji + end end end end - helpers do - def awardable_read_ability_name(awardable) - end - def can_read_awardable?(awardable) + helpers do + def can_read_awardable? ability = "read_#{awardable.class.to_s.underscore}".to_sym can?(current_user, ability, awardable) end + + def awardable + @awardable ||= + begin + if params.include?(:note_id) + noteable.notes.find(params[:note_id]) + else + noteable + end + end + end + + def noteable + if params.include?(:issue_id) + user_project.issues.find(params[:issue_id]) + else + user_project.merge_requests.find(params[:merge_request_id]) + end + end end end end |