Tugas Arsitektur Berasis Layanan Sesi 10
Nama : Mohammad Luthfi Zefrio Faizin NIM : 20210801134
Data Database MySql
GET Data
Post Data
PUT Data
DELETE Data
Code Controller REST-API
<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
class Barang extends REST_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Barang_model', 'barang');
}
public function index_get() {
$id = $this->get('id');
if ($id === null) {
$barang = $this->barang->getBarang();
} else {
$barang = $this->barang->getBarang($id);
}
if ($barang) {
$this->response([
'status' => true, 'data' => $barang ], REST_Controller::HTTP_OK);
} else {
$this->response([
'status' => false,
'data' => 'id tidak ditemukan' ], REST_Controller::HTTP_NOT_FOUND);
} }
public function index_delete() {
$id = $this->delete('id');
if ($id === null) {
$this->response([
'status' => false,
'message' => 'masukkan id!' ], REST_Controller::HTTP_NOT_FOUND);
}
elseif ($this->barang->deleteBarang($id) > 0) {
$this->response([
'status' => true, 'id' => $id,
'message' => 'data barang telah dihapus!' ], REST_Controller::HTTP_OK);
} else {
$this->response([
'status' => false,
'message' => 'id tidak ditemukan!' ], REST_Controller::HTTP_NOT_FOUND);
} }
public function index_post() {
$data = [
'id' => $this->post('id'), 'nama' => $this->post('nama'), 'jenis' => $this->post('jenis'), 'harga' => $this->post('harga'), 'stok' => $this->post('stok') ];
if ($this->barang->createBarang($data) > 0) {
$this->response([
'status' => true,
'message' => 'telah dibuat barang baru.' ], REST_Controller::HTTP_CREATED);
} else {
//id not found $this->response([
'status' => false,
'message' => 'gagal menambah data baru!' ], REST_Controller::HTTP_BAD_REQUEST);
} }
public function index_put() {
$id = $this->put('id');
$data = [
'id' => $this->put('id'), 'nama' => $this->put('nama'), 'jenis' => $this->put('jenis'), 'harga' => $this->put('harga'), 'stok' => $this->put('stok') ];
if ($this->barang->updateBarang($data, $id) > 0) {
$this->response([
'status' => true,
'message' => 'data barang telah diperbaharui.' ], REST_Controller::HTTP_CREATED);
} else {
$this->response([
'status' => false,
'message' => 'gagal memperbaharui data!' ], REST_Controller::HTTP_BAD_REQUEST);
} } }
Code Models REST-API
<?php
class Barang_model extends CI_Model{
public function getBarang($id = null){
if ($id === null) {
return $this->db->get('barang')->result_array();
} else {
return $this->db->get_where('barang', ['id' => $id])->result_array();
} }
public function deleteBarang($id) {
$this->db->delete('barang', ['id' => $id]);
return $this->db->affected_rows();
}
public function createBarang($data) {
$this->db->insert('barang', $data);
return $this->db->affected_rows();
}
public function updateBarang($data, $id) {
$this->db->update('barang', $data, ['id' => $id]);
return $this->db->affected_rows();
} }