Created API for API requests
This commit is contained in:
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