Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
1.2 kB
1
Indexable
Never
/ GET endpoint to fetch all reviews for a brewery
app.get('/api/breweries/:id/reviews', async (req, res) => {
    try {
        const breweryId = req.params.id;

        // Fetch reviews for the brewery from the database
        const reviews = await Review.find({ breweryId });

        res.json(reviews);
    } catch (err) {
        console.error('Error fetching reviews:', err);
        res.status(500).json({ error: 'Server error' });
    }
});

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

        // Validate if the breweryId is valid
        const brewery = await Brewery.findById(breweryId);
        if (!brewery) {
            return res.status(404).json({ error: 'Brewery not found' });
        }

        // Create new review
        const newReview = new Review({ breweryId, rating, description });
        await newReview.save();

        res.status(201).json({ message: 'Review added successfully' });
    } catch (err) {
        console.error('Error adding review:', err);
        res.status(500).json({ error: 'Error adding review' });
    }
});
Leave a Comment