Initial Commit: Tugas Semua Pertemuan
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
DB_HOST=localhost
|
||||
DB_PORT=3306
|
||||
DB_NAME=spbu_db
|
||||
DB_USER=root
|
||||
DB_PASSWORD=
|
||||
@@ -0,0 +1 @@
|
||||
.env
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Methods: POST");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
require_once '../config/database.php';
|
||||
|
||||
// Cek apakah user sudah login sebagai admin
|
||||
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
|
||||
http_response_code(401);
|
||||
echo json_encode(array("message" => "Unauthorized"));
|
||||
exit();
|
||||
}
|
||||
|
||||
$database = new Database();
|
||||
$db = $database->getConnection();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if (
|
||||
!empty($data->nama) &&
|
||||
!empty($data->no_spbu) &&
|
||||
!empty($data->status) &&
|
||||
!empty($data->latitude) &&
|
||||
!empty($data->longitude)
|
||||
) {
|
||||
try {
|
||||
$query = "INSERT INTO spbu (nama, no_spbu, status, latitude, longitude) VALUES (:nama, :no_spbu, :status, :latitude, :longitude)";
|
||||
$stmt = $db->prepare($query);
|
||||
|
||||
$stmt->bindParam(":nama", $data->nama);
|
||||
$stmt->bindParam(":no_spbu", $data->no_spbu);
|
||||
$stmt->bindParam(":status", $data->status);
|
||||
$stmt->bindParam(":latitude", $data->latitude);
|
||||
$stmt->bindParam(":longitude", $data->longitude);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
http_response_code(201);
|
||||
echo json_encode(array(
|
||||
"message" => "SPBU berhasil ditambahkan",
|
||||
"id" => $db->lastInsertId()
|
||||
));
|
||||
} else {
|
||||
http_response_code(503);
|
||||
echo json_encode(array("message" => "Gagal menambahkan SPBU"));
|
||||
}
|
||||
} catch(PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(array("message" => "Error: " . $e->getMessage()));
|
||||
}
|
||||
} else {
|
||||
http_response_code(400);
|
||||
echo json_encode(array("message" => "Data tidak lengkap"));
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
session_start();
|
||||
|
||||
if (isset($_SESSION['is_admin']) && $_SESSION['is_admin'] === true) {
|
||||
http_response_code(200);
|
||||
echo json_encode(array(
|
||||
"is_admin" => true,
|
||||
"username" => $_SESSION['admin_username']
|
||||
));
|
||||
} else {
|
||||
http_response_code(200);
|
||||
echo json_encode(array(
|
||||
"is_admin" => false
|
||||
));
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Methods: DELETE");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
require_once '../config/database.php';
|
||||
|
||||
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
|
||||
http_response_code(401);
|
||||
echo json_encode(array("message" => "Unauthorized"));
|
||||
exit();
|
||||
}
|
||||
|
||||
$database = new Database();
|
||||
$db = $database->getConnection();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if (!empty($data->id)) {
|
||||
try {
|
||||
$query = "DELETE FROM spbu WHERE id = :id";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(":id", $data->id);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
http_response_code(200);
|
||||
echo json_encode(array("message" => "SPBU berhasil dihapus"));
|
||||
} else {
|
||||
http_response_code(503);
|
||||
echo json_encode(array("message" => "Gagal menghapus SPBU"));
|
||||
}
|
||||
} catch(PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(array("message" => "Error: " . $e->getMessage()));
|
||||
}
|
||||
} else {
|
||||
http_response_code(400);
|
||||
echo json_encode(array("message" => "ID tidak ditemukan"));
|
||||
}
|
||||
?><?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Methods: DELETE");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
require_once '../config/database.php';
|
||||
|
||||
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
|
||||
http_response_code(401);
|
||||
echo json_encode(array("message" => "Unauthorized"));
|
||||
exit();
|
||||
}
|
||||
|
||||
$database = new Database();
|
||||
$db = $database->getConnection();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if (!empty($data->id)) {
|
||||
try {
|
||||
$query = "DELETE FROM spbu WHERE id = :id";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(":id", $data->id);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
http_response_code(200);
|
||||
echo json_encode(array("message" => "SPBU berhasil dihapus"));
|
||||
} else {
|
||||
http_response_code(503);
|
||||
echo json_encode(array("message" => "Gagal menghapus SPBU"));
|
||||
}
|
||||
} catch(PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(array("message" => "Error: " . $e->getMessage()));
|
||||
}
|
||||
} else {
|
||||
http_response_code(400);
|
||||
echo json_encode(array("message" => "ID tidak ditemukan"));
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
require_once '../config/database.php';
|
||||
|
||||
$database = new Database();
|
||||
$db = $database->getConnection();
|
||||
|
||||
try {
|
||||
$query = "SELECT id, nama, no_spbu, status, latitude, longitude FROM spbu ORDER BY nama ASC";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->execute();
|
||||
|
||||
$spbu_arr = array();
|
||||
|
||||
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
$spbu_item = array(
|
||||
"id" => $row['id'],
|
||||
"nama" => $row['nama'],
|
||||
"no_spbu" => $row['no_spbu'],
|
||||
"status" => $row['status'],
|
||||
"latitude" => floatval($row['latitude']),
|
||||
"longitude" => floatval($row['longitude'])
|
||||
);
|
||||
array_push($spbu_arr, $spbu_item);
|
||||
}
|
||||
|
||||
http_response_code(200);
|
||||
echo json_encode($spbu_arr);
|
||||
|
||||
} catch(PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(array("message" => "Error: " . $e->getMessage()));
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Methods: POST");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
require_once '../config/database.php';
|
||||
|
||||
$database = new Database();
|
||||
$db = $database->getConnection();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if (!empty($data->username) && !empty($data->password)) {
|
||||
try {
|
||||
$query = "SELECT id, username, password FROM admin WHERE username = :username LIMIT 1";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(":username", $data->username);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if (password_verify($data->password, $row['password'])) {
|
||||
$_SESSION['is_admin'] = true;
|
||||
$_SESSION['admin_id'] = $row['id'];
|
||||
$_SESSION['admin_username'] = $row['username'];
|
||||
|
||||
http_response_code(200);
|
||||
echo json_encode(array(
|
||||
"message" => "Login berhasil",
|
||||
"username" => $row['username']
|
||||
));
|
||||
} else {
|
||||
http_response_code(401);
|
||||
echo json_encode(array("message" => "Password salah"));
|
||||
}
|
||||
} else {
|
||||
http_response_code(401);
|
||||
echo json_encode(array("message" => "Username tidak ditemukan"));
|
||||
}
|
||||
} catch(PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(array("message" => "Error: " . $e->getMessage()));
|
||||
}
|
||||
} else {
|
||||
http_response_code(400);
|
||||
echo json_encode(array("message" => "Username dan password diperlukan"));
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
session_start();
|
||||
session_destroy();
|
||||
|
||||
http_response_code(200);
|
||||
echo json_encode(array("message" => "Logout berhasil"));
|
||||
?>
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Methods: PUT");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
require_once '../config/database.php';
|
||||
|
||||
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
|
||||
http_response_code(401);
|
||||
echo json_encode(array("message" => "Unauthorized"));
|
||||
exit();
|
||||
}
|
||||
|
||||
$database = new Database();
|
||||
$db = $database->getConnection();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if (!empty($data->id) && !empty($data->latitude) && !empty($data->longitude)) {
|
||||
try {
|
||||
$query = "UPDATE spbu SET latitude = :latitude, longitude = :longitude WHERE id = :id";
|
||||
$stmt = $db->prepare($query);
|
||||
|
||||
$stmt->bindParam(":latitude", $data->latitude);
|
||||
$stmt->bindParam(":longitude", $data->longitude);
|
||||
$stmt->bindParam(":id", $data->id);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
http_response_code(200);
|
||||
echo json_encode(array("message" => "Posisi SPBU berhasil diupdate"));
|
||||
} else {
|
||||
http_response_code(503);
|
||||
echo json_encode(array("message" => "Gagal mengupdate posisi"));
|
||||
}
|
||||
} catch(PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(array("message" => "Error: " . $e->getMessage()));
|
||||
}
|
||||
} else {
|
||||
http_response_code(400);
|
||||
echo json_encode(array("message" => "Data tidak lengkap"));
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Methods: PUT");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
require_once '../config/database.php';
|
||||
|
||||
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
|
||||
http_response_code(401);
|
||||
echo json_encode(array("message" => "Unauthorized"));
|
||||
exit();
|
||||
}
|
||||
|
||||
$database = new Database();
|
||||
$db = $database->getConnection();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if (!empty($data->id)) {
|
||||
try {
|
||||
$query = "UPDATE spbu SET nama = :nama, no_spbu = :no_spbu, status = :status, latitude = :latitude, longitude = :longitude WHERE id = :id";
|
||||
$stmt = $db->prepare($query);
|
||||
|
||||
$stmt->bindParam(":nama", $data->nama);
|
||||
$stmt->bindParam(":no_spbu", $data->no_spbu);
|
||||
$stmt->bindParam(":status", $data->status);
|
||||
$stmt->bindParam(":latitude", $data->latitude);
|
||||
$stmt->bindParam(":longitude", $data->longitude);
|
||||
$stmt->bindParam(":id", $data->id);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
http_response_code(200);
|
||||
echo json_encode(array("message" => "SPBU berhasil diupdate"));
|
||||
} else {
|
||||
http_response_code(503);
|
||||
echo json_encode(array("message" => "Gagal mengupdate SPBU"));
|
||||
}
|
||||
} catch(PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(array("message" => "Error: " . $e->getMessage()));
|
||||
}
|
||||
} else {
|
||||
http_response_code(400);
|
||||
echo json_encode(array("message" => "ID tidak ditemukan"));
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
class Database {
|
||||
private $host;
|
||||
private $port;
|
||||
private $db_name;
|
||||
private $username;
|
||||
private $password;
|
||||
public $conn;
|
||||
|
||||
public function __construct() {
|
||||
// Load environment variables dari file .env
|
||||
$this->loadEnv();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load environment variables dari file .env
|
||||
*/
|
||||
private function loadEnv() {
|
||||
$envFile = dirname(__DIR__) . '/.env';
|
||||
|
||||
if (!file_exists($envFile)) {
|
||||
// Jika .env tidak ada, coba copy dari .env.example
|
||||
$envExample = dirname(__DIR__) . '/.env.example';
|
||||
if (file_exists($envExample)) {
|
||||
copy($envExample, $envFile);
|
||||
} else {
|
||||
die('File .env tidak ditemukan! Silakan buat file .env berdasarkan .env.example');
|
||||
}
|
||||
}
|
||||
|
||||
// Baca file .env
|
||||
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
$env = [];
|
||||
|
||||
foreach ($lines as $line) {
|
||||
// Skip komentar
|
||||
if (strpos(trim($line), '#') === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Parse key=value
|
||||
if (strpos($line, '=') !== false) {
|
||||
list($key, $value) = explode('=', $line, 2);
|
||||
$key = trim($key);
|
||||
$value = trim($value);
|
||||
$value = trim($value, '"\'');
|
||||
$env[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Set database configuration
|
||||
$this->host = $env['DB_HOST'] ?? 'localhost';
|
||||
$this->port = $env['DB_PORT'] ?? '3307';
|
||||
$this->db_name = $env['DB_NAME'] ?? 'spbu_db';
|
||||
$this->username = $env['DB_USER'] ?? 'root';
|
||||
$this->password = $env['DB_PASSWORD'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get database connection
|
||||
*/
|
||||
public function getConnection() {
|
||||
$this->conn = null;
|
||||
try {
|
||||
$dsn = "mysql:host=" . $this->host . ";port=" . $this->port . ";dbname=" . $this->db_name;
|
||||
|
||||
$this->conn = new PDO(
|
||||
$dsn,
|
||||
$this->username,
|
||||
$this->password,
|
||||
array(
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
|
||||
)
|
||||
);
|
||||
} catch(PDOException $e) {
|
||||
// Log error daripada menampilkannya (untuk production)
|
||||
error_log("Database Connection Error: " . $e->getMessage());
|
||||
|
||||
// Untuk development, tampilkan error
|
||||
if ($this->isDevelopment()) {
|
||||
die("Connection error: " . $e->getMessage());
|
||||
} else {
|
||||
die("Database connection failed. Please try again later.");
|
||||
}
|
||||
}
|
||||
return $this->conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if running in development mode
|
||||
*/
|
||||
private function isDevelopment() {
|
||||
return true; // Set false untuk production
|
||||
}
|
||||
|
||||
/**
|
||||
* Get environment value
|
||||
*/
|
||||
public function getEnv($key, $default = null) {
|
||||
return $this->$key ?? $default;
|
||||
}
|
||||
}
|
||||
?>
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user