Initial Commit: Tugas Semua Pertemuan

This commit is contained in:
2026-06-08 22:37:30 +07:00
commit bdfa23b412
50 changed files with 6888 additions and 0 deletions
+58
View File
@@ -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"));
}
?>
+19
View File
@@ -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
));
}
?>
+85
View File
@@ -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"));
}
?>
+36
View File
@@ -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()));
}
?>
+51
View File
@@ -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"));
}
?>
+10
View File
@@ -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"));
?>
+46
View File
@@ -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"));
}
?>
+49
View File
@@ -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"));
}
?>