todos.rb 3.1 KB
Newer Older
1 2
# frozen_string_literal: true

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
3 4
module API
  class Todos < Grape::API
5 6
    include PaginationParams

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
7 8
    before { authenticate! }

9 10
    helpers ::Gitlab::IssuableMetadata

11
    ISSUABLE_TYPES = {
12 13
      'merge_requests' => ->(iid) { find_merge_request_with_access(iid) },
      'issues' => ->(iid) { find_project_issue(iid) }
Douwe Maan's avatar
Douwe Maan committed
14
    }.freeze
15

16 17 18 19 20 21 22
    helpers do
      # EE::API::Todos would override this method
      def find_todos
        TodosFinder.new(current_user, params).execute
      end
    end

Robert Schilling's avatar
Robert Schilling committed
23 24 25
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
26
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
27
      ISSUABLE_TYPES.each do |type, finder|
28
        type_id_str = "#{type.singularize}_iid".to_sym
29

Robert Schilling's avatar
Robert Schilling committed
30 31 32 33
        desc 'Create a todo on an issuable' do
          success Entities::Todo
        end
        params do
34
          requires type_id_str, type: Integer, desc: 'The IID of an issuable'
Robert Schilling's avatar
Robert Schilling committed
35
        end
36 37 38 39 40 41 42 43 44 45 46 47 48
        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

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
49 50
    resource :todos do
      helpers do
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        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)
71
            options[type] = { issuable_metadata: issuable_meta_data(targets, type, current_user) }
72 73
          end
        end
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
74 75
      end

Robert Schilling's avatar
Robert Schilling committed
76 77 78
      desc 'Get a todo list' do
        success Entities::Todo
      end
79 80 81
      params do
        use :pagination
      end
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
82
      get do
83
        todos = paginate(find_todos.with_entity_associations)
84 85 86 87
        options = { with: Entities::Todo, current_user: current_user }
        batch_load_issuable_metadata(todos, options)

        present todos, options
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
88 89
      end

Robert Schilling's avatar
Robert Schilling committed
90 91 92 93 94 95
      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
96
      post ':id/mark_as_done' do
97
        TodoService.new.mark_todos_as_done_by_ids(params[:id], current_user)
98
        todo = current_user.todos.find(params[:id])
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
99

100
        present todo, with: Entities::Todo, current_user: current_user
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
101 102
      end

Robert Schilling's avatar
Robert Schilling committed
103
      desc 'Mark all todos as done'
104
      post '/mark_as_done' do
105
        todos = find_todos
106
        TodoService.new.mark_todos_as_done(todos, current_user)
107 108

        no_content!
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
109 110 111 112
      end
    end
  end
end
113 114

API::Todos.prepend_if_ee('EE::API::Todos')