diff --git a/server/controllers/destinationController.js b/server/controllers/destinationController.js new file mode 100644 index 00000000..7580bd0d --- /dev/null +++ b/server/controllers/destinationController.js @@ -0,0 +1,17 @@ +// controllers/destinationController.js + +const { PrismaClient } = require('@prisma/client'); +const prisma = new PrismaClient(); + +// Fetch all destinations +const getAllDestinations = async (req, res) => { + try { + const destinations = await prisma.destination.findMany(); + res.status(200).json({ success: true, data: destinations }); + } catch (error) { + console.error('Error fetching destinations:', error); + res.status(500).json({ success: false, message: 'Failed to fetch destinations' }); + } +}; + +module.exports = { getAllDestinations }; diff --git a/server/index.js b/server/index.js index e61139b8..5d299981 100644 --- a/server/index.js +++ b/server/index.js @@ -2,16 +2,24 @@ const express = require("express"); const cors = require("cors"); const products = require("./src/data/places.json"); const dotenv = require("dotenv"); -// const prisma = require(".db/prisma"); +const v2Routes = require('./routes/v2'); +// Load environment variables dotenv.config(); const app = express(); -const PORT = 8800; +// Use PORT from environment or fallback to 4000 +const PORT = process.env.PORT || 8800; + +// Middleware app.use(cors()); app.use(express.json()); +// Routes +app.use('/api/v2', v2Routes); + +// API Endpoints app.get("/places/:id", (req, res) => { const placeId = parseInt(req.params.id); const place = products.find((p) => p.id === placeId); @@ -25,6 +33,14 @@ app.get("/places/:id", (req, res) => { app.post("/savePlace", (req, res) => { const { placeId } = req.body; + + if (!placeId) { + return res.status(400).json({ + success: false, + message: "Place ID is required.", + }); + } + console.log(`Received place ID: ${placeId}`); res.json({ success: true, @@ -32,19 +48,28 @@ app.post("/savePlace", (req, res) => { }); }); -// import Routes +// Import Additional Routes const placesRoutes = require("./routes/places"); const destinationsRoutes = require("./routes/destinations"); const searchedPlacesRoutes = require("./routes/searchedPlaces"); const bookingsRoutes = require("./routes/bookings"); -// Use Routes +// Use Additional Routes app.use("/places", placesRoutes); app.use("/destinations", destinationsRoutes); app.use("/s", searchedPlacesRoutes); app.use("/bookings", bookingsRoutes); -app.listen(PORT, (error) => { - if (error) console.log("Error starting the server:", error); - else console.log(`Server listening on PORT ${PORT}`); +// Fallback for undefined routes +app.use((req, res) => { + res.status(404).json({ error: "Endpoint not found." }); }); + +// Start Server +app.listen(PORT, (error) => { + if (error) { + console.error("Error starting the server:", error); + } else { + console.log(`Server is running on http://localhost:${PORT}`); + } +}); \ No newline at end of file diff --git a/server/prisma/migrations/destinations/migration.sql b/server/prisma/migrations/destinations/migration.sql new file mode 100644 index 00000000..1d0d00c3 --- /dev/null +++ b/server/prisma/migrations/destinations/migration.sql @@ -0,0 +1,23 @@ +CREATE TABLE "Destination" ( + "id" SERIAL NOT NULL PRIMARY KEY, + "name" TEXT NOT NULL, + "location" TEXT NOT NULL, + "description" TEXT NOT NULL, + "price" TEXT NOT NULL, + "imageUrl" TEXT NOT NULL, + "createdAt" TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL, + "updatedAt" TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL +); + +-- Triggers or additional logic to update "updatedAt" when rows are modified +CREATE OR REPLACE FUNCTION update_updatedAt_column() +RETURNS TRIGGER AS $$ +BEGIN + NEW."updatedAt" = CURRENT_TIMESTAMP; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER update_destination_updatedAt +BEFORE UPDATE ON "Destination" +FOR EACH ROW EXECUTE FUNCTION update_updatedAt_column(); diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma index 8db3f356..50e6bdb7 100644 --- a/server/prisma/schema.prisma +++ b/server/prisma/schema.prisma @@ -72,4 +72,15 @@ model Place { descriptionSpace String guestAccess String otherThings String +} + +model Destination { + id Int @id @default(autoincrement()) + name String + location String + description String + price String + imageUrl String + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt } \ No newline at end of file diff --git a/server/routes/v2.js b/server/routes/v2.js new file mode 100644 index 00000000..016a3a1b --- /dev/null +++ b/server/routes/v2.js @@ -0,0 +1,8 @@ +const express = require('express'); +const { getAllDestinations } = require('../controllers/destinationController'); +const router = express.Router(); + +// Define the destinations endpoint +router.get('/destinations', getAllDestinations); + +module.exports = router;