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);
|
||||
}
|
||||
}
|
||||
|
||||
159
App/Models/ApiModel.php
Normal file
159
App/Models/ApiModel.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
use App\Config;
|
||||
|
||||
class ApiModel
|
||||
{
|
||||
protected static $conn = null;
|
||||
|
||||
protected static function connectDB()
|
||||
{
|
||||
try {
|
||||
if (!self::$conn) {
|
||||
$dsn = 'mysql:host='.Config::DB_HOST.';dbname='.Config::DB_DB;
|
||||
self::$conn = new \PDO($dsn, Config::DB_UNAME, Config::DB_PWD);
|
||||
|
||||
self::$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
return self::$conn;
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage, 444);
|
||||
}
|
||||
}
|
||||
|
||||
public function showAll($conditions = [], $table)
|
||||
{
|
||||
$sql = "SELECT * FROM {$table}";
|
||||
|
||||
if ($conditions) {
|
||||
$sql .= " WHERE";
|
||||
foreach ($conditions as $condition) {
|
||||
|
||||
$keys[] = $condition[0];
|
||||
$operators[] = $condition[1];
|
||||
$values[] = $condition[2];
|
||||
}
|
||||
|
||||
$x = 0;
|
||||
foreach ($keys as $key) {
|
||||
$sql .= " $key $operators[$x] ?";
|
||||
$x++;
|
||||
if ($x < count($keys)) {
|
||||
$sql .= " AND";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$con = static::connectDB();
|
||||
$query = $con->prepare($sql);
|
||||
|
||||
if (count($conditions)) {
|
||||
$x = 1;
|
||||
foreach ($values as $value) {
|
||||
$query->bindValue($x, $value);
|
||||
$x++;
|
||||
}
|
||||
}
|
||||
|
||||
$query->execute();
|
||||
return $query->fetchAll(\PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
echo "Error: $e->getMessage()";
|
||||
}
|
||||
}
|
||||
|
||||
public function update($table, $args)
|
||||
{
|
||||
$sql = "UPDATE {$table} SET";
|
||||
|
||||
$id = $args['id'];
|
||||
unset($args['id']);
|
||||
|
||||
$keys = array_keys($args);
|
||||
$fields = [];
|
||||
foreach ($keys as $key) {
|
||||
$fields[] = $key . " = ?";
|
||||
}
|
||||
if (count($fields) > 1) {
|
||||
$fields = implode(', ', $fields);
|
||||
} else {
|
||||
$fields = implode('', $fields);
|
||||
}
|
||||
|
||||
try {
|
||||
$con = static::connectDB();
|
||||
$sql .= " {$fields} WHERE id = ?";
|
||||
|
||||
$query = $con->prepare($sql);
|
||||
$x = 1;
|
||||
foreach ($args as $value) {
|
||||
$query->bindValue($x, $value);
|
||||
$x++;
|
||||
}
|
||||
$query->bindValue($x, $id);
|
||||
|
||||
$query->execute();
|
||||
|
||||
return $this->showAll([
|
||||
['id', '=', $id]
|
||||
], $table);
|
||||
} catch (PDOException $e) {
|
||||
echo "Error: $e->getMessage()";
|
||||
}
|
||||
}
|
||||
|
||||
public function entry($table, $args)
|
||||
{
|
||||
$sql = "INSERT INTO {$table}";
|
||||
|
||||
$fields = array_keys($args);
|
||||
$fields = implode(", ", $fields);
|
||||
|
||||
$values = "";
|
||||
for ($i=1; $i <= count($args); $i++) {
|
||||
$values .= "?";
|
||||
if ($i < count($args)) $values .= ", ";
|
||||
}
|
||||
|
||||
$sql .= " ({$fields}) VALUES ({$values})";
|
||||
|
||||
try {
|
||||
$con = static::connectDB();
|
||||
|
||||
$query = $con->prepare($sql);
|
||||
$x = 1;
|
||||
foreach ($args as $value) {
|
||||
$query->bindValue($x, $value);
|
||||
$x++;
|
||||
}
|
||||
|
||||
$query->execute();
|
||||
|
||||
$last_entry = "SELECT LAST_INSERT_ID()";
|
||||
$last_entry = $con->prepare($last_entry);
|
||||
$last_entry->execute();
|
||||
|
||||
return $last_entry->fetch();
|
||||
} catch (PDOException $e) {
|
||||
echo "Error: $e->getMessage()";
|
||||
}
|
||||
}
|
||||
|
||||
public function remove($table, $id)
|
||||
{
|
||||
$sql = "UPDATE {$table} SET `status` = 0 WHERE `id` = ?";
|
||||
try {
|
||||
$con = static::connectDB();
|
||||
|
||||
$query = $con->prepare($sql);
|
||||
$query->bindValue(1, $id);
|
||||
$query->execute();
|
||||
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
echo "Error: $e->getMessage()";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user