import express from "express"; import pool from "../db/pool.js"; const router = express.Router(); // Add a new location router.post("/", async (req, res) => { const { user_id, is_saved_address, location_type, country, state, district, city_village, pincode, lat, lng, source_type, source_confidence, } = req.body; try { const insertQuery = "INSERT INTO locations (user_id, is_saved_address, location_type, country, state, district, city_village, pincode, lat, lng, source_type, source_confidence) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING *"; const values = [ user_id, is_saved_address, location_type, country, state, district, city_village, pincode, lat, lng, source_type, source_confidence, ]; const result = await pool.query(insertQuery, values); res.status(201).json({ message: "Location added successfully", location: result.rows[0], }); } catch (error) { console.error("Error adding location:", error); res.status(500).json({ error: "Internal server error adding location", }); } }); // Update a location by ID router.put("/:id", async (req, res) => { const locationId = req.params.id; const { user_id, is_saved_address, location_type, country, state, district, city_village, pincode, lat, lng, source_type, source_confidence, } = req.body; try { const updateQuery = "UPDATE locations SET user_id = $1, is_saved_address = $2, location_type = $3, country = $4, state = $5, district = $6, city_village = $7, pincode = $8, lat = $9, lng = $10, source_type = $11, source_confidence = $12 WHERE id = $13 RETURNING *"; const values = [ user_id, is_saved_address, location_type, country, state, district, city_village, pincode, lat, lng, source_type, source_confidence, locationId, ]; const result = await pool.query(updateQuery, values); if (result.rows.length === 0) { return res.status(404).json({ error: "Location not found" }); } res.status(200).json({ message: "Location updated successfully", location: result.rows[0], }); } catch (error) { console.error("Error updating location:", error); res.status(500).json({ error: "Internal server error updating location", }); } }); // Get all locations for a user router.get("/user/:user_id", async (req, res) => { const userId = req.params.user_id; try { const result = await pool.query( "SELECT * FROM locations WHERE user_id = $1 ORDER BY created_at DESC", [userId] ); res.status(200).json(result.rows); } catch (error) { console.error("Error fetching user's locations:", error); res.status(500).json({ error: "Internal server error fetching user's locations", }); } }); // TODO: Remove this route later // Get all users router.get("/users", async (req, res) => { try { const result = await pool.query( "SELECT * FROM users ORDER BY created_at DESC" ); res.status(200).json(result.rows); } catch (error) { console.error("Error fetching all users:", error); res.status(500).json({ error: "Internal server error fetching users", }); } }); // Get all locations router.get("/", async (req, res) => { try { const result = await pool.query( "SELECT * FROM locations ORDER BY created_at DESC" ); res.status(200).json(result.rows); } catch (error) { console.error("Error fetching all locations:", error); res.status(500).json({ error: "Internal server error fetching locations", }); } }); // Get location by ID router.get("/:id", async (req, res) => { const locationId = req.params.id; try { const selectQuery = "SELECT * FROM locations WHERE id = $1"; const result = await pool.query(selectQuery, [locationId]); if (result.rows.length === 0) { return res.status(404).json({ error: "Location not found" }); } res.status(200).json(result.rows[0]); } catch (error) { console.error("Error fetching location:", error); res.status(500).json({ error: "Internal server error fetching location", }); } }); // Delete a location by ID router.delete("/:id", async (req, res) => { const locationId = req.params.id; try { const deleteQuery = "DELETE FROM locations WHERE id = $1 RETURNING *"; const result = await pool.query(deleteQuery, [locationId]); if (result.rows.length === 0) { return res.status(404).json({ error: "Location not found" }); } res.status(200).json({ message: "Location deleted successfully", location: result.rows[0], }); } catch (error) { console.error("Error deleting location:", error); res.status(500).json({ error: "Internal server error deleting location", }); } }); export default router;