// hDiyanetProxy - Yer Bilgisi Modeli const pool = require('../utils/db'); const PlaceModel = { // --- Ülkeler --- async upsertCountry(id, name) { await pool.execute( 'INSERT INTO countries (id, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name)', [id, name] ); }, async getAllCountries() { const [rows] = await pool.execute('SELECT * FROM countries ORDER BY name'); return rows; }, // --- Eyaletler / İller --- async upsertState(id, name, countryId) { await pool.execute( 'INSERT INTO states (id, name, country_id) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name)', [id, name, countryId] ); }, async getStatesByCountry(countryId) { const [rows] = await pool.execute('SELECT * FROM states WHERE country_id = ? ORDER BY name', [countryId]); return rows; }, async getAllStates() { const [rows] = await pool.execute('SELECT * FROM states ORDER BY name'); return rows; }, // --- Şehirler / İlçeler --- async upsertCity(id, name, stateId, lat = null, lng = null) { await pool.execute( 'INSERT INTO cities (id, name, state_id, latitude, longitude) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name), latitude = VALUES(latitude), longitude = VALUES(longitude)', [id, name, stateId, lat, lng] ); }, async getCitiesByState(stateId) { const [rows] = await pool.execute('SELECT * FROM cities WHERE state_id = ? ORDER BY name', [stateId]); return rows; }, async getCityDetail(cityId) { const [rows] = await pool.execute( `SELECT c.*, s.name as state_name, co.name as country_name FROM cities c JOIN states s ON c.state_id = s.id JOIN countries co ON s.country_id = co.id WHERE c.id = ?`, [cityId] ); return rows[0] || null; }, // İstatistikler async getStats() { const [[countries]] = await pool.execute('SELECT COUNT(*) as count FROM countries'); const [[states]] = await pool.execute('SELECT COUNT(*) as count FROM states'); const [[cities]] = await pool.execute('SELECT COUNT(*) as count FROM cities'); const [[prayerTimes]] = await pool.execute('SELECT COUNT(*) as count FROM prayer_times'); const [[ramadanTimes]] = await pool.execute('SELECT COUNT(*) as count FROM ramadan_times'); const [[eidTimes]] = await pool.execute('SELECT COUNT(*) as count FROM eid_times'); return { countries: countries.count, states: states.count, cities: cities.count, prayerTimes: prayerTimes.count, ramadanTimes: ramadanTimes.count, eidTimes: eidTimes.count }; } }; module.exports = PlaceModel;