2025-10-24 20:27:18 +02:00
|
|
|
import { Router, Request, Response } from 'express';
|
2025-10-25 17:02:42 +02:00
|
|
|
import { dbAdapter } from '../db';
|
2025-10-24 20:27:18 +02:00
|
|
|
|
|
|
|
|
const router = Router();
|
|
|
|
|
|
|
|
|
|
// Get fields for a list
|
|
|
|
|
router.get('/list/:listId', async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
2025-10-25 17:02:42 +02:00
|
|
|
const fields = await dbAdapter.queryAll('SELECT * FROM fields WHERE listId = ? AND isDeleted = 0 ORDER BY "order" ASC', [req.params.listId]);
|
2025-10-24 20:27:18 +02:00
|
|
|
const formattedFields = (fields as any[]).map(field => ({ ...field, isDeleted: !!field.isDeleted }));
|
|
|
|
|
res.json(formattedFields);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
res.status(500).json({ error: 'Failed to fetch fields' });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Create field
|
|
|
|
|
router.post('/', async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
2025-11-12 00:15:16 +01:00
|
|
|
const { id, name, fieldType, fieldOptions, alignment, listId, order, createdAt, updatedAt, isDeleted } = req.body;
|
2025-10-25 17:02:42 +02:00
|
|
|
await dbAdapter.execute(
|
2025-11-12 00:15:16 +01:00
|
|
|
'INSERT INTO fields (id, name, fieldType, fieldOptions, alignment, listId, "order", createdAt, updatedAt, isDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
|
|
|
[id, name, fieldType, fieldOptions, alignment ?? 'start', listId, order, createdAt, updatedAt, isDeleted ? 1 : 0]
|
2025-10-25 17:02:42 +02:00
|
|
|
);
|
|
|
|
|
const field = await dbAdapter.queryOne('SELECT * FROM fields WHERE id = ?', [id]);
|
2025-10-24 20:27:18 +02:00
|
|
|
res.status(201).json({ ...(field as any), isDeleted: !!(field as any).isDeleted });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
res.status(500).json({ error: 'Failed to create field' });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Update field
|
|
|
|
|
router.put('/:id', async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
|
|
|
|
const updatedAt = Date.now();
|
2025-11-12 00:15:16 +01:00
|
|
|
const { name, fieldType, fieldOptions, alignment, order } = req.body;
|
2025-10-25 17:02:42 +02:00
|
|
|
const result = await dbAdapter.execute(
|
2025-11-12 00:15:16 +01:00
|
|
|
'UPDATE fields SET name = ?, fieldType = ?, fieldOptions = ?, alignment = ?, "order" = ?, updatedAt = ? WHERE id = ?',
|
|
|
|
|
[name, fieldType, fieldOptions, alignment ?? 'start', order, updatedAt, req.params.id]
|
2025-10-25 17:02:42 +02:00
|
|
|
);
|
2025-10-24 20:27:18 +02:00
|
|
|
|
|
|
|
|
if (result.changes === 0) {
|
|
|
|
|
return res.status(404).json({ error: 'Field not found' });
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-25 17:02:42 +02:00
|
|
|
const field = await dbAdapter.queryOne('SELECT * FROM fields WHERE id = ?', [req.params.id]);
|
2025-10-24 20:27:18 +02:00
|
|
|
res.json({ ...(field as any), isDeleted: !!(field as any).isDeleted });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
res.status(500).json({ error: 'Failed to update field' });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Delete field (soft delete)
|
|
|
|
|
router.delete('/:id', async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
|
|
|
|
const updatedAt = Date.now();
|
2025-10-25 17:02:42 +02:00
|
|
|
const result = await dbAdapter.execute(
|
|
|
|
|
'UPDATE fields SET isDeleted = 1, updatedAt = ? WHERE id = ?',
|
|
|
|
|
[updatedAt, req.params.id]
|
|
|
|
|
);
|
2025-10-24 20:27:18 +02:00
|
|
|
|
|
|
|
|
if (result.changes === 0) {
|
|
|
|
|
return res.status(404).json({ error: 'Field not found' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json({ message: 'Field deleted successfully' });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
res.status(500).json({ error: 'Failed to delete field' });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default router;
|