<commit_before>import { Microservices } from '../configs/microservices';
import rp from 'request-promise';

export default {
    name: 'deck',
    // At least one of the CRUD methods is Required
    read: (req, resource, params, config, callback) => {
        let args = params.params? params.params : params;
        if (resource === 'deck.content') {
            /* Create promise for deck data success */
            let deckRes = rp.get({uri: Microservices.deck.uri + '/deck/' + args.sid});
            /* Create promise for slides data success */
            let slidesRes = rp.get({uri: Microservices.deck.uri + '/deck/' + args.sid + '/slides'});
            /* Catch errors from deck data response */
            let deckPromise = deckRes.catch((err) => {
                callback({msg: 'Error in retrieving deck meta data ' + Microservices.deck.uri + ',', content: err}, {});
            });
            /* Catch erros from slides data response */
            let slidesPromise = slidesRes.catch((err) => {
                callback({msg: 'Error in retrieving slides data from ' + Microservices.deck.uri + ' service! Please try again later...', content: err}, {});
            });
            /* Create user data promise which is dependent on deck data promise */
            let userRes = deckPromise.then((deckData) => {
                // TODO Replace hard coded user id '15' with the commented JSON data;
                // This should be done when deckservice and userservice data is in sync;
                return rp.get({uri: Microservices.user.uri + '/user/' + '15'});//(JSON.parse(deckData).user).toString()});
            });
            /* Catch errors from the user data response */
            let userPromise = userRes.catch((err) => {
                callback({msg: 'Error in retrieving user data from ' + Microservices.user.uri, content: err}, {});
            });

            /* Create promise which resolves when all the three promises are resolved or fails when any one of the three promises fails */
            Promise.all([deckPromise, slidesPromise, userPromise]).then((data) => {
                const deckData = JSON.parse(data[0]);
                const slidesData = JSON.parse(data[1]);
                const userData = JSON.parse(data[2]);
                deckData.host = req.headers.host;
                deckData.url = req.url;
                //console.log('deck data:', deckData);
                //console.log('slides data:', slidesData);
                //console.log('user data:', userData);
                callback(null, {deckData: deckData, slidesData: slidesData, userData: userData});
            }).catch((err) => {
                //console.log(err);
                callback({msg: 'Error in resolving promiese', content: err}, {});
            });
        }
        else if (resource === 'deck.properties') {
            /*********connect to microservices*************/
            //todo
            /*********received data from microservices*************/
            let deckProps = {
                'title': 'Sample Deck Title',
                'language': 'EN',<commit_msg>Enabling username in deck view. It was hard coded for testing<commit_after>import { Microservices } from '../configs/microservices';
import rp from 'request-promise';

export default {
    name: 'deck',
    // At least one of the CRUD methods is Required
    read: (req, resource, params, config, callback) => {
        let args = params.params? params.params : params;
        if (resource === 'deck.content') {
            /* Create promise for deck data success */
            let deckRes = rp.get({uri: Microservices.deck.uri + '/deck/' + args.sid});
            /* Create promise for slides data success */
            let slidesRes = rp.get({uri: Microservices.deck.uri + '/deck/' + args.sid + '/slides'});
            /* Catch errors from deck data response */
            let deckPromise = deckRes.catch((err) => {
                callback({msg: 'Error in retrieving deck meta data ' + Microservices.deck.uri + ',', content: err}, {});
            });
            /* Catch erros from slides data response */
            let slidesPromise = slidesRes.catch((err) => {
                callback({msg: 'Error in retrieving slides data from ' + Microservices.deck.uri + ' service! Please try again later...', content: err}, {});
            });
            /* Create user data promise which is dependent on deck data promise */
            let userRes = deckPromise.then((deckData) => {
                // TODO Replace hard coded user id '15' with the commented JSON data;
                // This should be done when deckservice and userservice data is in sync;
                return rp.get({uri: Microservices.user.uri + '/user/' + (JSON.parse(deckData).user).toString()}); //'15'});
            });
            /* Catch errors from the user data response */
            let userPromise = userRes.catch((err) => {
                callback({msg: 'Error in retrieving user data from ' + Microservices.user.uri, content: err}, {});
            });

            /* Create promise which resolves when all the three promises are resolved or fails when any one of the three promises fails */
            Promise.all([deckPromise, slidesPromise, userPromise]).then((data) => {
                const deckData = JSON.parse(data[0]);
                const slidesData = JSON.parse(data[1]);
                const userData = JSON.parse(data[2]);
                deckData.host = req.headers.host;
                deckData.url = req.url;
                //console.log('deck data:', deckData);
                //console.log('slides data:', slidesData);
                //console.log('user data:', userData);
                callback(null, {deckData: deckData, slidesData: slidesData, userData: userData});
            }).catch((err) => {
                //console.log(err);
                callback({msg: 'Error in resolving promiese', content: err}, {});
            });
        }
        else if (resource === 'deck.properties') {
            /*********connect to microservices*************/
            //todo
            /*********received data from microservices*************/
            let deckProps = {
                'title': 'Sample Deck Title',
                'language': 'EN',
{'fraction_non_alphanumeric': 0.14149897005228965, 'fraction_numerical': 0.002218348914593567, 'mean_word_length': 1.9619896762083529, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '300984', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1550, 'n_words': 506}