-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sui Zer
committed
Mar 21, 2024
1 parent
080e905
commit 133753e
Showing
5 changed files
with
52 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
// Handle POST requests - Create a new location | ||
if (req.method === "POST") { | ||
const { lat, lon } = req.body; | ||
try { | ||
// Validate lat and lon are numbers | ||
if (typeof lat !== "number" || typeof lon !== "number") { | ||
return res | ||
.status(400) | ||
.json({ error: "Latitude and longitude must be valid numbers" }); | ||
} | ||
|
||
const newLocation = await prisma.location.create({ | ||
data: { | ||
lat, | ||
lon, | ||
}, | ||
}); | ||
res.status(200).json(newLocation); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ error: "Failed to create location" }); | ||
} | ||
} | ||
// Handle GET requests - List all locations | ||
else if (req.method === "GET") { | ||
try { | ||
const locations = await prisma.location.findMany(); | ||
res.status(200).json(locations); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ error: "Failed to retrieve locations" }); | ||
} | ||
} | ||
// Respond with 405 Method Not Allowed if the request method is not supported | ||
else { | ||
res.status(405).json({ error: "Method Not Allowed" }); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters