58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
<?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"));
|
|
}
|
|
?>
|