BigW Consortium Gitlab

todos.rb 2.04 KB
Newer Older
1 2
module API
  class Todos < Grape::API
3 4
    include PaginationParams

5 6
    before { authenticate! }

7
    ISSUABLE_TYPES = {
8 9
      'merge_requests' => ->(iid) { find_merge_request_with_access(iid) },
      'issues' => ->(iid) { find_project_issue(iid) }
10
    }.freeze
11

Robert Schilling committed
12 13 14
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
15
    resource :projects, requirements: { id: %r{[^/]+} } do
16
      ISSUABLE_TYPES.each do |type, finder|
17
        type_id_str = "#{type.singularize}_iid".to_sym
18

Robert Schilling committed
19 20 21 22
        desc 'Create a todo on an issuable' do
          success Entities::Todo
        end
        params do
23
          requires type_id_str, type: Integer, desc: 'The IID of an issuable'
Robert Schilling committed
24
        end
25 26 27 28 29 30 31 32 33 34 35 36 37
        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

38 39 40 41 42 43 44
    resource :todos do
      helpers do
        def find_todos
          TodosFinder.new(current_user, params).execute
        end
      end

Robert Schilling committed
45 46 47
      desc 'Get a todo list' do
        success Entities::Todo
      end
48 49 50
      params do
        use :pagination
      end
51
      get do
52
        present paginate(find_todos), with: Entities::Todo, current_user: current_user
53 54
      end

Robert Schilling committed
55 56 57 58 59 60
      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
61
      post ':id/mark_as_done' do
62
        todo = current_user.todos.find(params[:id])
63
        TodoService.new.mark_todos_as_done([todo], current_user)
64

65
        present todo.reload, with: Entities::Todo, current_user: current_user
66 67
      end

Robert Schilling committed
68
      desc 'Mark all todos as done'
69
      post '/mark_as_done' do
70
        todos = find_todos
71
        TodoService.new.mark_todos_as_done(todos, current_user)
72 73

        no_content!
74 75 76 77
      end
    end
  end
end