Created API for API requests
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
<?php
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Models\Post;
|
||||
use Core\Session;
|
||||
use App\Models\ApiModel;
|
||||
|
||||
class Api
|
||||
{
|
||||
@@ -10,38 +9,110 @@ class Api
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new Post();
|
||||
$this->model = new ApiModel();
|
||||
}
|
||||
|
||||
public function posts($args = ['id' => '1'])
|
||||
public function index()
|
||||
{
|
||||
$index = [];
|
||||
$index['data'] = [
|
||||
"get_url" => "http://lepisi.dev/api/{tablename}{/id}",
|
||||
"put_url" => "http://lepisi.dev/api/{table}",
|
||||
"post_url" => "http://lepisi.dev/api/{table}",
|
||||
"delete_url" => "http://lepisi.dev/api/{table}"
|
||||
];
|
||||
$index['count'] = count($index['data']);
|
||||
|
||||
header("Content-Type: application/json");
|
||||
echo json_encode($index, JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
// public function posts($args = ['id' => '1'])
|
||||
// {
|
||||
// $get = [];
|
||||
//
|
||||
// if ($args['id'] == 3) {
|
||||
// $get['data'] = $this->model->showJoin();
|
||||
// } else {
|
||||
// $get['data'] = $this->model->showJoin([
|
||||
// ['pengumuman.status', '=', $args['id']]
|
||||
// ]);
|
||||
// }
|
||||
//
|
||||
// if ($get['data'] == false) {
|
||||
// $get['data']['content'] = 'Tidak ada pengumuman';
|
||||
// $get['data']['valid_at'] = '';
|
||||
// $get['data']['expired_at'] = '';
|
||||
// $get['data']['status'] = 0;
|
||||
// $get['data']['background'] = '#333';
|
||||
// $get['data']['foreground'] = '#888';
|
||||
// }
|
||||
//
|
||||
// if (array_key_exists(0, $get['data']) == false) {
|
||||
// $temp_data = $get['data'];
|
||||
// unset($get['data']);
|
||||
// $get['data'][] = $temp_data;
|
||||
// $temp_data = [];
|
||||
// }
|
||||
//
|
||||
// header('Content-Type: application/json');
|
||||
// echo json_encode($get);
|
||||
// }
|
||||
|
||||
public function get($table, $id = "")
|
||||
{
|
||||
$get = [];
|
||||
|
||||
if ($args['id'] == 3) {
|
||||
$get['data'] = $this->model->showJoin();
|
||||
} else {
|
||||
$get['data'] = $this->model->showJoin([
|
||||
['pengumuman.status', '=', $args['id']]
|
||||
]);
|
||||
}
|
||||
|
||||
if ($get['data'] == false) {
|
||||
$get['data']['content'] = 'Tidak ada pengumuman';
|
||||
$get['data']['valid_at'] = '';
|
||||
$get['data']['expired_at'] = '';
|
||||
$get['data']['status'] = 0;
|
||||
$get['data']['background'] = '#333';
|
||||
$get['data']['foreground'] = '#888';
|
||||
}
|
||||
|
||||
if (array_key_exists(0, $get['data']) == false) {
|
||||
$temp_data = $get['data'];
|
||||
unset($get['data']);
|
||||
$get['data'][] = $temp_data;
|
||||
$temp_data = [];
|
||||
}
|
||||
$get['data'] = $this->model->showAll(
|
||||
($id == "") ? [] : [
|
||||
['id', '=', $id]
|
||||
], $table
|
||||
);
|
||||
$get['count'] = count($get['data']);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($get);
|
||||
}
|
||||
|
||||
public function put($table)
|
||||
{
|
||||
$put = [];
|
||||
|
||||
$args = file_get_contents("php://input");
|
||||
$args = json_decode($args, true);
|
||||
|
||||
$put['data'] = $this->model->update($table, $args);
|
||||
$put['count'] = count($put['data']);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($put);
|
||||
}
|
||||
|
||||
public function post($table)
|
||||
{
|
||||
$post = [];
|
||||
$args = file_get_contents("php://input");
|
||||
$args = json_decode($args, true);
|
||||
|
||||
$post['data'] = $this->model->entry($table, $args);
|
||||
$post['data'] = $this->model->showAll([
|
||||
['id', '=', $post['data'][0]]
|
||||
], $table);
|
||||
$post['count'] = count($post['data']);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($post);
|
||||
}
|
||||
|
||||
public function delete($table)
|
||||
{
|
||||
$delete = [];
|
||||
$args = file_get_contents("php://input");
|
||||
$args = json_decode($args, true);
|
||||
|
||||
$delete['data'] = $this->model->remove($table, $args['id']);
|
||||
$delete['count'] = count($delete['data']);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($delete);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user