86 lines
3.1 KiB
JavaScript
86 lines
3.1 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const db = require('./db');
|
|
|
|
const app = express();
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
// Get all cocktails
|
|
app.get('/api/cocktails', (req, res) => {
|
|
db.all('SELECT * FROM cocktails', (err, rows) => {
|
|
if (err) res.status(500).json({ error: err.message });
|
|
else {
|
|
// Parse ingredienti da JSON string ad array
|
|
const cocktails = rows.map(row => ({
|
|
...row,
|
|
ingredienti: JSON.parse(row.ingredienti)
|
|
}));
|
|
res.json(cocktails);
|
|
}
|
|
});
|
|
});
|
|
|
|
// Get single cocktail by ID
|
|
app.get('/api/cocktails/:id', (req, res) => {
|
|
db.get('SELECT * FROM cocktails WHERE id = ?', [req.params.id], (err, row) => {
|
|
if (err) res.status(500).json({ error: err.message });
|
|
else if (!row) res.status(404).json({ error: 'Not found' });
|
|
else {
|
|
row.ingredienti = JSON.parse(row.ingredienti);
|
|
res.json(row);
|
|
}
|
|
});
|
|
});
|
|
|
|
// Get single cocktail by slug
|
|
app.get('/api/cocktails/slug/:slug', (req, res) => {
|
|
db.get('SELECT * FROM cocktails WHERE slug = ?', [req.params.slug], (err, row) => {
|
|
if (err) res.status(500).json({ error: err.message });
|
|
else if (!row) res.status(404).json({ error: 'Not found' });
|
|
else {
|
|
row.ingredienti = JSON.parse(row.ingredienti);
|
|
res.json(row);
|
|
}
|
|
});
|
|
});
|
|
|
|
// Add cocktail
|
|
app.post('/api/cocktails', (req, res) => {
|
|
const { slug, nome, autore, luogo, anno, ingredienti, metodo, bicchiere, ghiaccio, garnish, note, image } = req.body;
|
|
|
|
db.run(
|
|
'INSERT INTO cocktails (slug, nome, autore, luogo, anno, ingredienti, metodo, bicchiere, ghiaccio, garnish, note, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
[slug, nome, autore, luogo, anno, JSON.stringify(ingredienti), metodo, bicchiere, ghiaccio, garnish, note, image],
|
|
function(err) {
|
|
if (err) res.status(400).json({ error: err.message });
|
|
else res.status(201).json({ id: this.lastID, slug, nome, autore, luogo, anno, ingredienti, metodo, bicchiere, ghiaccio, garnish, note, image });
|
|
}
|
|
);
|
|
});
|
|
|
|
// Update cocktail
|
|
app.put('/api/cocktails/:id', (req, res) => {
|
|
const { slug, nome, autore, luogo, anno, ingredienti, metodo, bicchiere, ghiaccio, garnish, note, image } = req.body;
|
|
|
|
db.run(
|
|
'UPDATE cocktails SET slug = ?, nome = ?, autore = ?, luogo = ?, anno = ?, ingredienti = ?, metodo = ?, bicchiere = ?, ghiaccio = ?, garnish = ?, note = ?, image = ? WHERE id = ?',
|
|
[slug, nome, autore, luogo, anno, JSON.stringify(ingredienti), metodo, bicchiere, ghiaccio, garnish, note, image, req.params.id],
|
|
function(err) {
|
|
if (err) res.status(400).json({ error: err.message });
|
|
else res.json({ id: req.params.id, slug, nome, autore, luogo, anno, ingredienti, metodo, bicchiere, ghiaccio, garnish, note, image });
|
|
}
|
|
);
|
|
});
|
|
|
|
// Delete cocktail
|
|
app.delete('/api/cocktails/:id', (req, res) => {
|
|
db.run('DELETE FROM cocktails WHERE id = ?', [req.params.id], function(err) {
|
|
if (err) res.status(500).json({ error: err.message });
|
|
else res.json({ ok: true, deleted: this.changes });
|
|
});
|
|
});
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
app.listen(PORT, () => console.log(`Server su http://localhost:${PORT}`));
|