2016-03-11 14:04:42 -05:00
|
|
|
module API
|
|
|
|
# Todos API
|
|
|
|
class Todos < Grape::API
|
|
|
|
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) }
|
|
|
|
}
|
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
ISSUABLE_TYPES.each do |type, finder|
|
|
|
|
type_id_str = "#{type.singularize}_id".to_sym
|
|
|
|
|
|
|
|
# Create a todo on an issuable
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# issuable_id (required) - The ID of an issuable
|
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/issues/:issuable_id/todo
|
|
|
|
# POST /projects/:id/merge_requests/:issuable_id/todo
|
|
|
|
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
|
|
|
|
|
|
|
|
# Get a todo list
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /todos
|
2016-05-21 13:01:11 -04:00
|
|
|
#
|
2016-03-11 14:04:42 -05:00
|
|
|
get do
|
2016-05-21 13:01:11 -04:00
|
|
|
todos = find_todos
|
2016-03-11 14:04:42 -05:00
|
|
|
|
2016-06-22 13:15:09 -04:00
|
|
|
present paginate(todos), with: Entities::Todo, current_user: current_user
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
|
2016-06-16 05:12:42 -04:00
|
|
|
# Mark a todo as done
|
2016-03-11 14:04:42 -05:00
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id: (required) - The ID of the todo being marked as done
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /todos/:id
|
|
|
|
#
|
|
|
|
delete ':id' do
|
2016-05-21 13:01:11 -04:00
|
|
|
todo = current_user.todos.find(params[:id])
|
|
|
|
todo.done
|
2016-03-11 14:04:42 -05:00
|
|
|
|
2016-06-22 13:15:09 -04:00
|
|
|
present todo, with: Entities::Todo, current_user: current_user
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Mark all todos as done
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /todos
|
|
|
|
#
|
|
|
|
delete do
|
2016-05-21 13:01:11 -04:00
|
|
|
todos = find_todos
|
|
|
|
todos.each(&:done)
|
2016-03-11 14:04:42 -05:00
|
|
|
|
2016-06-22 13:15:09 -04:00
|
|
|
present paginate(Kaminari.paginate_array(todos)), with: Entities::Todo, current_user: current_user
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|