drivers api endpoint created
This commit is contained in:
committed by
GitHub
parent
945a1b96e0
commit
c5c50e75db
@@ -0,0 +1,15 @@
|
|||||||
|
import { neon } from "@neondatabase/serverless";
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
const sql = neon(process.env.DATABASE_URL!);
|
||||||
|
|
||||||
|
const response = await sql`SELECT * FROM drivers`;
|
||||||
|
|
||||||
|
return Response.json({ data: response });
|
||||||
|
} catch (error) {
|
||||||
|
console.log("[GET_DRIVERS]: ", error);
|
||||||
|
|
||||||
|
return Response.json({ error }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import { neon } from "@neondatabase/serverless";
|
||||||
|
|
||||||
|
export async function GET(request: Request, { id }: { id: string }) {
|
||||||
|
if (!id)
|
||||||
|
return Response.json({ error: "Missing required fields" }, { status: 400 });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const sql = neon(`${process.env.DATABASE_URL}`);
|
||||||
|
const response = await sql`
|
||||||
|
SELECT
|
||||||
|
rides.ride_id,
|
||||||
|
rides.origin_address,
|
||||||
|
rides.destination_address,
|
||||||
|
rides.origin_latitude,
|
||||||
|
rides.origin_longitude,
|
||||||
|
rides.destination_latitude,
|
||||||
|
rides.destination_longitude,
|
||||||
|
rides.ride_time,
|
||||||
|
rides.fare_price,
|
||||||
|
rides.payment_status,
|
||||||
|
rides.created_at,
|
||||||
|
'driver', json_build_object(
|
||||||
|
'driver_id', drivers.id,
|
||||||
|
'first_name', drivers.first_name,
|
||||||
|
'last_name', drivers.last_name,
|
||||||
|
'profile_image_url', drivers.profile_image_url,
|
||||||
|
'car_image_url', drivers.car_image_url,
|
||||||
|
'car_seats', drivers.car_seats,
|
||||||
|
'rating', drivers.rating
|
||||||
|
) AS driver
|
||||||
|
FROM
|
||||||
|
rides
|
||||||
|
INNER JOIN
|
||||||
|
drivers ON rides.driver_id = drivers.id
|
||||||
|
WHERE
|
||||||
|
rides.user_id = ${id}
|
||||||
|
ORDER BY
|
||||||
|
rides.created_at DESC;
|
||||||
|
`;
|
||||||
|
|
||||||
|
return Response.json({ data: response });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[GET_RIDE]: ", error);
|
||||||
|
return Response.json({ error: "Internal Server Error" }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user