Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.4 kB
1
Indexable
Never
// API endpoint to get brewery details by ID
app.get('/api/breweries/:id', async (req, res) => {
    try {
        const breweryId = req.params.id;
        const brewery = await Brewery.findById(breweryId);
        if (!brewery) {
            return res.status(404).json({ error: 'Brewery not found' });
        }
        res.json(brewery);
    } catch (err) {
        console.error('Error fetching brewery details:', err);
        res.status(500).json({ error: 'Server error' });
    }
});

// API endpoint to get reviews for a brewery by ID
app.get('/api/breweries/:id/reviews', async (req, res) => {
    try {
        const breweryId = req.params.id;
        const reviews = await Review.find({ breweryId });
        res.json(reviews);
    } catch (err) {
        console.error('Error fetching reviews:', err);
        res.status(500).json({ error: 'Server error' });
    }
});

// API endpoint to add a new review for a brewery
app.post('/api/breweries/:id/reviews', async (req, res) => {
    try {
        const { breweryId, rating, description } = req.body;
        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