app.post('/api/products', (req, res, next) => {
const product = new Product({
...req.body
});
product.save()
.then(() => res.status(201).json({ product }))
.catch(error => res.status(400).json({ error }));
});
app.get('/api/products/:id', (req, res, next) => {
Product.findOne({ _id: req.params.id })
.then(product => res.status(200).json({ product }))
.catch(error => res.status(404).json({ error }));
});
app.put('/api/products/:id', (req, res, next) => {
Product.updateOne({ _id: req.params.id }, { ...req.body, _id: req.params.id })
.then(() => res.status(200).json({ message: 'Objet modifié !'}))
.catch(error => res.status(400).json({ error }));
});
app.delete('/api/products/:id', (req, res, next) => {
Product.deleteOne({ _id: req.params.id })
.then(() => res.status(200).json({ message: 'Deleted !'}))
.catch(error => res.status(400).json({ error }));
});
app.get('/api/products', (req, res, next) => {
Product.find()
.then(products => res.status(200).json({ products }))
.catch(error => res.status(400).json({ error }));
});
Modifié par DiegoGaby (19 Apr 2020 - 02:37)