const db = require('../config/database'); exports.getAllLocations = async (req, res) => { try { const [rows] = await db.query('SELECT * FROM locations'); res.json({ success: true, data: rows }); } catch (error) { res.status(500).json({ success: false, error: error.message }); } }; exports.getLocationById = async (req, res) => { try { const [rows] = await db.query('SELECT * FROM locations WHERE location_id = ?', [req.params.id]); if (rows.length === 0) { return res.status(404).json({ success: false, error: 'Location not found' }); } res.json({ success: true, data: rows[0] }); } catch (error) { res.status(500).json({ success: false, error: error.message }); } }; exports.createLocation = async (req, res) => { try { const { village_name, district, state } = req.body; const [result] = await db.query( 'INSERT INTO locations (village_name, district, state) VALUES (?, ?, ?)', [village_name, district, state] ); res.status(201).json({ success: true, message: 'Location created successfully', data: { location_id: result.insertId } }); } catch (error) { res.status(500).json({ success: false, error: error.message }); } }; exports.updateLocation = async (req, res) => { try { const { village_name, district, state } = req.body; const [result] = await db.query( 'UPDATE locations SET village_name = ?, district = ?, state = ? WHERE location_id = ?', [village_name, district, state, req.params.id] ); if (result.affectedRows === 0) { return res.status(404).json({ success: false, error: 'Location not found' }); } res.json({ success: true, message: 'Location updated successfully' }); } catch (error) { res.status(500).json({ success: false, error: error.message }); } }; exports.deleteLocation = async (req, res) => { try { const [result] = await db.query('DELETE FROM locations WHERE location_id = ?', [req.params.id]); if (result.affectedRows === 0) { return res.status(404).json({ success: false, error: 'Location not found' }); } res.json({ success: true, message: 'Location deleted successfully' }); } catch (error) { res.status(500).json({ success: false, error: error.message }); } };