first
This commit is contained in:
70
backend/api/location.php
Normal file
70
backend/api/location.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
// backend/api/location.php
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: POST');
|
||||
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
||||
|
||||
require_once '../config/db.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'message' => 'Method Not Allowed']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$inputJSON = file_get_contents('php://input');
|
||||
$input = json_decode($inputJSON, true);
|
||||
|
||||
if (!$input || !isset($input['fingerprint']) || !isset($input['locations'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid data structure. Fingerprint and locations required.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$fingerprint = $input['fingerprint'];
|
||||
$locations = $input['locations'];
|
||||
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
$sql = "INSERT INTO driver_locations (fingerprint, latitude, longitude, speed, recorded_at, uploaded_at)
|
||||
VALUES (:fingerprint, :latitude, :longitude, :speed, :recorded_at, NOW())";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$insertedCount = 0;
|
||||
foreach ($locations as $loc) {
|
||||
$latitude = $loc['latitude'] ?? null;
|
||||
$longitude = $loc['longitude'] ?? null;
|
||||
$speed = $loc['speed'] ?? 0;
|
||||
$timestamp = $loc['timestamp'] ?? null;
|
||||
|
||||
if ($latitude !== null && $longitude !== null && $timestamp !== null) {
|
||||
$stmt->execute([
|
||||
':fingerprint' => $fingerprint,
|
||||
':latitude' => $latitude,
|
||||
':longitude' => $longitude,
|
||||
':speed' => $speed,
|
||||
':recorded_at' => date('Y-m-d H:i:s', $timestamp / 1000)
|
||||
]);
|
||||
$insertedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
|
||||
http_response_code(201);
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => "Bulk location sync successful",
|
||||
'synced_count' => $insertedCount
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
61
backend/api/rides.php
Normal file
61
backend/api/rides.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
// backend/api/rides.php
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: POST');
|
||||
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
||||
|
||||
require_once '../config/db.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'message' => 'Method Not Allowed']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$inputJSON = file_get_contents('php://input');
|
||||
$input = json_decode($inputJSON, true);
|
||||
|
||||
if (!$input) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid JSON input']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$platform = $input['platform'] ?? 'Unknown';
|
||||
$price = $input['price'] ?? 0.0;
|
||||
$pickupDistance = $input['pickupDistance'] ?? 'Unknown';
|
||||
$dropoffDistance = $input['dropoffDistance'] ?? 'Unknown';
|
||||
$timeToPickup = $input['timeToPickup'] ?? 'Unknown';
|
||||
$isAccepted = isset($input['isAccepted']) ? (int)$input['isAccepted'] : 0;
|
||||
$rawText = $input['rawText'] ?? '';
|
||||
$fingerprint = $input['fingerprint'] ?? 'UNKNOWN_DEVICE';
|
||||
|
||||
try {
|
||||
$sql = "INSERT INTO rides (fingerprint, platform, price, pickup_distance, dropoff_distance, time_to_pickup, is_accepted, raw_text, created_at)
|
||||
VALUES (:fingerprint, :platform, :price, :pickup_distance, :dropoff_distance, :time_to_pickup, :is_accepted, :raw_text, NOW())";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
':fingerprint' => $fingerprint,
|
||||
':platform' => $platform,
|
||||
':price' => $price,
|
||||
':pickup_distance' => $pickupDistance,
|
||||
':dropoff_distance' => $dropoffDistance,
|
||||
':time_to_pickup' => $timeToPickup,
|
||||
':is_accepted' => $isAccepted,
|
||||
':raw_text' => $rawText
|
||||
]);
|
||||
|
||||
http_response_code(201);
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Ride record created successfully',
|
||||
'ride_id' => $pdo->lastInsertId()
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user