-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoints_api.php
More file actions
156 lines (125 loc) · 4.3 KB
/
points_api.php
File metadata and controls
156 lines (125 loc) · 4.3 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
header("Content-Type: application/json");
include "dbcon.php";
// =========================================================
// 1. GET METHOD → RETURN USER POINTS
// =========================================================
if ($_SERVER["REQUEST_METHOD"] === "GET") {
if (!isset($_GET['id']) || empty($_GET['id'])) {
echo json_encode(["status" => "error", "message" => "Missing or empty user ID"]);
exit;
}
$userID = $_GET['id'];
$stmt = $conn->prepare("SELECT points_total FROM users WHERE user_unique_id=?");
$stmt->bind_param("s", $userID);
$stmt->execute();
$res = $stmt->get_result();
if ($res->num_rows === 0) {
echo json_encode(["status" => "error", "message" => "User not found"]);
exit;
}
$user = $res->fetch_assoc();
echo json_encode([
"status" => "success",
"mode" => "fetch_points",
"user_id" => $userID,
"points_total" => (int)$user["points_total"]
]);
exit;
}
// =========================================================
// 2. POST METHOD → ADD OR DEDUCT POINTS
// =========================================================
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Get JSON body
$input = file_get_contents("php://input");
$data = json_decode($input, true);
if (!$data ||
!isset($data["user_unique_id"]) ||
!isset($data["points_added"])) {
echo json_encode([
"status" => "error",
"message" => "Missing required fields: user_unique_id or points_added"
]);
exit;
}
$userID = $data["user_unique_id"];
$points_added = (int)$data["points_added"];
$description = $data["description"] ?? "No description";
$source = $data["source"] ?? "ESP32";
// Check user exists
$stmt = $conn->prepare("SELECT points_total FROM users WHERE user_unique_id=?");
$stmt->bind_param("s", $userID);
$stmt->execute();
$res = $stmt->get_result();
if ($res->num_rows === 0) {
echo json_encode([
"status" => "error",
"message" => "User not found"
]);
exit;
}
$user = $res->fetch_assoc();
$currentPoints = (int)$user["points_total"];
// Check if deduction exceeds current points
if ($points_added < 0) {
$deduction = abs($points_added);
if ($currentPoints < $deduction) {
echo json_encode([
"status" => "error",
"message" => "Not enough points",
"current_points" => $currentPoints,
"required" => $deduction
]);
exit;
}
}
// ================================================
// SAFE DATABASE TRANSACTION
// ================================================
$conn->begin_transaction();
try {
// Update points
$stmt = $conn->prepare("
UPDATE users
SET points_total = points_total + ?
WHERE user_unique_id=?
");
$stmt->bind_param("is", $points_added, $userID);
$stmt->execute();
$stmt->close();
// Insert transaction log
$stmt = $conn->prepare("
INSERT INTO transactions (user_unique_id, points_added, description, source, created_at)
VALUES (?, ?, ?, ?, NOW())
");
$stmt->bind_param("siss", $userID, $points_added, $description, $source);
$stmt->execute();
$stmt->close();
// Commit everything
$conn->commit();
echo json_encode([
"status" => "success",
"mode" => ($points_added < 0 ? "claim" : "deposit"),
"user_id" => $userID,
"points_change" => $points_added
]);
}
catch (Exception $e) {
$conn->rollback();
echo json_encode([
"status" => "error",
"message" => "Database error: " . $e->getMessage()
]);
}
exit;
}
// =========================================================
// 3. OTHER METHODS → NOT ALLOWED
// =========================================================
echo json_encode([
"status" => "error",
"message" => "Invalid request method"
]);
exit;
?>