- REST ful サービスの利用-
スマートフォン
商品一覧 → 商品詳細
アプリの動き
商品一覧
商品一覧から商品詳細を表示
ソースの解説(クライアント 側)
スマートフォン
restapp.html ... getItem ファンクション
/* 商品の取得 action 0:照会, 1:変更, 2:登録, 3:削除 */
function getItem(code, action){
$.ajax({
type:'GET',
url: SERVER_NAMES+’/rest/items/'+code, async: false,
success: function(msg){
var results = JSON.parse(msg);
if(jQuery.trim(results) !== ""){
$('#frm_code').val(jQuery.trim(results[0].CODE));
... 省略
settingForm(action);
iui.busy = true;
iui.showPageById("frmItem");
}else{
alert("商品コードが違います。");
...省略
return false;
} },
error: function(req, status, errthrow){
alert(errthrow);
} });
}
・GET 要求
・URI
・JSON 操作
ソースの解説(IBM i 側)
REST ful サービス
ItemsController.php ... getAction ファンクション
public function getAction(){
$params = $this->_getAllParams();
$actionid = isset($params['id']) ? $params['id'] : null ; $retValue = "";
if($actionid){
$item = new Item();
switch ($actionid){
case 'search':
$ retValue = $item->getSearchResult($params);
break;
default:
$ retValue = $item->getItem($actionid);
break;
}
$item = null;
}
echo Zend_Json::encode($retValue);
}
・商品コードの取得
・DBからのデータ取得
・JSON形式で戻す
ソースの解説(IBM i 側)
データベースの処理
Item.php ... getItem ファンクション
public function getItem($code){
try{
$select = ' select code, kana, name, kind, price,
sqty, desc, wh, del from ‘ ._LIBNAME . '.item ';
$where = ' where 1=1 ' ;
$where .= " and code = '“ .db2_escape_string($code). "'";
$strSQL = $select . $where;
$result=$this->_db->fetchAll($strSQL);
return $result;
}catch(Exception $e){
error_log('Item:getItem exception = '.$e->getMessage());
}
} ・SQL 文の作成
・SQL実行
・結果の戻し
- REST ful サービスの利用-
スマートフォン
商品の追加
アプリの動き
商品 追加
商品を登録
ソースの解説(クライアント 側)
スマートフォン
restapp.html ... postItem ファンクション
/* 商品の登録 */
function postItem(){
$.ajax({
type:"POST",
url: SERVER_NAMES+”/rest/items/", async: false,
data:$('#frmItem').serialize(), success: function(msg){
var results = JSON.parse(msg);
if(results){
alert("登録が完了しました。");
iui.busy = true;
iui.showPageById("home");
}else{
alert("登録が失敗しました。");
settingForm(2);
iui.busy = true;
iui.showPageById("frmItem");
} },
error: function(req, status, errthrow){
alert(errthrow);
} });
}
・POST 要求
・URI
・JSON 操作
ソースの解説(IBM i 側)
REST ful サービス
ItemsController.php ... postAction ファンクション
public function postAction(){
$params = $this->_getAllParams();
$retValue = "";
$item = new Item();
$retValue = $item->insertItem($params);
$item = null;
echo Zend_Json::encode($retValue);
} ・全データの取得
・DBへのデータ登録
・JSON形式で戻す
ソースの解説(IBM i 側)
データベースの処理
Item.php ... insertItem ファンクション
public function insertItem($params){
try{
$code = $this->getCodeValue();
$kana = isset($params['kana']) ? $params['kana'] : null ; ... 省略
$insert = " insert into “ ._LIBNAME. ".item ";
$values = " values(?,?,?,?,?,?,?,?,?) ";
$strSQL = $insert . $values;
$stmt = $this->_db->prepare($strSQL);
$dbparams = array(
db2_escape_string($code), db2_escape_string($kana), ... 省略
);
$result = $stmt->execute($dbparams);
return $result;
}catch(Exception $e){
error_log('Item:insertItem exception = '.$e->getMessage());
} }
・入力値の取得
・SQL 文の作成
・SQL実行
・結果の戻し
(true/false)
- REST ful サービスの利用-
スマートフォン
商品の変更
アプリの動き
商品 変更
商品を変更
ソースの解説(クライアント 側)
スマートフォン
restapp.html ... putItem ファンクション
/* 商品の変更 */
function putItem(code){
$.ajax({
type:"PUT",
url: SERVER_NAMES+”/rest/items/"+code+"/?"+$('#frmItem').serialize(), async: false,
success: function(msg){
var results = JSON.parse(msg);
if(jQuery.trim(results) !== ""){
alert("更新が完了しました。");
iui.busy = true;
iui.showPageById("home");
}else{
alert("更新に失敗しました。");
settingForm(1);
iui.busy = true;
iui.showPageById("frmItem");
} },
error: function(req, status, errthrow){
alert(errthrow);
} });
}
・PUT 要求
・URI
・JSON 操作
ソースの解説(IBM i 側)
REST ful サービス
ItemsController.php ... putAction ファンクション
public function putAction(){
$params = $this->_getAllParams();
$actionid = isset($params['id']) ? $params['id'] : null ; $retValue = "";
if($actionid){
$item = new Item();
$retValue = $item->updateItem($params);
$item = null;
}
echo Zend_Json::encode($retValue);
} ・全データの取得
・商品コードの取得
・DBへのデータ更新
・JSON形式で戻す
ソースの解説(IBM i 側)
データベースの処理
Item.php ... updateItem ファンクション
public function updateItem($params){
try{
$code = isset($params['id']) ? $params['id'] : null ;
$kana = isset($params['kana']) ? $params['kana'] : null ; ... 省略
$update = " update “ ._LIBNAME. ".item ";
$set = " set kana = ?, name = ?, kind = ?, price = ?, sqty = ?, desc = ?, wh = ?, del = ? ";
$where = " where 1=1 and code = ?";
$strSQL = $update . $set . $where;
$stmt = $this->_db->prepare($strSQL);
$dbparams = array(
db2_escape_string($kana), db2_escape_string($name), ... 省略
);
$result = $stmt->execute($dbparams);
return $result;
}catch(Exception $e){
error_log('Item:updateItem exception = '.$e->getMessage());
} }
・値の取得
・SQL 文の作成
・SQL実行
・結果の戻し
(true/false)
- REST ful サービスの利用-
スマートフォン
商品の削除
アプリの動き
商品 削除
商品を削除
ソースの解説(クライアント 側)
スマートフォン
restapp.html ... deleteItem ファンクション
/* 商品の削除 */
function deleteItem(code){
$.ajax({
type:"DELETE",
url: SERVER_NAMES+”/rest/items/"+code, async: false,
success: function(msg){
var results = JSON.parse(msg);
if(results){
alert("削除が完了しました。");
iui.busy = true;
iui.showPageById("home");
}else{
alert("削除が失敗しました。");
settingForm(3);
iui.busy = true;
iui.showPageById("frmItem");
} },
error: function(req, status, errthrow){
alert(errthrow);
} });
}