2016-03-11 14:04:42 -05:00
|
|
|
module API
|
|
|
|
class Todos < Grape::API
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-03-11 14:04:42 -05:00
|
|
|
before { authenticate! }
|
|
|
|
|
2016-06-28 12:04:44 -04:00
|
|
|
ISSUABLE_TYPES = {
|
|
|
|
'merge_requests' => ->(id) { user_project.merge_requests.find(id) },
|
|
|
|
'issues' => ->(id) { find_project_issue(id) }
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2016-06-28 12:04:44 -04:00
|
|
|
resource :projects do
|
|
|
|
ISSUABLE_TYPES.each do |type, finder|
|
|
|
|
type_id_str = "#{type.singularize}_id".to_sym
|
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
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-06-28 12:04:44 -04:00
|
|
|
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
|
|
|
|
|
2016-03-11 14:04:42 -05:00
|
|
|
resource :todos do
|
|
|
|
helpers do
|
|
|
|
def find_todos
|
|
|
|
TodosFinder.new(current_user, params).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
desc 'Get a todo list' do
|
|
|
|
success Entities::Todo
|
|
|
|
end
|
2016-12-04 12:11:19 -05:00
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2016-03-11 14:04:42 -05:00
|
|
|
get do
|
2016-12-04 12:11:19 -05:00
|
|
|
present paginate(find_todos), with: Entities::Todo, current_user: current_user
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
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-03-11 14:04:42 -05:00
|
|
|
delete ':id' do
|
2016-05-21 13:01:11 -04:00
|
|
|
todo = current_user.todos.find(params[:id])
|
2016-08-11 12:39:50 -04:00
|
|
|
TodoService.new.mark_todos_as_done([todo], current_user)
|
2016-03-11 14:04:42 -05:00
|
|
|
|
2016-08-11 12:39:50 -04:00
|
|
|
present todo.reload, with: Entities::Todo, current_user: current_user
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
desc 'Mark all todos as done'
|
2016-03-11 14:04:42 -05:00
|
|
|
delete do
|
2016-05-21 13:01:11 -04:00
|
|
|
todos = find_todos
|
2016-07-08 12:42:47 -04:00
|
|
|
TodoService.new.mark_todos_as_done(todos, current_user)
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|