Jumat, 01 Juni 2018

Membuat Layanan RestFull API Codeigniter Serial Studi Kasus Perpustakaan Part 3

Kembali lagi di Coding Library,

Di Part 3 ini kita akan melakukan implementasi membuat layanan Web Services tentunya kita akan membuat tabel pada database yang sebelumnya telah kita buat. oh ya buat teman-teman yang hanya melihat halaman ini pada kesan pertama saya berharap teman-teman juga mengikuti sejak awal pembuatan atau konfigurasi RestFull API dengan Framework Codeigniter dan Library codeigniter Rest_server di Part 1  dan juga membuat authentikasi pada Part 2 dan silahkan teman-teman lanjut ke Part ini.

Baiklah teman-teman silahkan copy script membuat table berikut, namun saya menyarankan untuk mngetik langsung agar teman-teman bisa faham betul membuat table dengan script sql.

USE perpustakaan;
CREATE TABLE sys_category(
 books_cat_id varchar(5) PRIMARY KEY,
    book_cat_name varchar(30)
);
INSERT INTO sys_category VALUES('KS', 'Computer & Sains');
CREATE TABLE sys_publisher(
 publisher_id int AUTO_INCREMENT PRIMARY KEY,
    publisher_name varchar(100)
);
INSERT INTO sys_publisher VALUES(null,'BOOKS PUBLISHER');
CREATE TABLE sys_books
(
  books_id varchar(10) PRIMARY KEY,
  books_title varchar(100),
  books_cat_id varchar(5),
  books_description text,
  books_year varchar(4),
  books_qty int,
  publisher_id int 
);

Sekarang teman-teman buatkan sebuah file controller>api> dengan nama Books.php


<?php
/**
 *
 */
 require APPPATH . 'libraries/REST_Controller.php';
 class Books extends REST_Controller
 {

  function __construct()
  {
    parent::__construct();
    $this->load->model('validation/model','val');
    $this->load->model('buku/buku_m','book');

  }

  function index_get(){
    $token=$this->get('app_token');
    $id=$this->get('id');
  $valid = $this->val->cek_token($token);
  if ($token==""){
   $this->response(array('status'=>false,'error' =>'Invalid Aktifitas, Tidak memiliki Akses'),REST_Controller::HTTP_OK);
  }else if($valid==true){
   $this->response(array('status'=>true,'response'=>$this->book->get_books($id)), REST_Controller::HTTP_OK);
  }else{
   $this->response(array('status'=>false,'error' =>'Not Found'),REST_Controller::HTTP_OK);
  }
  }

  function index_post()
  {
      $books_id= $this->post('p1');
      $books_title= $this->post('p2');
      $books_cat_id= $this->post('p3');
      $books_description= $this->post('p4');
      $books_year= $this->post('p5');
      $books_qty= $this->post('p6');
      $publisher_id= $this->post('p7');
      if($books_id=="" || $books_title=="" || $books_cat_id=="" || $books_description=="" || $books_year=="" || $books_qty=="" || $publisher_id==""){
          $this->response('Mohon Lengkapi data', REST_Controller::HTTP_OK);
      }else{
          $arrayName = array('books_id' => $books_id,
                              'books_title' => $books_title,
                              'books_cat_id' => $books_cat_id,
                              'books_description' => $books_description,
                              'books_year' => $books_year,
                              'books_qty' => $books_qty,
                              'publisher_id'=> $publisher_id,
                            );
          $this->db->insert('sys_books',$arrayName);
          $this->response('Sukses', REST_Controller::HTTP_OK);
    }
  }
  function index_put()
  {
      $books_id= $this->put('p1');
      $books_title= $this->put('p2');
      $books_cat_id= $this->put('p3');
      $books_description= $this->put('p4');
      $books_year= $this->put('p5');
      $books_qty= $this->put('p6');
      $publisher_id= $this->put('p7');
      if($books_id=="" || $books_title=="" || $books_cat_id=="" || $books_description=="" || $books_year=="" || $books_qty=="" || $publisher_id==""){
          $this->response('Mohon Lengkapi data', REST_Controller::HTTP_OK);
      }else{
          $arrayName = array(
                              'books_title' => $books_title,
                              'books_cat_id' => $books_cat_id,
                              'books_description' => $books_description,
                              'books_year' => $books_year,
                              'books_qty' => $books_qty,
                              'publisher_id'=> $publisher_id,
                            );
          $this->db->where('books_id',$books_id);
          $this->db->update('sys_books',$arrayName);
          $this->response('Update Success', REST_Controller::HTTP_OK);
    }
  }

  function index_delete()
  {
      $books_id= $this->delete('p1');
      $this->db->where('books_id',$books_id);
      $delete =$this->db->delete('sys_books');
      if($delete){
          $this->response('Delete Sukses', 201);
      }else{
        $this->response('Error Delete',502);
      }
  }
}

Buat sebuah folder buku pada model dan buatkan sebuah file dengan nama Buku_m.php copy kan script model sebagai berikut :

<?php
/**
*
*/
class Buku_m extends CI_Model
{

 function __construct()
 {
  parent ::__construct();
 }
 function get_books($value='')
 {
  if($value !=''){
   $this->db->where('sys_books.books_id',$value);
  }
  $this->db->select(
'sys_books.books_id,sys_books.books_title,
sys_category.book_cat_name,sys_books.books_description,
sys_books.books_year,sys_books.books_qty,
sys_publisher.publisher_name');
  $this->db->from('sys_books');
  $this->db->join('sys_category','sys_books.books_cat_id=sys_category.books_cat_id');
  $this->db->join('sys_publisher','sys_books.publisher_id =sys_publisher.publisher_id');
  $query = $this->db->get();

  if($query->num_rows() >0 ){
   return $query->result_array();
  }

 }

}

Silahkan teman-teman melakukan registrasi buku dengan tools Postman pada controller Books.php dengan metode POST seperti pada gambar berikut ini : 
 silahkan ulangi registrasi pada postman dengan nilai p1=BK002

setelah itu kita coba uji menampilkan data dengan metode GET tanpa books_id atau parameter id seperti gambar berikut dan perlu diingatkan tanpa token user tidak bisa melihat data yang di request :
 Berikut hasil apabila kita melakukan request dengan book_id atau parameter id di Postman

sampai disini kita sudah selesai membuat Layanan Restfull API untuk proses input data buku, diharapkan teman-teman bisa melakukan hal yang sama pada data Anggota jadi pada part berikutnya kita hanya membahasa masalah transaksi Pinjam dan  Juga pengembalian Buku.

oke teman-teman selamat mencoba dan sukses selalu, 
sampai jupa di part selanjutnya

0 komentar:

Posting Komentar

Terima Kasih atas kunjungan serta komentar saudara,
dalam waktu 1 x 24 jam kami akan merespon komentar saudara,