const firebaseConfig = { apiKey: "AIzaSyCc_bwjTnBIC42Kw6Lv9cCYbyMCguIoQck", authDomain: "first-assembly-of-god-ca-ebff6.firebaseapp.com", databaseURL: "https://first-assembly-of-god-ca-ebff6-default-rtdb.firebaseio.com", projectId: "first-assembly-of-god-ca-ebff6", storageBucket: "first-assembly-of-god-ca-ebff6.appspot.com", messagingSenderId: "589574395292", appId: "1:589574395292:web:e5fc9d2908d2a6b49a2459", measurementId: "G-GKTEN7ECDQ" }; FirebaseCRUD = { DATABASE_NAME: "universe", SONG_LIST: "songCollection", PLAYLISTS: "playlists", initialize: function () { if (!firebase.apps.length) { firebase.initializeApp(firebaseConfig); } firebase.auth().onAuthStateChanged(user => { if (!user) { window.open("Authentication.html", "_self"); } }); }, signOut: function () { return firebase.auth().signOut(); }, addSong: function (song) { var ref = firebase.database().ref(`${this.DATABASE_NAME}/${this.SONG_LIST}`).push(); var key = ref.key; song.setSongID(key); return ref.set(SongAPI.createJSONFromSongObject(song, key)); }, addPlaylist: function (playlistDate, selectedSongs) { return firebase.database().ref(`${this.DATABASE_NAME}/${this.PLAYLISTS}`).update({ [playlistDate]: selectedSongs }); }, addSongPictureAndGetReference: function (imageURL, songName) { if (imageURL !== DEFAULT_DISC_IMAGE && !imageURL.includes("firebase") && imageURL !== "") { var imageName = songName.replace(/\s+/g, "") + DatesAPI.getCurrentTimeInMilliseconds(); var imageRef = firebase.storage().ref().child(imageName); return imageRef.putString(imageURL, "data_url").then((snapshot) => { console.log("Song Image uploaded successfully"); return firebase.storage().ref(imageName).getDownloadURL().then(data => { return data; }); }); } return Promise.resolve(DEFAULT_DISC_IMAGE); }, deleteOldPictureFromStorage: function (oldImageURL) { if (oldImageURL.includes("firebase")) { var imageRef = firebase.storage().refFromURL(oldImageURL); return imageRef.delete(); } return Promise.resolve(""); }, load: function () { return firebase.database().ref(this.DATABASE_NAME).once("value"); }, updateSong: function (song) { var updates = {}; updates[`${this.DATABASE_NAME}/${this.SONG_LIST}/${song.getSongID()}/`] = SongAPI.createJSONFromSongObject(song); return firebase.database().ref().update(updates); }, delete: function (id) { return firebase.database().ref(`${this.DATABASE_NAME}/${this.SONG_LIST}`).child(id).remove(); } }