diff --git a/backend/database.js b/backend/database.js index e02622c..ddc7837 100644 --- a/backend/database.js +++ b/backend/database.js @@ -110,6 +110,7 @@ const User = sequelize.define('User', { } }); +// Review model const Review = sequelize.define("Review", { review_text: { type: DataTypes.TEXT, @@ -121,6 +122,7 @@ const Review = sequelize.define("Review", { }, }); +// sample events to test posting to db const sampleEvents = [ { name: 'Rock Fest 2024', @@ -301,7 +303,9 @@ const sampleEvents = [ } ]; -//sync database +//This code is used to sync the Sequelize models with the database (by dropping and recreating +// tables), insert sample event data, and fetch and log those events from the database. +// It's mainly used for testing or setting up initial data for development purposes. (async () => { try { //Changed to force: false, so the database won't recreate itself every time @@ -311,7 +315,7 @@ const sampleEvents = [ await Event.bulkCreate(sampleEvents); //TEMPORARY TO INSERT SAMPLE EVENTS console.log('Sample events inserted'); - const events = await Event.findAll(); + const events = await Event.findAll(); // to see what events are in db console.log('Events fetched from the database:', JSON.stringify(events, null, 2)); } catch (error) { console.error('Error creating database and inserting sample data:', error); diff --git a/backend/server.js b/backend/server.js index f6e7df6..54a8a58 100644 --- a/backend/server.js +++ b/backend/server.js @@ -205,27 +205,31 @@ app.delete("/events/:id", async (req, res) => { } }); +// API endpoint to post submitted reviews to the database; request object has both content of review +// and the event that is being reviewed app.post("/api/reviews", async (req, res) => { console.log("in /api/reviews"); // Handle review submission const { review_text, event_name } = req.body; - res.status(200).json({ review_text }); + res.status(200).json({ review_text }); // send just the textual content of review in response console.log("Server has received a review: ", review_text); - const newReview = await Review.create({ + const newReview = await Review.create({ // create entry in Reviews table review_text: req.body.review_text, event_name: req.body.event_name, }); }); +// API endpoint to retrieve submitted reviews from the database; ultimately, these are shown on each individual +// event's page app.get("/api/reviews", async (req, res) => { const eventName = req.query.event_name; try { - const reviews = await Review.findAll({ where: { event_name: eventName } }); + const reviews = await Review.findAll({ where: { event_name: eventName } }); // finding reviews for specific event res.json(reviews); - } catch (error) { + } catch (error) { // error catching console.error("Error fetching reviews:", error); res.status(500).json({ error: "Failed to fetch reviews" }); }