GardenPlanner/api/server.js

127 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-06-25 10:13:39 +02:00
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { Pool } = require('pg');
const app = express();
const PORT = 3000;
app.use(cors()); // Enable CORS
app.use(bodyParser.json()); // Parse JSON request bodies
const pool = new Pool({
host: 'gardenplanner-db',
port: 5432,
database: 'gardenPlaner',
user: 'garden',
password: 'garden',
});
app.get('/plants/:id', (req, res) => {
const plantId = req.params.id;
pool.query('SELECT * FROM plants WHERE id = $1', [plantId])
.then((result) => {
const plant = result.rows[0];
if (!plant) {
res.status(404).json({ error: 'Pflanze nicht gefunden' });
return;
}
pool.query('SELECT * FROM plant_times WHERE plant_id = $1', [plantId])
.then((result) => {
const plantTimes = result.rows;
const plantWithTimes = {
...plant,
times: plantTimes
};
res.json(plantWithTimes);
})
.catch((error) => {
console.error('Fehler beim landen der Zeiten', error);
res.status(500).json({ error: 'Fehler beim landen der Zeiten' });
});
})
.catch((error) => {
console.error('Fehler beim landen der Zeiten:', error);
res.status(500).json({ error: 'Fehler beim landen der Zeiten' });
});
});
app.get('/plants', (req, res) => {
Promise.all([
pool.query('SELECT * FROM plants'),
pool.query('SELECT * FROM plant_times')
])
.then(([plantsResult, plantTimesResult]) => {
const rawPlants = plantsResult.rows;
const plantTimes = plantTimesResult.rows;
const plants = rawPlants.map((plant) => ({
...plant,
times: plantTimes.filter((time) => time.plant_id === plant.id)
}));
res.json(plants);
})
.catch((error) => {
console.error('Failed to fetch plants and plant times:', error);
res.status(500).json({ error: 'Failed to fetch plants and plant times' });
});
});
app.get('/beet', (req, res) => {
pool.query('SELECT * FROM beets')
.then(( beetResult) => {
const beet = beetResult.rows;
res.json(beet);
})
.catch((error) => {
console.error('Failed to fetch plants and plant times:', error);
res.status(500).json({ error: 'Failed to fetch plants and plant times' });
});
});
app.post('/beet', (req, res) => {
const beetEntries = req.body;
console.log(beetEntries);
if (!beetEntries || !Array.isArray(beetEntries)) {
res.status(400).json({ error: 'falscher body' });
return;
}
const clearQuery = 'DELETE FROM beets';
const insertQuery = 'INSERT INTO beets (plant_id, position, beet_row) VALUES ($1, $2, $3)';
const values = beetEntries.map(({ plantId, position, beet_row }) => [plantId, position, beet_row]);
pool
.connect()
.then((client) => {
return client
.query(clearQuery) // Clear the beets table
.then(() =>
Promise.all(
values.map((params) => client.query(insertQuery, params))
)
)
.finally(() => client.release());
})
.then(() => {
res.json({ message: 'Beet gespeichert' });
})
.catch((error) => {
console.error('Fehler beim speichern:', error);
res.status(500).json({ error: 'Fehler beim speichern' });
});
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});