| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- const express = require('express');
- const pool = require('../db');
- const { authMiddleware } = require('../auth');
- const router = express.Router();
- router.use(authMiddleware);
- router.get('/', async (req, res) => {
- const rows = (await pool.query('SELECT * FROM properties WHERE user_id = $1 ORDER BY created_at DESC', [req.userId])).rows;
- res.json(rows);
- });
- router.post('/', async (req, res) => {
- const { name, address, city, zip_code, type, rooms, area, scan_directory, scan_cron_enabled } = req.body;
- if (!name || !address || !city || !zip_code || !type)
- return res.status(400).json({ error: 'Champs requis manquants' });
- const result = await pool.query(
- 'INSERT INTO properties (user_id, name, address, city, zip_code, type, rooms, area, scan_directory, scan_cron_enabled) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING id',
- [req.userId, name, address, city, zip_code, type, rooms || null, area || null, scan_directory || null, scan_cron_enabled ? 1 : 0]
- );
- res.status(201).json({ id: result.rows[0].id });
- });
- router.put('/:id', async (req, res) => {
- const { name, address, city, zip_code, type, rooms, area, scan_directory, scan_cron_enabled } = req.body;
- const prop = (await pool.query('SELECT id FROM properties WHERE id = $1 AND user_id = $2', [req.params.id, req.userId])).rows[0];
- if (!prop) return res.status(404).json({ error: 'Bien non trouvé' });
- await pool.query(
- 'UPDATE properties SET name=$1, address=$2, city=$3, zip_code=$4, type=$5, rooms=$6, area=$7, scan_directory=$8, scan_cron_enabled=$9 WHERE id=$10',
- [name, address, city, zip_code, type, rooms || null, area || null, scan_directory || null, scan_cron_enabled ? 1 : 0, req.params.id]
- );
- res.json({ success: true });
- });
- router.delete('/:id', async (req, res) => {
- const prop = (await pool.query('SELECT id FROM properties WHERE id = $1 AND user_id = $2', [req.params.id, req.userId])).rows[0];
- if (!prop) return res.status(404).json({ error: 'Bien non trouvé' });
- await pool.query('DELETE FROM properties WHERE id = $1', [req.params.id]);
- res.json({ success: true });
- });
- module.exports = router;
|