49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
import db from "../config/database.js";
|
|
|
|
class LocationService {
|
|
static async getCountries() {
|
|
return await db.allAsync('SELECT DISTINCT country FROM locations ORDER BY country');
|
|
}
|
|
|
|
static async getCitiesByCountry(country) {
|
|
return await db.allAsync(
|
|
'SELECT DISTINCT city FROM locations WHERE country = ? ORDER BY city',
|
|
[country]
|
|
);
|
|
}
|
|
|
|
static async getDistrictsByCountryAndCity(country, city) {
|
|
return await db.allAsync(
|
|
'SELECT district FROM locations WHERE country = ? AND city = ? ORDER BY district',
|
|
[country, city]
|
|
);
|
|
}
|
|
|
|
static async getLocation(country, city, district) {
|
|
try {
|
|
const location = await db.getAsync(
|
|
'SELECT * FROM locations WHERE country = ? AND city = ? AND district = ?',
|
|
[country, city, district]
|
|
);
|
|
return location;
|
|
} catch (error) {
|
|
console.error('Error fetching location:', error);
|
|
throw new Error('Failed to fetch location');
|
|
}
|
|
}
|
|
|
|
static async getLocationById(locationId) {
|
|
try {
|
|
const location = await db.getAsync(
|
|
'SELECT * FROM locations WHERE id = ?',
|
|
[locationId]
|
|
);
|
|
return location;
|
|
} catch (error) {
|
|
console.error('Error fetching location by ID:', error);
|
|
throw new Error('Failed to fetch location');
|
|
}
|
|
}
|
|
}
|
|
|
|
export default LocationService; |