iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 27
0

上次成功創建通用的檢查函式,這次我們繼續寫下面的get_list, get_once部分,首先我們一樣打開controllers/Api.php,接著我們針對to_work_get_list做新增:

// 取得特定多筆資料
function to_work_get_list($path_name, $func_name, $data) {

}

接著跟先前一樣,取得設定檔案:

$methods = $this->config->item('routes_table');
$table = $methods[$path_name]['table'];
$info = $methods[$path_name]['detals'][$func_name];

接著驗證資源:

// 驗證需要的元素
$required = array();
foreach ($info['require'] as $keyRequired => $valueRequired) {
  if (!isset($data[$valueRequired])) array_push($required, $valueRequired);
}
// 如果缺少資源跳出
if (sizeof($required) > 0) {
  $json_arr = $this->mod_config->msgResponse((isset($json_arr))?$json_arr:array(), 'error', 'DATA_FAIL', $this->language);
  $json_arr['requred'] = $required;
  return $json_arr;
}

下一步就是資料庫的驗證:

// 負責驗證的元素
$verify = array();
foreach ($info['verify'] as $key => $value) {
  if (isset($data[$value])) {
    if ($key == '_id') {
      $verify[$key] = new MongoId($data[$value]);
    } else {
      $verify[$key] = $data[$value];
    }
  }
}

接著寫入可能的關鍵字搜尋:

// 負責寫入關鍵字元素內容
$likes = array();
foreach ($info['likes'] as $key => $value) {
  if (isset($data[$key])) $likes[$key] = $data[$key];
}

另外在加寫一些限制元素(where_in, where_ne):

// 負責寫入限制元素
$record = array();
foreach ($info['record'] as $key => $value) {
  if (isset($data[$key])) $record[$key] = $data[$key];
}

下一步加入限制抓取數量:

// 確認資料數量 預設抓取 30 筆資料
if (!isset($record['limit'])) {
  $record['limit'] = 30;
}

再來是如果有分頁,就確認分頁量:

// 確認頁數
if (!isset($record['page'])) {
  $record['page'] = 1;
}

最後我們在呼叫通用函式去查詢資料再把結果回傳回去:

// 加入邏輯佇列
$dataQuery['verify'] = $verify;
$dataQuery['likes'] = $likes;
$dataQuery['record'] = $record;
$dataQuery['table'] = $table;
$response = $this->mod_universal->get_list($dataQuery);
$json_arr['response'] = $response;
$json_arr = $this->mod_config->msgResponse((isset($json_arr))?$json_arr:array(), 'success', 'GET_DATA_SUCCESS', $this->language);
return $json_arr;

完整程式:

// 取得特定多筆資料
function to_work_get_list($path_name, $func_name, $data) {
  $methods = $this->config->item('routes_table');
  $table = $methods[$path_name]['table'];
  $info = $methods[$path_name]['detals'][$func_name];

  // 驗證需要的元素
  $required = array();
  foreach ($info['require'] as $keyRequired => $valueRequired) {
    if (!isset($data[$valueRequired])) array_push($required, $valueRequired);
  }
  // 如果缺少資源跳出
  if (sizeof($required) > 0) {
    $json_arr = $this->mod_config->msgResponse((isset($json_arr))?$json_arr:array(), 'error', 'DATA_FAIL', $this->language);
    $json_arr['requred'] = $required;
    return $json_arr;
  }

  // 負責驗證的元素
  $verify = array();
  foreach ($info['verify'] as $key => $value) {
    if (isset($data[$value])) {
      if ($key == '_id') {
        $verify[$key] = new MongoId($data[$value]);
      } else {
        $verify[$key] = $data[$value];
      }
    }
  }

  // 負責寫入關鍵字元素內容
  $likes = array();
  foreach ($info['likes'] as $key => $value) {
    if (isset($data[$key])) $likes[$key] = $data[$key];
  }

  // 負責寫入限制元素
  $record = array();
  foreach ($info['record'] as $key => $value) {
    if (isset($data[$key])) $record[$key] = $data[$key];
  }

  // 確認資料數量 預設抓取 30 筆資料
  if (!isset($record['limit'])) {
    $record['limit'] = 30;
  }

  // 確認頁數
  if (!isset($record['page'])) {
    $record['page'] = 1;
  }

  // 加入邏輯佇列
  $dataQuery['verify'] = $verify;
  $dataQuery['likes'] = $likes;
  $dataQuery['record'] = $record;
  $dataQuery['table'] = $table;
  $response = $this->mod_universal->get_list($dataQuery);
  $json_arr['response'] = $response;
  $json_arr = $this->mod_config->msgResponse((isset($json_arr))?$json_arr:array(), 'success', 'GET_DATA_SUCCESS', $this->language);
  return $json_arr;
}

如此一來就大功告成,接著get_once效果也是一樣,差別在於去除限制數量與分頁,再來通用函式的不同:

// 取得特定單筆資料
function to_work_get_once($path_name, $func_name, $data) {
  $methods = $this->config->item('routes_table');
  $table = $methods[$path_name]['table'];
  $info = $methods[$path_name]['detals'][$func_name];

  // 驗證需要的元素
  $required = array();
  foreach ($info['require'] as $keyRequired => $valueRequired) {
    if (!isset($data[$valueRequired])) array_push($required, $valueRequired);
  }
  // 如果缺少資源跳出
  if (sizeof($required) > 0) {
    $json_arr = $this->mod_config->msgResponse((isset($json_arr))?$json_arr:array(), 'error', 'DATA_FAIL', $this->language);
    $json_arr['requred'] = $required;
    return $json_arr;
  }

  // 負責驗證的元素
  $verify = array();
  foreach ($info['verify'] as $key => $value) {
    if (isset($data[$value])) {
      if ($key == '_id') {
        $verify[$key] = new MongoId($data[$value]);
      } else {
        $verify[$key] = $data[$value];
      }
    }
  }

  // 負責寫入關鍵字元素內容
  $likes = array();
  foreach ($info['likes'] as $key => $value) {
    if (isset($data[$key])) $likes[$key] = $data[$key];
  }

  // 負責寫入限制元素
  $record = array();
  foreach ($info['record'] as $key => $value) {
    if (isset($data[$key])) $record[$key] = $data[$key];
  }

  // 加入邏輯佇列
  $dataQuery['verify'] = $verify;
  $dataQuery['likes'] = $likes;
  $dataQuery['record'] = $record;
  $dataQuery['table'] = $table;
  $response = $this->mod_universal->get_once($dataQuery);
  $json_arr['response'] = $response;
  $json_arr = $this->mod_config->msgResponse((isset($json_arr))?$json_arr:array(), 'success', 'GET_DATA_SUCCESS', $this->language);
  return $json_arr;
}

如此一來就把get_list,get_once也完成了,下一章在接續insert,update,delete

Next station ... RESTful API


上一篇
[Day26] RESTful API Part3
下一篇
[Day28] RESTful API Part5
系列文
你說Codeigniter還能怎麼做?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言