Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Common database helper functions.
- */
- let dbpromise;
- class DBHelper {
- /**
- * Database URL.
- * Change this to restaurants.json file location on your server.
- */
- static get DATABASE_URL() {
- const port = 1337; // Change this to your server port
- return `http://localhost:${port}/restaurants`;
- }
- /**
- * Create DataBase
- */
- static openDataBase(){
- return idb.open('restaurantDb',1,function(upgradeDb){
- upgradeDb.createObjectStore('restaurantDb',{
- keyPath:'id'
- });
- });
- }
- /**
- * Saving on DataBase
- */
- static saveDataBase(data){
- return DBHelper.openDataBase().then(function (db){
- if(!db) return;
- let tx=db.transaction('restaurantDb','readwrite');
- let store=tx.objectStore('restaurantDb');
- data.forEach(function(restaurant){
- store.put(restaurant);
- });
- return tx.complete;
- });
- }
- /**
- * Getting data
- */
- static getCachedDb(){
- dbpromise=DBHelper.openDataBase();
- return dbpromise.then(function (db){
- if(!db) return;
- let tx=db.transaction('restaurantDb');
- let store=tx.objectStore('restaurantDb');
- return store.getAll();
- });
- }
- static fromAPI(){
- return fetch(DBHelper.DATABASE_URL).then(function (response) {
- return response.json();
- }).then(restaurants=>{
- DBHelper.saveDataBase(restaurants);
- return restaurants;
- });
- }
- /**
- * Fetch all restaurants.
- */
- static fetchRestaurants(callback) {
- fetch(DBHelper.DATABASE_URL)
- .then(function(response){
- return response.json();
- })
- .then(function(response){
- callback(null,response);
- console.log('Sucess:',response);
- })
- }
- /**
- * Fetch a restaurant by its ID.
- */
- static fetchRestaurantById(id, callback) {
- // fetch all restaurants with proper error handling.
- DBHelper.fetchRestaurants((error, restaurants) => {
- if (error) {
- callback(error, null);
- } else {
- const restaurant = restaurants.find(r => r.id == id);
- if (restaurant) { // Got the restaurant
- callback(null, restaurant);
- } else { // Restaurant does not exist in the database
- callback('Restaurant does not exist', null);
- }
- }
- });
- }
- /**
- * Fetch restaurants by a cuisine type with proper error handling.
- */
- static fetchRestaurantByCuisine(cuisine, callback) {
- // Fetch all restaurants with proper error handling
- DBHelper.fetchRestaurants((error, restaurants) => {
- if (error) {
- callback(error, null);
- } else {
- // Filter restaurants to have only given cuisine type
- const results = restaurants.filter(r => r.cuisine_type == cuisine);
- callback(null, results);
- }
- });
- }
- /**
- * Fetch restaurants by a neighborhood with proper error handling.
- */
- static fetchRestaurantByCuisine(cuisine, callback) {
- // Fetch all restaurants with proper error handling
- DBHelper.fetchRestaurants((error, restaurants) => {
- if (error) {
- callback(error, null);
- } else {
- // Filter restaurants to have only given cuisine type
- const results = restaurants.filter(r => r.cuisine_type == cuisine);
- callback(null, results);
- }
- });
- }
- /**
- * Fetch restaurants by a cuisine and a neighborhood with proper error handling.
- */
- static fetchRestaurantByCuisineAndNeighborhood(cuisine, neighborhood, callback) {
- // Fetch all restaurants
- DBHelper.fetchRestaurants((error, restaurants) => {
- if (error) {
- callback(error, null);
- } else {
- let results = restaurants
- if (cuisine != 'all') { // filter by cuisine
- results = results.filter(r => r.cuisine_type == cuisine);
- }
- if (neighborhood != 'all') { // filter by neighborhood
- results = results.filter(r => r.neighborhood == neighborhood);
- }
- callback(null, results);
- }
- });
- }
- /**
- * Fetch all neighborhoods with proper error handling.
- */
- static fetchNeighborhoods(callback) {
- // Fetch all restaurants
- DBHelper.fetchRestaurants((error, restaurants) => {
- if (error) {
- callback(error, null);
- } else {
- // Get all neighborhoods from all restaurants
- const neighborhoods = restaurants.map((v, i) => restaurants[i].neighborhood)
- // Remove duplicates from neighborhoods
- const uniqueNeighborhoods = neighborhoods.filter((v, i) => neighborhoods.indexOf(v) == i)
- callback(null, uniqueNeighborhoods);
- }
- });
- }
- /**
- * Fetch all cuisines with proper error handling.
- */
- static fetchCuisines(callback) {
- // Fetch all restaurants
- DBHelper.fetchRestaurants((error, restaurants) => {
- if (error) {
- callback(error, null);
- } else {
- // Get all cuisines from all restaurants
- const cuisines = restaurants.map((v, i) => restaurants[i].cuisine_type)
- // Remove duplicates from cuisines
- const uniqueCuisines = cuisines.filter((v, i) => cuisines.indexOf(v) == i)
- callback(null, uniqueCuisines);
- }
- });
- }
- /**
- * Restaurant page URL.
- */
- static urlForRestaurant(restaurant) {
- return (`./restaurant.html?id=${restaurant.id}`);
- }
- /**
- * Restaurant image URL.
- */
- static imageUrlForRestaurant(restaurant) {
- return (`./img/${restaurant.id}`);
- }
- /**
- * Map marker for a restaurant.
- */
- static mapMarkerForRestaurant(restaurant, map) {
- // https://leafletjs.com/reference-1.3.0.html#marker
- const marker = new L.marker([restaurant.latlng.lat, restaurant.latlng.lng], {
- title: restaurant.name,
- alt: restaurant.name,
- url: DBHelper.urlForRestaurant(restaurant)
- })
- marker.addTo(newMap);
- return marker;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment