feat: enhance player model with organizationId and update related API handling

This commit is contained in:
Lars Behrends
2025-12-29 11:22:44 +01:00
parent 5fe6724663
commit 5fcd5dbdcb
4 changed files with 30 additions and 72 deletions

View File

@@ -153,11 +153,11 @@ app.get('/api/players', (req, res) => {
const parsed = rows.map(r => ({
...r,
tags: JSON.parse(r.tags || '[]'),
stats: JSON.parse(r.minecraftStats || '{}'),
//stats: JSON.parse(r.stats || '{}'),
//minecraftStats: r.minecraftStats ? JSON.parse(r.minecraftStats) : undefined,
stats: JSON.parse(r.stats || '{}'),
minecraftStats: r.minecraftStats ? JSON.parse(r.minecraftStats) : undefined,
inventory: JSON.parse(r.inventory || '[]'),
isOnline: !!r.isOnline
isOnline: !!r.isOnline,
organizationId: r.organizationId || undefined
}));
res.json(parsed);
});
@@ -189,60 +189,11 @@ app.put('/api/players/:uuid', (req, res) => {
}
const updates = req.body;
const allowedFields = ['storyMarkdown', 'tags'];
const allowedFields = ['storyMarkdown', 'tags', 'organizationId'];
const updateFields = [];
const values = [];
// Handle organizationId specially - it goes into stats JSON
if (updates.organizationId !== undefined) {
// First get current player data
db.get("SELECT stats FROM players WHERE uuid = ?", [req.params.uuid], (err, row) => {
if (err) return res.status(500).json({error: err.message});
if (!row) return res.status(404).json({error: 'Spieler nicht gefunden'});
try {
const currentStats = JSON.parse(row.stats || '{}');
const updatedStats = { ...currentStats, organizationId: updates.organizationId };
updateFields.push('stats = ?');
values.push(JSON.stringify(updatedStats));
} catch (e) {
return res.status(500).json({error: 'Fehler beim Verarbeiten der Stats'});
}
// Continue with other fields
for (const field of allowedFields) {
if (updates[field] !== undefined) {
if (field === 'tags') {
// Tags are stored as JSON
updateFields.push(`${field} = ?`);
values.push(JSON.stringify(updates[field]));
} else {
updateFields.push(`${field} = ?`);
values.push(updates[field]);
}
}
}
if (updateFields.length === 0) {
return res.status(400).json({error: 'Keine gültigen Felder zum Aktualisieren'});
}
const query = `UPDATE players SET ${updateFields.join(', ')} WHERE uuid = ?`;
values.push(req.params.uuid);
db.run(query, values, function(err) {
if (err) {
console.error('Error updating player:', err);
return res.status(500).json({error: 'Fehler beim Aktualisieren'});
}
res.json({success: true, message: 'Spieler erfolgreich aktualisiert'});
});
});
return; // Exit early since we're handling this asynchronously
}
// Handle other fields normally
// Handle fields
for (const field of allowedFields) {
if (updates[field] !== undefined) {
if (field === 'tags') {