93 lines
3.0 KiB
JavaScript
93 lines
3.0 KiB
JavaScript
import { MongoAPIError, ObjectId } from 'mongodb';
|
|
import bcrypt from 'bcrypt';
|
|
import { getCollections, update_pflux } from '../db/mongo.js';
|
|
|
|
export function registerApiRoutes(app, requireLogin) {
|
|
const { usersCollection, prop_fluxCollection } = getCollections();
|
|
|
|
app.get('/api/check-email', async (req, res) => {
|
|
const email = (req.query.email || '').toLowerCase().trim();
|
|
if (!email) return res.json({ exists: false });
|
|
try {
|
|
const existingUser = await usersCollection.findOne({ email });
|
|
res.json({ exists: !!existingUser });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Fehler bei der E-Mail-Prüfung' });
|
|
}
|
|
});
|
|
|
|
app.post('/api/save', requireLogin, async (req, res) => {
|
|
let { espId, sensorNumber, name, description, address } = req.body;
|
|
if (!espId || !sensorNumber) {
|
|
return res.json({ error: 'ESP-ID und Sensornummer sind Pflichtfelder' });
|
|
}
|
|
sensorNumber = parseInt(sensorNumber, 10);
|
|
try {
|
|
const doc = {
|
|
id: espId,
|
|
name: name || '',
|
|
description: description || '',
|
|
createdAt: new Date()
|
|
};
|
|
await update_pflux(sensorNumber, doc)
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Fehler beim Speichern' });
|
|
}
|
|
});
|
|
|
|
app.put('/api/update/:id', requireLogin, async (req, res) => {
|
|
const { id } = req.params;
|
|
let { espId, sensorNumber, name, description, address } = req.body;
|
|
if (!espId || !sensorNumber) {
|
|
return res.json({ error: 'ESP-ID und Sensornummer sind Pflichtfelder' });
|
|
}
|
|
sensorNumber = parseInt(sensorNumber, 10);
|
|
try {
|
|
await prop_fluxCollection.updateOne(
|
|
{ _id: new ObjectId(id) },
|
|
{ $set: { espId, sensorNumber, name, description, address } }
|
|
);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Fehler beim Aktualisieren' });
|
|
}
|
|
});
|
|
|
|
app.get('/api/list', requireLogin, async (req, res) => {
|
|
const { id } = req.query;
|
|
if (id) {
|
|
try {
|
|
const item = await prop_fluxCollection.findOne({ _id: new ObjectId(id) });
|
|
if (item) return res.json([item]);
|
|
return res.json([]);
|
|
} catch (err) {
|
|
console.error(err);
|
|
return res.status(500).json({ error: 'Fehler beim Laden' });
|
|
}
|
|
}
|
|
const page = parseInt(req.query.page) || 1;
|
|
const limit = parseInt(req.query.limit) || 10;
|
|
const skip = (page - 1) * limit;
|
|
try {
|
|
const items = await prop_fluxCollection.find({chip: {$exists: true}})
|
|
.sort({ "chip.createdAt": -1 })
|
|
.skip(skip)
|
|
.limit(limit)
|
|
.toArray();
|
|
res.json(items);
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Fehler beim Laden' });
|
|
}
|
|
});
|
|
|
|
app.delete('/api/delete/:id', requireLogin, async (req, res) => {
|
|
await prop_fluxCollection.deleteOne({ _id: new ObjectId(req.params.id) });
|
|
res.json({ success: true });
|
|
});
|
|
}
|