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

74 lines
1.9 KiB
Ruby
Raw Normal View History

2016-08-24 12:49:21 +05:30
module API
# Todos API
class Todos < Grape::API
before { authenticate! }
ISSUABLE_TYPES = {
'merge_requests' => ->(id) { user_project.merge_requests.find(id) },
'issues' => ->(id) { find_project_issue(id) }
}
2016-11-03 12:29:30 +05:30
params do
requires :id, type: String, desc: 'The ID of a project'
end
2016-08-24 12:49:21 +05:30
resource :projects do
ISSUABLE_TYPES.each do |type, finder|
type_id_str = "#{type.singularize}_id".to_sym
2016-11-03 12:29:30 +05:30
desc 'Create a todo on an issuable' do
success Entities::Todo
end
params do
requires type_id_str, type: Integer, desc: 'The ID of an issuable'
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
end
2016-11-03 12:29:30 +05:30
desc 'Get a todo list' do
success Entities::Todo
end
2016-08-24 12:49:21 +05:30
get do
todos = find_todos
present paginate(todos), with: Entities::Todo, current_user: current_user
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
2016-08-24 12:49:21 +05:30
delete ':id' do
todo = current_user.todos.find(params[:id])
2016-09-13 17:45:13 +05:30
TodoService.new.mark_todos_as_done([todo], current_user)
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
present todo.reload, 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'
2016-08-24 12:49:21 +05:30
delete do
todos = find_todos
2016-09-13 17:45:13 +05:30
TodoService.new.mark_todos_as_done(todos, current_user)
2016-08-24 12:49:21 +05:30
end
end
end
end