Untitled

 avatar
unknown
plain_text
a year ago
1.1 kB
3
Indexable
// Define Mongoose schema and model for Review only
const reviewSchema = new mongoose.Schema({
    breweryId: String,
    rating: Number,
    description: String
});

const Review = mongoose.model('Review', reviewSchema);

// API endpoint to add a review for a brewery
app.post('/api/breweries/:id/review', async (req, res) => {
    const { id } = req.params;
    const { rating, description } = req.body;

    try {
        const newReview = new Review({ breweryId: id, rating, description });
        await newReview.save();

        res.status(201).json(newReview);
    } catch (err) {
        console.error('Error adding review:', err);
        res.status(500).json({ error: 'Error adding review' });
    }
});

// GET endpoint to fetch reviews for a brewery
app.get('/api/breweries/:id/reviews', async (req, res) => {
    const { id } = req.params;

    try {
        const reviews = await Review.find({ breweryId: id });
        res.json(reviews);
    } catch (err) {
        console.error('Error fetching reviews:', err);
        res.status(500).json({ error: 'Error fetching reviews' });
    }
});
Editor is loading...
Leave a Comment