TUTORIAL MEMBUAT RESTFUL API DENGAN SLIM FRAMEWORK [BAGIAN 6]

TUTORIAL MEMBUAT RESTFUL API DENGAN SLIM FRAMEWORK [BAGIAN 6]

Assalamualaikum

Pada bagian 5 kita sudah membuat update data

http://flutter.id/blog/tutorial-membuat-restful-api-dengan-slim-framework-bagian-5/

Pada bagian 6, kita akan membuat delete data, restore data dan menampilkan log catatan, berikut kode lengkapnya

<?php 
require __DIR__ . '/vendor/autoload.php';
require 'libs/NotORM.php'; 

use \Slim\App;

$app = new App();

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'dbcatatan';
$dbmethod = 'mysql:dbname=';

$dsn = $dbmethod.$dbname;
$pdo = new PDO($dsn, $dbuser, $dbpass);
$db  = new NotORM($pdo);

$app-> get('/', function(){
    echo "API Catatan Keuangan With SLIM Framework";
});

$app->post('/addcatatan', function($request, $response, $args) use($app, $db){
    $param = $request->getParams();
    $r = $db->tb_catatan->insert($param);

    $res["error"] = false;
    $res["message"] = "Berhasil menambahkan catatan ke database";
    echo json_encode($res);
});

$app ->get('/listcatatan', function() use($app, $db){

 $res["error"] = false;
    $res["message"] = "Berhasil mendapatkan data Catatan";

    foreach($db->v_catatan()->where('status', 0) as $data){
        $res['data'][] = array(
            'id' => $data['id'],
            'judul' => $data['judul'],
            'catatan' => $data['catatan'],
            'create_at' => $data['create_at'],
            'update_at' => $data['update_at']
            );
    }

    echo json_encode($res);

});

$app->post('/editcatatan/{id}', function($request, $response, $args) use($app, $db){

    $q = $db->tb_catatan()->where('id', $args);

    if($q->fetch()){

        $param = $request->getParams();
        $r = $q->update($param);
        echo json_encode(array(
            "error" => false,
            "message" => "Catatan berhasil diupdate"));

    }else{

        echo json_encode(array(
            "error" => true,
            "message" => "Catatan tersebut tidak ada"));

    }
});


$app->post('/deletecatatan/{id}', function($request, $response, $args) use($app, $db){

    $q = $db->tb_catatan()->where('id', $args);

    if($q->fetch()){

        $param = array("status" => 1);
        $r = $q->update($param);
        echo json_encode(array(
            "error" => false,
            "message" => "Catatan berhasil di delete"));

    }else{

        echo json_encode(array(
            "error" => true,
            "message" => "Catatan tersebut tidak ada"));

    }
});

$app->post('/restorecatatan/{id}', function($request, $response, $args) use($app, $db){

    $q = $db->tb_catatan()->where('id', $args);

    if($q->fetch()){

        $param = array("status" => 0);
        $r = $q->update($param);
        echo json_encode(array(
            "error" => false,
            "message" => "Catatan berhasil di restore"));

    }else{

        echo json_encode(array(
            "error" => true,
            "message" => "Catatan tersebut tidak ada"));

    }
});

$app ->get('/logcatatan', function() use($app, $db){

 $res["error"] = false;
    $res["message"] = "Berhasil mendapatkan log Catatan";

    foreach($db->v_catatan_log() as $data){
        $res['data'][] = array(
            'id' => $data['id'],
            'id_catatan' => $data['id_catatan'],
            'method' => $data['method'],
            'judul' => $data['judul'],
            'catatan' => $data['catatan'],
            'update_at' => $data['update_at']
            );
    }

    echo json_encode($res);

});

// $app->delete('/deletecatatan/{id}', function($request, $response, $args) use($app, $db){
//     $q = $db->tb_catatan()->where('id', $args);
//     if($q->fetch()){

//         $r = $q->delete();
//         echo json_encode(array(
//             "error" => false,
//             "message" => "Catatan berhasil dihapus"));

//     }
//     else{

//         echo json_encode(array(
//             "error" => true,
//             "message" => "Catatan tersebut tidak ada"));

//     }
// });



$app->run();

 

Mari Kita Coba

localhost/apicatatan/index.php/listcatatan
localhost/apicatatan/index.php/addcatatan
jadi 2 datanya
localhost/apicatatan/index.php/editcatatan/2
berubah datanya
localhost/apicatatan/index.php/deletecatatan/1
Datanya tinggal 1
localhost/apicatatan/index.php/restorecatatan/1
datanya kembali lagi
Tampil Log Catatan
{
    "error": false,
    "message": "Berhasil mendapatkan log Catatan",
    "data": [
        {
            "id": "10",
            "id_catatan": "1",
            "method": "UPDATE",
            "judul": "edit",
            "catatan": "sudah diedit",
            "update_at": "2019-12-01 00:12:05"
        },
        {
            "id": "9",
            "id_catatan": "1",
            "method": "BEFOREUPDATE",
            "judul": "edit",
            "catatan": "sudah diedit",
            "update_at": "2019-12-01 00:12:05"
        },
        {
            "id": "8",
            "id_catatan": "1",
            "method": "UPDATE",
            "judul": "edit",
            "catatan": "sudah diedit",
            "update_at": "2019-12-01 00:10:34"
        },
        {
            "id": "7",
            "id_catatan": "1",
            "method": "BEFOREUPDATE",
            "judul": "edit",
            "catatan": "sudah diedit",
            "update_at": "2019-12-01 00:10:34"
        },
        {
            "id": "6",
            "id_catatan": "2",
            "method": "UPDATE",
            "judul": "data update",
            "catatan": "data update",
            "update_at": "2019-12-01 00:08:58"
        },
        {
            "id": "5",
            "id_catatan": "2",
            "method": "BEFOREUPDATE",
            "judul": "data baru",
            "catatan": "data baru",
            "update_at": "2019-12-01 00:08:58"
        },
        {
            "id": "4",
            "id_catatan": "2",
            "method": "INSERT",
            "judul": "data baru",
            "catatan": "data baru",
            "update_at": "2019-12-01 00:07:04"
        },
        {
            "id": "3",
            "id_catatan": "1",
            "method": "UPDATE",
            "judul": "edit",
            "catatan": "sudah diedit",
            "update_at": "2019-11-30 23:57:51"
        },
        {
            "id": "2",
            "id_catatan": "1",
            "method": "BEFOREUPDATE",
            "judul": "tes",
            "catatan": "tes",
            "update_at": "2019-11-30 23:57:51"
        },
        {
            "id": "1",
            "id_catatan": "1",
            "method": "INSERT",
            "judul": "tes",
            "catatan": "tes",
            "update_at": "2019-11-30 23:39:26"
        }
    ]
}

 

Semoga Bermanfaat

Flutter Developer