Files
telegram-shop/src/services/locationService.js
2024-12-15 02:04:43 +00:00

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;