Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destinations/endpoint #297

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions server/controllers/destinationController.js
Original file line number Diff line number Diff line change
@@ -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 };
39 changes: 32 additions & 7 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -25,26 +33,43 @@ 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,
message: `Place ID ${placeId} saved successfully`,
});
});

// 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}`);
}
});
23 changes: 23 additions & 0 deletions server/prisma/migrations/destinations/migration.sql
Original file line number Diff line number Diff line change
@@ -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();
11 changes: 11 additions & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions server/routes/v2.js
Original file line number Diff line number Diff line change
@@ -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;