diff --git a/server.js b/server.js index 34f1046..5a4229e 100644 --- a/server.js +++ b/server.js @@ -20,8 +20,9 @@ app.use(express.json()); let db; async function initializeDB() { + const dbPath = path.join(__dirname, 'wifi_markers.db'); db = await open({ - filename: path.join(__dirname, 'wifi_markers.db'), + filename: dbPath, driver: sqlite3.Database }); @@ -46,13 +47,16 @@ async function initializeDB() { ) `); - console.log('Database initialized with WAL mode enabled.'); + console.log(`[DB] Database initialized at: ${dbPath}`); + console.log('[DB] WAL mode enabled for concurrent writes.'); } // Routes app.get('/api/markers', async (req, res) => { + console.log(`[API] GET /api/markers - Fetching markers from SQLite`); try { const markers = await db.all('SELECT * FROM markers'); + console.log(`[API] Found ${markers.length} markers`); // Convert isPublic from 0/1 to boolean const formattedMarkers = markers.map(m => ({ ...m, @@ -67,6 +71,7 @@ app.get('/api/markers', async (req, res) => { app.post('/api/markers', async (req, res) => { const { id, name, ssid, password, lat, lng, securityType, iconType, description, addedBy, createdAt, isPublic } = req.body; + console.log(`[API] POST /api/markers - Saving marker: ${name} (${ssid}) to SQLite`); try { await db.run(` @@ -74,6 +79,7 @@ app.post('/api/markers', async (req, res) => { VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, [id, name, ssid, password, lat, lng, securityType, iconType, description || '', addedBy, createdAt, isPublic ? 1 : 0]); + console.log(`[API] Successfully saved marker: ${id}`); res.json({ success: true }); } catch (error) { console.error(error); @@ -83,8 +89,10 @@ app.post('/api/markers', async (req, res) => { app.delete('/api/markers/:id', async (req, res) => { const { id } = req.params; + console.log(`[API] DELETE /api/markers/${id} - Deleting marker from SQLite`); try { await db.run('DELETE FROM markers WHERE id = ?', [id]); + console.log(`[API] Successfully deleted marker: ${id}`); res.json({ success: true }); } catch (error) { console.error(error); diff --git a/services/api.ts b/services/api.ts index 33c8b1e..2114534 100644 --- a/services/api.ts +++ b/services/api.ts @@ -5,10 +5,12 @@ const API_Base_URL = '/api/markers'; export const apiService = { getHotspots: async (): Promise => { + console.log(`[API Call] GET ${API_Base_URL}`); try { const response = await fetch(API_Base_URL); if (!response.ok) throw new Error('Failed to fetch markers'); const data = await response.json(); + console.log(`[API Response] Received ${data.length} hotspots`, data); return data; } catch (e) { console.error("Failed to load hotspots from API", e); @@ -17,6 +19,7 @@ export const apiService = { }, saveHotspot: async (hotspot: WiFiHotspot): Promise => { + console.log(`[API Call] POST ${API_Base_URL}`, hotspot); try { const response = await fetch(API_Base_URL, { method: 'POST', @@ -26,6 +29,8 @@ export const apiService = { body: JSON.stringify(hotspot), }); if (!response.ok) throw new Error('Failed to save marker'); + const data = await response.json(); + console.log(`[API Response] Save success:`, data); // Trigger a storage event to refresh UI if needed (keeping compatibility) window.dispatchEvent(new Event('storage_updated')); } catch (e) { @@ -34,11 +39,15 @@ export const apiService = { }, deleteHotspot: async (id: string): Promise => { + const url = `${API_Base_URL}/${id}`; + console.log(`[API Call] DELETE ${url}`); try { - const response = await fetch(`${API_Base_URL}/${id}`, { + const response = await fetch(url, { method: 'DELETE', }); if (!response.ok) throw new Error('Failed to delete marker'); + const data = await response.json(); + console.log(`[API Response] Delete success:`, data); window.dispatchEvent(new Event('storage_updated')); } catch (e) { console.error("Failed to delete hotspot", e);