+62 812-1171-5379 Fast Respond

Crud JSON dengan Delphi

Crud JSON dengan Delphi

Topik kali adalah membahas bagaimana cara membuat crud dengan delphi.
Ikuti langkah-langkah berikut :
1. Dowoload code igniter versi 3 (Silahkan versi tertinggi saja) https://codeigniter.com/download, nama file hasil download adalah bcit-ci-CodeIgniter-3.1.13-0-gbcb17eb.zip, extrax file tersebut, berikutnya ganti nama foldernya menjadi kursusdelphi-API, terakhir kopi folder tersebut ke C:\xampp\htdocs,
2. Sekarang silahkan tes dengan browser http://localhost/kursusdelphi-API/index.php, jika tidak ada error maka sampai posisi ini masih benar.
3. Sekarang buka browser https://github.com/chriskacerguis/codeigniter-restserver dan silahkan didownload
--==PERHATIKAN ==--
+ Hasil download ada 3 file (Format.php, RestController.php, rest.php)
+ File (Format.php, RestController.php) di kopikan ke "C:\xampp\htdocs\kursusdelphi-API\application\libraries"
+ File (rest.php) di kopikan ke "C:\xampp\htdocs\kursusdelphi-API\application\config"

Sampai sini sudah selesai konfigurasi pada CodeIgniter
4. Buat tabel "key" pada database "tes" dengan script dibawah ini :

CREATE TABLE `keys` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `key` varchar(40) NOT NULL,
  `level` int(2) NOT NULL,
  `ignore_limits` tinyint(1) NOT NULL DEFAULT '0',
  `is_private_key` tinyint(1) NOT NULL DEFAULT '0',
  `ip_addresses` text,
  `date_created` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `keys` VALUES (1,1,'21232f297a57a5a743894a0e4a801fc3',1,0,0,'%',0);


5. Membuat tabel untuk dimanipulasi data ( insert, update, delete )

CREATE TABLE `t_tes_api` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nama` varchar(255) DEFAULT NULL,
  `foto` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `t_tes_api` VALUES (1,'Fathon Sugiharta',NULL);
INSERT INTO `t_tes_api` VALUES (2,'Kursus Delphi',NULL);
INSERT INTO `t_tes_api` VALUES (3,'Kursus Website Pro',NULL);

6. Membuat Controller pada code igniter, silahkan beri nama API.php (file ini disimpan pada "C:\xampp\htdocs\kursusdelphi-API\application\controllers")
 
defined('BASEPATH') OR exit('No direct script access allowed');

//use chriskacerguis\RestServer\RestController;
# require REST Library
require APPPATH . 'libraries/RestController.php';
require APPPATH . 'libraries/Format.php';

# use REST Class
use Restserver\Libraries\RestController;

class API extends RestController {

    function __construct() {
		
		header('Access-Control-Allow-Origin: *');
		header('Content-Type: application/json; charset=UTF-8');
		header("Access-Control-Allow-Headers: APP_KEY, Origin, X-Requested-With, Content-Type, Enctype, Accept, Access-Control-Request-Method");
		header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
		$method = $_SERVER['REQUEST_METHOD'];
		if($method == "OPTIONS") {
			die();
		}
		
        parent::__construct();
        $this->load->database();
    }
	
	public $storage_path = 'storage/uploads/';
	  
    // Melihat data
	function index_test_get() {
        $tes_api = $this->db->get('t_tes_api')->result();
        // testing response
        $response['status']=200;
        $response['error']=false;
        $response['data']=$tes_api;
        // tampilkan response
        $this->response($response);
    }
    
    // Mengirim atau menambah data baru
	function index_test_post() {
		$data = $this->post();

        $tes_api = $this->db->insert('t_tes_api',$data);

        if($this->db->affected_rows() > 0){
            // testing response
            $response['status']=200;
            $response['error']=false;
            $response['message']='Berhasil Tambah';
            // tampilkan response
            $this->response($response);
        }else{
            // testing response
            $response['status']=502;
            $response['error']=true;
            $response['message']='Gagal Tambah';
            // tampilkan response
            $this->response($response); 
        }
    }
    // Mengedit data baru
    public function index_test_put()
    {
        if($this->put('id') == ''){
            // testing response
            $response['status']=502;
            $response['error']=true;
            $response['message']='ID tidak boleh kosong';
            // tampilkan response
            $this->response($response);
            die;
        }
      
        $set = array(
            "nama"=>$this->put('nama'),
            "foto"=>$this->put('foto')
        );
          
        $where = array(
            "id"=>$this->put('id')
        );
        
        $this->db->where($where);
        
        $update = $this->db->update('t_tes_api', $set);
        
        if($this->db->affected_rows() > 0){
            // testing response
            $response['status']=200;
            $response['error']=false;
            $response['message']='Berhasil Update';
            // tampilkan response
            $this->response($response);
        }else{
            // testing response
            $response['status']=502;
            $response['error']=true;
            $response['message']='Gagal Update';
            // tampilkan response
            $this->response($response); 
        }
        
    }
    
    // Menghapus data baru
    public function index_test_delete()
    {
        $jsonArray = json_decode($this->input->raw_input_stream, true);
        
        if($jsonArray['id'] == ''){
            // testing response
            $response['status']=502;
            $response['error']=true;
            $response['message']='ID tidak boleh kosong';
            // tampilkan response
            $this->response($response);
            die;
        }
          
        $where = array(
            "id"=>$jsonArray['id']
        );
        
        $this->db->where($where);
        
        $update = $this->db->delete('t_tes_api');
        
        if($this->db->affected_rows() > 0){
            // testing response
            $response['status']=200;
            $response['error']=false;
            $response['message']='Berhasil Delete';
            // tampilkan response
            $this->response($response);
        }else{
            // testing response
            $response['status']=502;
            $response['error']=true;
            $response['message']='Gagal Delete';
            // tampilkan response
            $this->response($response); 
        }
        
    }
    
    // Melihat data kontak
    function index_get() {
        $tes_api = $this->db->get('t_tes_api')->result();
        // testing response
        $response['status']=200;
        $response['error']=false;
        $response['data']=$tes_api;
        // tampilkan response
        $this->response($response);
    }
	
	// Mengirim atau menambah data kontak baru
	function index_post() {
		$data = $this->post();
		$id = '';
		$post_data = $this->_all_column($data, $id);

        $this->db->insert('t_tes_api',$post_data);

        if ($this->db->affected_rows() > 0) {
            
            // testing response
            $response['status']=200;
            $response['error']=false;
            $response['data']='success create quote post';
            // tampilkan response
            $this->response($response);
        }else {
            
            // testing response
            $response['status']=200;
            $response['error']=false;
            $response['data']='failed create quote post';
            // tampilkan response
            $this->response($response);
        }

        return $response;
    }
	
	// Memperbarui data kontak yang telah ada
	function index_edit_post() {
		$data = $this->post();
		
		if(isset($data['id']) == ''){
            // testing response
            $response['status']=502;
            $response['error']=true;
            $response['message']='ID tidak boleh kosong';
            // tampilkan response
            $this->response($response);
            die;
        }
        
        $id = $data['id'];
		
        $identity = ['id' => $id];

        if (!empty($_FILES['foto'])) {
            $delete_image = $this->db->get_where('t_tes_api',$identity)->row_array();
			
			if($delete_image['foto'] != 'No-Image.png'){
				@unlink($this->storage_path.$delete_image['foto']);
				@unlink($this->storage_path.'thumbnail/'.$delete_image['foto']);           
			}
        }

        $post_data = $this->_all_column($data, $id);

        $this->db->trans_start();
        $this->db->update('t_tes_api', $post_data, $identity);
        $this->db->trans_complete();

        if ($this->db->affected_rows() > 0) {
            // testing response
            $response['status']=200;
            $response['error']=false;
            $response['data']='success update quote post';
            // tampilkan response
            $this->response($response);
        }else {
            if ($this->db->trans_status() === false) {
                // testing response
                $response['status']=200;
                $response['error']=false;
                $response['data']='failed update quote post';
                // tampilkan response
                $this->response($response);
            }else {                
                // testing response
                $response['status']=200;
                $response['error']=false;
                $response['data']='success update quote post';
                // tampilkan response
                $this->response($response);
            }
        }   

        return $response;
    }
	
	//Menghapus salah satu data kontak
	function index_delete() {
	    
	        $jsonArray = json_decode($this->input->raw_input_stream, true);
	        
            if($jsonArray['id'] == ''){
                // testing response
                $response['status']=502;
                $response['error']=true;
                $response['message']='ID tidak boleh kosong';
                // tampilkan response
                $this->response($response);
                die;
            }
            
			$id = $jsonArray['id'];
			
			$identity = ['id' => $id];

            $this->db->db_debug = false;
            $read = $this->db->get_where('t_tes_api', $identity);

            if ($read->num_rows() <= 0) {
                // testing response
                $response['status']=200;
                $response['error']=false;
                $response['message']='key not found on table';
                // tampilkan response
                $this->response($response);

                return $response;
            }else{
                if ($read->num_rows() > 0) {
                    $read = $read->row_array();
					if($read['foto'] != 'No-Image.png'){
						@unlink($this->storage_path.$read['foto']);
						@unlink($this->storage_path.'thumbnail/'.$read['foto']);           
					}      
                    $this->db->delete('t_tes_api',$identity);        
                    if ($this->db->affected_rows() > 0) {
                        
                        // testing response
                        $response['status']=200;
                        $response['error']=false;
                        $response['message']='success delete';
                        // tampilkan response
                        $this->response($response);
                    }else {
                        // testing response
                        $response['status']=200;
                        $response['error']=false;
                        $response['message']='failed delete';
                        // tampilkan response
                        $this->response($response);
                    }
				}
			}
			return $response;
    }
	
	public function _all_column($data, $id){

        # file upload
        $file = $this->_upload($data);

        $prepare_data = array(
            'id' => $id,
            'nama' => $data['nama'] ,
            );


        if ($file !== 'noimage') {
            $prepare_data['foto'] = $file;
        }

        $post_data = $prepare_data;

        return $post_data;
    }


    //Masukan function selanjutnya disini
}


7. Lanjut besuk guest