CRUD Rest API dengan CodeIgniter 3.x

Melanjutkan belajar membuat Rest API dengan CodeIginiter 3.x. Kali ini kita akan belajar membuat CRUD Rest API.

Apa saja yang perlu dipersiapkan ?
– Download Framework CodeIgniter 3.x klik disini
– Download MySQL (untuk database)
– PHP Version 7.x
– Download Library CodeIgniter RestServer klik disini

API Create
Buat file controller misalnya, “Create_product.php

<?php

require (APPPATH.'/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;

class Create_product extends REST_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('mod_product');
    }
    
    public function index_post(){
        $nama = $_POST['catalog_nama'];
        $harga = $_POST['catalog_harga'];
        $deskripsi = $_POST['catalog_deskripsi'];
        $image = '';
        $response = $this->mod_product->create_product($nama, $harga, $deskripsi, $image);
        $this->response($response);
    }
} 
?>

API Read
Buat file controller misalnya, “Read_product.php

<?php

require (APPPATH.'/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;

class Read_product extends REST_Controller{
     
    public function __construct(){
        parent::__construct();
        $this->load->model('mod_product');
    }
    
    public function index_get(){
        $response = $this->mod_product->read_product();
        $this->response($response);
    }
} 
?>

API Update
Buat file controller misalnya, “Update_product.php

<?php

require (APPPATH.'/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;

class Update_product extends REST_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('mod_product');
    }
    
    public function index_post(){
        $id = $_POST['id'];
        $nama = $_POST['catalog_nama'];
        $harga = $_POST['catalog_harga'];
        $deskripsi = $_POST['catalog_deskripsi'];
        $image = '';
        $response = $this->mod_product->update_product($id, $nama, $harga, $deskripsi, $image);
        $this->response($response);
    }
} 
?>

API Delete
Buat file controller misalnya, “Delete_product.php

<?php

require (APPPATH.'/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;

class Delete_product extends REST_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('mod_product');
    }
    
    public function index_post(){
        $id = $_POST['id'];
        $response = $this->mod_product->delete_product($id);
        $this->response($response);
    }
} 
?>

Modules
Buat file module misalnya, “Mod_product.php

<?php
 
Class Mod_product extends CI_Model
{
   public function create_product(string $nama, int $harga, string $deskripsi, string $image)
   {
      $data = [
         'catalog_nama' => $nama,
         'catalog_harga' => $harga,
         'catalog_deskripsi' => $deskripsi,
         'catalog_image' => $image,
      ];
      $insert = $this->db->insert('catalog', $data);
      if ($insert) {
         $response = array();
         $response['error'] = false;
         $response['message'] = 'Successfully added product data';
         return $response;
      }
      return false;
   }

   public function read_product()
   {
      $this->db->order_by('catalog_id', 'DESC');
      $query = $this->db->get('catalog');
      if ($query->num_rows() > 0) {
         $response = array();
         $response['error'] = false;
         $response['message'] = 'Successfully retrieved product data';
         foreach ($query->result() as $row) {
            $harga = $row->catalog_harga;
            $tempArray = array();
            $tempArray['product_id'] = (int)$row->catalog_id;
            $tempArray['product_name'] = $row->catalog_nama;
            $tempArray['price'] = (int)$harga;
            $tempArray['note'] = $row->catalog_deskripsi;
            $tempArray['image'] = 'http://127.0.0.1:8000/image/'.$row->catalog_image;
            $response['data'][] = $tempArray;
         }
         return $response;
      }
      return false;
   }

   public function update_product(int $id, string $nama, int $harga, string $deskripsi, string $image)
   {
      $update = $this->db->query(
         "UPDATE `catalog` 
          SET 
            catalog_nama='$nama', 
            catalog_harga=$harga, 
            catalog_deskripsi='$deskripsi', 
            catalog_image='$image' 
          WHERE catalog_id = $id "
      );
      if ($update) {
         $response = array();
         $response['error'] = false;
         $response['message'] = 'Successfully changed product data';
         return $response;
      }
      return false;
   }

   public function delete_product(int $id)
   {
      $id = $id;
      $delete = $this->db->query('DELETE FROM `catalog` WHERE catalog_id = '. $id);
      if ($delete) {
         $response = array();
         $response['error'] = false;
         $response['message'] = 'Successfully deleted product data';
         return $response;
      }
   }
}

Selanjutnya di tes menggunakan Rest Client seperti berikut hasilnya :

result_api_read_product_belajaraplikasi
Result API read_product
result_api_create_product_belajaraplikasi
Result API create_product

Terima kasih
Selamat mencoba
Happy coding 🙂

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *