Skip to content

Webservice Erweiterungen

Sebastian Faust edited this page Jul 9, 2018 · 1 revision

Für die Umsetzung mit Firebase haben wir kleine Erweiterungen im Code vorgenommen, die wir für die korrekte Umsetzung benötigen. Für diese Erweiterungen bietet Firebase keine standardisierten Funktionen an. All unsere Erweiterungsfunktionen sind in "Dienstgeber/own_modules/firestoreExtensions.js" zu finden.

getIdInCollection(collectionName)

/**
 * Returns a unigue ID in a specific collection
 * @param collectionName name of the collection that a id is to be generated for
 * @returns int id unique ID
 */
function getIdInCollection(collectionName) {
    let ref = db.collection(collectionName).doc();
    let id = ref.id;

    return id;
}

getCollectionAsJSON(collectionName)

/**
 * Returns a Promise that is to be resolved as a JSON and represents a specific collection (GET)
 * @param collectionName naem of the collecetion
 * @returns {Promise<any>} Promise that resolves as JSON
 */
function getCollectionAsJSON(collectionName) {
    return new Promise(function (resolve) {
        let json = {};

        let collection = db.collection(collectionName);
        collection.get()
            .then(snapshot => {
                snapshot.forEach(doc => {

                    json[doc.id] = doc.data();
                });
            }).then(function () {
            resolve(json);
        });
    });
}

getDokumentAsJSON(collectionName,docName)

/**
 * Returns a Promise that is to be resolved as a JSON and represents a specific document in a collection (GET)
 * @param collectionName name of the collection
 * @param docName name of the document
 * @returns {Promise<any>} Promise that resolves as JSON
 */
function getDokumentAsJSON(collectionName,docName) {
    return new Promise(function (resolve) {
        let json = {};

        let document = db.collection(collectionName).doc(docName);
        document.get()
            .then(doc => {
                json = doc.data();

            }).then(function () {
            resolve(json);
        });
    });
}

checkIfDocInCollection(collectionName, docName)

/**
 * Returns a Promise that is to be resolved as a boolean that shows us if a ID exists in a collection
 * @param collectionName name of the collection
 * @param docName name of the document
 * @returns {Promise<any>} Promise that resolves as bool
 */
function checkIfDocInCollection(collectionName, docName) {
    return new Promise(function (resolve) {
        // Test for the existence of certain keys within a DataSnapshot;
        db.collection(collectionName).once(docName)
            .then(function(snapshot) {
                let idExists = snapshot.exists();
                resolve(idExists);
            });
    });
}