first
This commit is contained in:
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