import { getDatabase, ref, child, get, push, update } from "firebase/database";
const database = getDatabase();
const routesRef = ref(database, "routes");
/**
* The function `getSavedRoutes` retrieves saved routes data for a specific user from a database using
* asynchronous JavaScript.
* @param userID - The `userID` parameter in the `getSavedRoutes` function is used to identify the user
* for whom we want to retrieve saved routes. It is typically a unique identifier associated with a
* specific user in the system.
* @returns The function `getSavedRoutes(userID)` returns the routes data for the specified `userID` if
* it exists in the database. If no routes are found for the user, it returns `null`. If an error
* occurs during the retrieval process, the function will log the error and then propagate the error to
* the caller.
*/
export async function getSavedRoutes(userID) {
try {
const snapshot = await get(child(routesRef, `${userID}`));
// const snapshot = await get(routesRef, userID);
if (snapshot.exists()) {
const routesData = snapshot.val();
// console.log("RoutesData: ", routesData);
// console.log("RoutesData: ", Object.keys(routesData));
const routesArray = Object.values(routesData);
return routesArray;
} else {
console.log("No routes found for user.");
return null;
}
} catch (error) {
console.error("Error retrieving routes from database:", error);
throw error; // Propagate the error to the caller
}
}
/**
* The function `getSavedRoutesWithKeys` retrieves saved routes data for a specific user from a
* database using their userID. This is meant for deletion/updating purposes. Please use getSavedRoutes
* if only the values are required.
* @param userID - The `userID` parameter in the `getSavedRoutesWithKeys` function is used to specify
* the user for whom we want to retrieve saved routes.
* @returns The `getSavedRoutesWithKeys` function is returning the `routesData` object if the snapshot
* exists, which contains the saved routes data for the specified `userID`. If no routes are found for
* the user, it returns `null`.
*/
export async function getSavedRoutesWithKeys(userID) {
try {
const snapshot = await get(child(routesRef, `${userID}`));
// const snapshot = await get(routesRef, userID);
if (snapshot.exists()) {
const routesData = snapshot.val();
const routesKeysArray = Object.keys(routesData);
return routesKeysArray;
} else {
console.log("No routes found for user.");
return null;
}
} catch (error) {
console.error("Error retrieving routes from database:", error);
throw error; // Propagate the error to the caller
}
}
/**
* The function `uploadSavedRoutes` uploads saved routes data for a specific user to a database using
* Firebase Realtime Database.
* @param userID - The `userID` parameter is the unique identifier of the user for whom the saved
* routes are being uploaded.
* @param postData - The `postData` parameter likely contains the data of a saved route that the user
* wants to upload. This data could include information such as the route name, starting point,
* destination, waypoints, distance, duration, and any other relevant details about the route.
* @returns The `uploadSavedRoutes` function is returning the result of the `update` function called
* with `routesRef` and `updates` as arguments.
*/
export function uploadSavedRoutes(userID, postData) {
const newPostKey = push(child(routesRef, `${userID}`)).key;
const updates = {};
updates[`/${userID}/` + newPostKey] = postData;
return update(routesRef, updates);
}
/**
* The function `deleteSavedRoutes` deletes a specific postKey under a user's userID in a database.
* @param userID - The `userID` parameter is the unique identifier of the user whose saved route is
* being deleted.
* @param postKey - The `postKey` parameter is the key of the specific route that you want to delete
* from the saved routes of a user.
*/
export function deleteSavedRoutes(userID, postKey) {
const updates = {};
updates[`/${userID}/` + postKey] = null;
update(routesRef, updates);
}