debian-mirror-gitlab/lib/api/todos.rb

110 lines
3 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
module API
class Todos < Grape::API
2017-08-17 22:00:37 +05:30
include PaginationParams
2016-08-24 12:49:21 +05:30
before { authenticate! }
2019-05-18 00:54:41 +05:30
helpers ::Gitlab::IssuableMetadata
2016-08-24 12:49:21 +05:30
ISSUABLE_TYPES = {
2017-08-17 22:00:37 +05:30
'merge_requests' => ->(iid) { find_merge_request_with_access(iid) },
'issues' => ->(iid) { find_project_issue(iid) }
}.freeze
2016-08-24 12:49:21 +05:30
2016-11-03 12:29:30 +05:30
params do
requires :id, type: String, desc: 'The ID of a project'
end
2019-03-02 22:35:43 +05:30
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2016-08-24 12:49:21 +05:30
ISSUABLE_TYPES.each do |type, finder|
2017-08-17 22:00:37 +05:30
type_id_str = "#{type.singularize}_iid".to_sym
2016-08-24 12:49:21 +05:30
2016-11-03 12:29:30 +05:30
desc 'Create a todo on an issuable' do
success Entities::Todo
end
params do
2017-08-17 22:00:37 +05:30
requires type_id_str, type: Integer, desc: 'The IID of an issuable'
2016-11-03 12:29:30 +05:30
end
2016-08-24 12:49:21 +05:30
post ":id/#{type}/:#{type_id_str}/todo" do
issuable = instance_exec(params[type_id_str], &finder)
todo = TodoService.new.mark_todo(issuable, current_user).first
if todo
present todo, with: Entities::Todo, current_user: current_user
else
not_modified!
end
end
end
end
resource :todos do
helpers do
def find_todos
TodosFinder.new(current_user, params).execute
end
2019-05-18 00:54:41 +05:30
def issuable_and_awardable?(type)
obj_type = Object.const_get(type)
(obj_type < Issuable) && (obj_type < Awardable)
rescue NameError
false
end
def batch_load_issuable_metadata(todos, options)
# This should be paginated and will cause Rails to SELECT for all the Todos
todos_by_type = todos.group_by(&:target_type)
todos_by_type.keys.each do |type|
next unless issuable_and_awardable?(type)
collection = todos_by_type[type]
next unless collection
targets = collection.map(&:target)
options[type] = { issuable_metadata: issuable_meta_data(targets, type) }
end
end
2016-08-24 12:49:21 +05:30
end
2016-11-03 12:29:30 +05:30
desc 'Get a todo list' do
success Entities::Todo
end
2017-08-17 22:00:37 +05:30
params do
use :pagination
end
2016-08-24 12:49:21 +05:30
get do
2019-05-18 00:54:41 +05:30
todos = paginate(find_todos.with_api_entity_associations)
options = { with: Entities::Todo, current_user: current_user }
batch_load_issuable_metadata(todos, options)
present todos, options
2016-08-24 12:49:21 +05:30
end
2016-11-03 12:29:30 +05:30
desc 'Mark a todo as done' do
success Entities::Todo
end
params do
requires :id, type: Integer, desc: 'The ID of the todo being marked as done'
end
2017-08-17 22:00:37 +05:30
post ':id/mark_as_done' do
2017-09-10 17:25:29 +05:30
TodoService.new.mark_todos_as_done_by_ids(params[:id], current_user)
2018-03-17 18:26:18 +05:30
todo = current_user.todos.find(params[:id])
2016-08-24 12:49:21 +05:30
2017-09-10 17:25:29 +05:30
present todo, with: Entities::Todo, current_user: current_user
2016-08-24 12:49:21 +05:30
end
2016-11-03 12:29:30 +05:30
desc 'Mark all todos as done'
2017-08-17 22:00:37 +05:30
post '/mark_as_done' do
2016-08-24 12:49:21 +05:30
todos = find_todos
2016-09-13 17:45:13 +05:30
TodoService.new.mark_todos_as_done(todos, current_user)
2017-08-17 22:00:37 +05:30
no_content!
2016-08-24 12:49:21 +05:30
end
end
end
end