Class: TaskManager::Api::V1::PlansController

Inherits:
TaskManager::ApplicationController show all
Defined in:
app/controllers/task_manager/api/v1/plans_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

创建计划

Examples:

# 请求
POST /api/plans HTTP/1.1
Accept: application/vnd.menglifang.com.cn; version=1

{
  "plan": {
    "name": ...,                     # 必填且唯一
    "plan_type": ...,                # 必填,有效取值:'daily',
                                     # 'weekly', 'monthly',
                                     # 'quarterly', 'yearly'
    "data": {
      "x": ...,                      # 可选
      "y": ...,                      # 可选
      "deadline_month": ...,         # 当"plan_type"为'yearly'或
                                     #  'quarterly'时,为必填项;
                                     #  否则,为null
      "deadline_day": ...,           # 当"plan_type"为'monthly'或
                                     #  'weekly'时,为必填项;
                                     #  否则,为null
      "deadline_hour": ...,          # 必填
      "deadline_minute": ...         # 必填
    },
    "enabled_at": ...,               # 必填
    "begin_to_remind": ...,          # 必填且大于等于0
    "autocompletable": ...,          # 必填,缺省值为false
    "assignables_attributes": [{     # 至少需要一个assignable
      "assignee_id": ...,
      "assignee_type": ...
    }, ...],

    "callables_attributes": [{       # 可选
      "callback_id": ...,
      "callback_type": ...
    }]
  }
}

# 响应
## 成功
HTTP/1.1 201 Created
{
  "plan": {
    "id": ...,
    "name": ...,
    "plan_type": ...,
    "data": ...,
    "enabled_at": ...,
    "begin_to_remind": ...,
    "autocompletable": ...,
    "created_at": ...,
    "updated_at": ...,
    "assignees": [{
      "id": ...,
      "name": ...
    }, ...]
  }
}
## 失败
HTTP/1.1 422 Unprocessable Entity
{
  "errors": {
    "name": [...],
    ...
  }
}


128
129
130
131
132
133
134
135
136
# File 'app/controllers/task_manager/api/v1/plans_controller.rb', line 128

def create
  plan = TaskManager::Plan.new(params[:plan])

  if plan.save
    render json: plan, status: :created
  else
    render json: { errors: plan.errors }, status: :unprocessable_entity
  end
end

#destroyObject

删除计划

Examples:

# 请求
DELETE /api/plans/... HTTP/1.1
Accept: application/vnd.menglifang.com.cn; version=1

# 响应
## 成功
HTTP/1.1 204 No Content


238
239
240
241
242
# File 'app/controllers/task_manager/api/v1/plans_controller.rb', line 238

def destroy
  plan.destroy

  head :no_content
end

#indexObject

查询计划

支持的查询属性有:

name                      计划名
plan_type                 计划周期
autocompletable           是否自动完成
last_task_created_at      最后任务生成时间
enabled_at                生效时间

支持的查询操作参见 github.com/ernie/ransack/wiki/Basic-Searching

分页查询参数:

page    请求的页码,缺省值1
limit   每页记录数,缺省值25

Examples:

# 请求
GET /api/plans?q[name_cont]=... HTTP/1.1
Accept: application/vnd.menglifang.com.cn; version=1

# 响应
HTTP/1.1 200 OK
{
  "total": ...,
  "plans": [{
    "id": ...,
    "name": ...,
    "data": ...,
    "plan_type": ...,
    "enabled_at": ...,
    "last_task_created_at": ...,
    "begin_to_remind": ...,
    "autocompletable": ...,
    "created_at": ...,
    "updated_at": ...,
    "assignees": [{
      "id": ...,
      "name": ...
    }, ...]
  }, ...]
}


49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/task_manager/api/v1/plans_controller.rb', line 49

def index
  plans = TaskManager::Plan.page(params[:page]).per(params[:limit]).
    order('id DESC').search(params[:q]).result
  result = {
    total: plans.total_count,
    plans: ActiveModel::ArraySerializer.new(plans).as_json
  }

  render json: result, status: :ok
end

#updateObject

更新计划

Examples:

# 请求
PUT /api/plans/... HTTP/1.1
Accept: application/vnd.menglifang.com.cn; version=1

{
  "plan": {
    "name": ...,                     # 必填且唯一
    "plan_type": ...,                # 必填,有效取值:'daily',
                                     # 'weekly', 'monthly',
                                     # 'quarterly', 'yearly'
    "data": {
      "x": ...,                      # 可选
      "y": ...,                      # 可选
      "deadline_month": ...,         # 当"plan_type"为'yearly'或
                                     #  'quarterly'时,为必填项;
                                     #  否则,为null
      "deadline_day": ...,           # 当"plan_type"为'monthly'或
                                     #  'weekly'时,为必填项;
                                     #  否则,为null
      "deadline_hour": ...,          # 必填
      "deadline_minute": ...         # 必填
    },
    "enabled_at": ...,               # 必填
    "begin_to_remind": ...,          # 必填且大于等于0
    "autocompletable": ...,          # 必填,缺省值为false

    "assignables_attributes": [{
      "id": ...,                     ## 可选,
                                     ## 如果有,则更新,否则创建
      "assignee_id": ...,          ## 必填
      "assignee_type": ...,        ## 必填
      "_destroy": "1"                ## 可选,
                                     ## 如果设置,存在则删除,
                                     ## 不存在则忽略
    }, ...],

    "callables_attributes": [{       # 可选
      "id": ...,                     ## 可选,
                                     ## 如果有,则更新,否则创建
      "callback_id": ...,            ## 必填
      "callback_type": ...,          ## 必填
      "_destroy": "1"                ## 可选,
                                     ## 如果设置,存在则删除,
                                     ## 不存在则忽略
    }]
  }
}

# 响应
## 成功
HTTP/1.1 202 Accepted
{
  "plan": {
    "id": ...,
    "name": ...,
    "plan_type": ...,
    "data": ...,
    "enabled_at": ...,
    "begin_to_remind": ...,
    "autocompletable": ...,
    "created_at": ...,
    "updated_at": ...,
    "assignees": [{
      "id": ...,
      "name": ...
    }, ...]
  }
}
## 失败
HTTP/1.1 422 Unprocessable Entity
{
  "errors": {
    "name": [...],
    ...
  }
}


217
218
219
220
221
222
223
224
225
226
# File 'app/controllers/task_manager/api/v1/plans_controller.rb', line 217

def update
  plan.assignables.destroy_all
  plan.callables.destroy_all

  if plan.update_attributes(params[:plan])
    render json: plan, status: :ok
  else
    render json: { errors: plan.errors }, status: :unprocessable_entity
  end
end