-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_wishlist.php
More file actions
87 lines (74 loc) · 3.47 KB
/
Copy pathapi_wishlist.php
File metadata and controls
87 lines (74 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
// Aktifkan pelaporan error mysqli sebagai exception untuk penanganan yang lebih baik
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
header('Content-Type: application/json');
// Fungsi terpusat untuk mengirim respons JSON dan menghentikan skrip
function send_json_response($status_code, $status, $message) {
http_response_code($status_code);
echo json_encode(['status' => $status, 'message' => $message]);
exit;
}
// Safety net: tangani error fatal yang tidak tertangkap oleh try-catch
set_error_handler(function($errno, $errstr, $errfile, $errline) {
// error_log("API Wishlist Error: $errstr in $errfile on line $errline"); // Opsional: log error
send_json_response(500, 'error', 'Terjadi kesalahan internal pada server API.');
});
// Baru mulai memuat file dan logika setelah error handler siap
require_once 'config_web.php';
session_start();
// Keamanan: Pastikan pengguna sudah login
if (!isset($_SESSION['user_id'])) {
send_json_response(403, 'error', 'Anda harus login untuk menggunakan fitur ini.');
}
$user_id = $_SESSION['user_id'];
$data = json_decode(file_get_contents('php://input'), true);
$action = $data['action'] ?? '';
// Pastikan koneksi database berhasil
if ($conn_web->connect_error) {
send_json_response(500, 'error', 'Koneksi database gagal.');
}
try {
switch ($action) {
case 'add':
$kode_produk = $data['kode'] ?? '';
if (empty($kode_produk)) {
send_json_response(400, 'error', 'Data produk tidak lengkap.');
}
$stmt_check = $conn_web->prepare("SELECT id FROM wishlist WHERE user_id = ? AND kode_produk = ?");
$stmt_check->bind_param("is", $user_id, $kode_produk);
$stmt_check->execute();
$stmt_check->store_result();
if ($stmt_check->num_rows > 0) {
send_json_response(200, 'info', 'Produk sudah ada di wishlist Anda.');
} else {
$stmt_insert = $conn_web->prepare("INSERT INTO wishlist (user_id, kode_produk, nama_produk, artikel, harga, gambar_url) VALUES (?, ?, ?, ?, ?, ?)");
$stmt_insert->bind_param("isssss", $user_id, $data['kode'], $data['nama'], $data['artikel'], $data['harga'], $data['gambar']);
if ($stmt_insert->execute()) {
send_json_response(200, 'success', 'Produk berhasil ditambahkan ke wishlist.');
}
}
$stmt_check->close();
break;
case 'remove':
$kode_produk = $data['kode'] ?? '';
if (empty($kode_produk)) { send_json_response(400, 'error', 'Kode produk dibutuhkan.'); }
$stmt = $conn_web->prepare("DELETE FROM wishlist WHERE user_id = ? AND kode_produk = ?");
$stmt->bind_param("is", $user_id, $kode_produk);
if ($stmt->execute()) { send_json_response(200, 'success', 'Produk berhasil dihapus dari wishlist.'); }
$stmt->close();
break;
default:
send_json_response(400, 'error', 'Aksi tidak valid.');
break;
}
} catch (mysqli_sql_exception $e) {
// Tangkap semua kemungkinan error database
// error_log($e->getMessage()); // Opsional: log error di server
send_json_response(500, 'error', 'Terjadi kesalahan pada database.');
} catch (Throwable $th) {
// Tangkap semua error lainnya
// error_log($th->getMessage());
send_json_response(500, 'error', 'Terjadi kesalahan umum pada server.');
}
$conn_web->close();
?>