Commit 822883b0 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Improve API descriptions and finder call

Simplifies the finder call using FinderMethods so that we don't have
to use the finder just for the auth check
parent e9169cde
# frozen_string_literal: true # frozen_string_literal: true
class ResourceStateEventFinder class ResourceStateEventFinder
include FinderMethods
def initialize(current_user, eventable) def initialize(current_user, eventable)
@current_user = current_user @current_user = current_user
@eventable = eventable @eventable = eventable
......
...@@ -7,43 +7,40 @@ module API ...@@ -7,43 +7,40 @@ module API
before { authenticate! } before { authenticate! }
[Issue, MergeRequest].each do |eventable_type| [Issue, MergeRequest].each do |eventable_class|
parent_type = eventable_type.parent_class.to_s.underscore eventable_name = eventable_class.to_s.underscore
eventables_str = eventable_type.to_s.underscore.pluralize
params do params do
requires :id, type: String, desc: "The ID of a #{parent_type}" requires :id, type: String, desc: "The ID of a project"
end end
resource parent_type.pluralize.to_sym, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc "Get a list of #{eventable_type.to_s.downcase} resource state events" do desc "Get a list of #{eventable_class.to_s.downcase} resource state events" do
success Entities::ResourceStateEvent success Entities::ResourceStateEvent
end end
params do params do
requires :eventable_id, types: [Integer, String], desc: 'The ID of the eventable' requires :eventable_iid, types: Integer, desc: "The IID of the #{eventable_name}"
use :pagination use :pagination
end end
get ":id/#{eventables_str}/:eventable_id/resource_state_events" do get ":id/#{eventable_name.pluralize}/:eventable_iid/resource_state_events" do
eventable = find_noteable(eventable_type, params[:eventable_id]) eventable = find_noteable(eventable_class, params[:eventable_iid])
events = ResourceStateEventFinder.new(current_user, eventable).execute events = ResourceStateEventFinder.new(current_user, eventable).execute
present paginate(events), with: Entities::ResourceStateEvent present paginate(events), with: Entities::ResourceStateEvent
end end
desc "Get a single #{eventable_type.to_s.downcase} resource state event" do desc "Get a single #{eventable_class.to_s.downcase} resource state event" do
success Entities::ResourceStateEvent success Entities::ResourceStateEvent
end end
params do params do
requires :event_id, type: String, desc: 'The ID of a resource state event' requires :eventable_iid, types: Integer, desc: "The IID of the #{eventable_name}"
requires :eventable_id, types: [Integer, String], desc: 'The ID of the eventable' requires :event_id, type: Integer, desc: 'The ID of a resource state event'
end end
get ":id/#{eventables_str}/:eventable_id/resource_state_events/:event_id" do get ":id/#{eventable_name.pluralize}/:eventable_iid/resource_state_events/:event_id" do
eventable = find_noteable(eventable_type, params[:eventable_id]) eventable = find_noteable(eventable_class, params[:eventable_iid])
not_found!('ResourceStateEvent') unless ResourceStateEventFinder.new(current_user, eventable).can_read_eventable? event = ResourceStateEventFinder.new(current_user, eventable).find(params[:event_id])
event = eventable.resource_state_events.find(params[:event_id])
present event, with: Entities::ResourceStateEvent present event, with: Entities::ResourceStateEvent
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment