From ac9178bd4180d6eb2c4128da0e7afba2bd82eba5 Mon Sep 17 00:00:00 2001 From: Sangita Sunuwar Date: Tue, 18 Sep 2018 10:43:48 +0100 Subject: [PATCH] implement getData and data structure >relates #28 --- Documents/bower_initial_css_install.md | 53 +++++++++++++ src/controllers/home.js | 2 +- src/controllers/index.js | 4 + src/controllers/resources.js | 12 +-- src/controllers/results.js | 103 +++++++++++++++++++++++++ src/model/getData.js | 82 ++++++++++---------- src/views/home2.hbs | 17 ++++ src/views/results.hbs | 26 +++++++ 8 files changed, 252 insertions(+), 47 deletions(-) create mode 100644 Documents/bower_initial_css_install.md create mode 100644 src/controllers/results.js create mode 100644 src/views/home2.hbs create mode 100644 src/views/results.hbs diff --git a/Documents/bower_initial_css_install.md b/Documents/bower_initial_css_install.md new file mode 100644 index 0000000..4654d62 --- /dev/null +++ b/Documents/bower_initial_css_install.md @@ -0,0 +1,53 @@ + + +``` +Nanis-MacBook-Air:tech-access ssun2$ git checkout -b css-works +Switched to a new branch 'css-works' +Nanis-MacBook-Air:tech-access ssun2$ npm i +audited 20040 packages in 7.338s +found 0 vulnerabilities + +Nanis-MacBook-Air:tech-access ssun2$ npm install -g bower +npm WARN deprecated bower@1.8.4: We don't recommend using Bower for new projects. Please consider Yarn and Webpack or Parcel. You can read how to migrate legacy project here: https://bower.io/blog/2017/how-to-migrate-away-from-bower/ +/usr/local/bin/bower -> /usr/local/lib/node_modules/bower/bin/bower ++ bower@1.8.4 +added 1 package from 1 contributor in 3.795s +Nanis-MacBook-Air:tech-access ssun2$ ls +Documents config.env database package-lock.json public +README.md coverage node_modules package.json src +Nanis-MacBook-Air:tech-access ssun2$ cd public/stylesheets +Nanis-MacBook-Air:stylesheets ssun2$ ls +bower.json main.css main.css.map main.scss +Nanis-MacBook-Air:stylesheets ssun2$ bower install +bower o-grid#^4.4.3 not-cached https://github.com/Financial-Times/o-grid.git#^4.4.3 +bower o-grid#^4.4.3 resolve https://github.com/Financial-Times/o-grid.git#^4.4.3 +bower o-grid#^4.4.3 download https://github.com/Financial-Times/o-grid/archive/v4.4.3.tar.gz +bower o-grid#^4.4.3 extract archive.tar.gz +bower o-grid#^4.4.3 resolved https://github.com/Financial-Times/o-grid.git#4.4.3 +bower sass-mq#^5.0.0 not-cached https://github.com/sass-mq/sass-mq.git#^5.0.0 +bower sass-mq#^5.0.0 resolve https://github.com/sass-mq/sass-mq.git#^5.0.0 +bower sass-mq#^5.0.0 download https://github.com/sass-mq/sass-mq/archive/v5.0.0.tar.gz +bower sass-mq#^5.0.0 extract archive.tar.gz +bower sass-mq#^5.0.0 resolved https://github.com/sass-mq/sass-mq.git#5.0.0 +bower o-grid#^4.4.3 install o-grid#4.4.3 +bower sass-mq#^5.0.0 install sass-mq#5.0.0 + +o-grid#4.4.3 bower_components/o-grid +└── sass-mq#5.0.0 + +sass-mq#5.0.0 bower_components/sass-mq +Nanis-MacBook-Air:stylesheets ssun2$ sass --watch main.scss main.css +File main.css is a CSS file. + Did you mean: sass --watch main.scss:main.css + Use --trace for backtrace. +Nanis-MacBook-Air:stylesheets ssun2$ sass --watch main.scss:main.css +>>> Sass is watching for changes. Press Ctrl-C to stop. + error bower_components/o-grid/main.scss (Line 7: File to import not found or unreadable: sass-mq/mq.) + + + + +>>> Change detected to: bower_components/o-grid/main.scss + write main.css + write main.css.map + ``` diff --git a/src/controllers/home.js b/src/controllers/home.js index bdcd66a..e35ffdc 100644 --- a/src/controllers/home.js +++ b/src/controllers/home.js @@ -1,3 +1,3 @@ exports.get = (req, res) => { - res.render('home', {activePage:{home: true}}) + res.render('home2', {activePage:{home2: true}}) } \ No newline at end of file diff --git a/src/controllers/index.js b/src/controllers/index.js index 39bf471..d599931 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -7,12 +7,16 @@ const background = require('./background') const resources = require('./resources') const error = require('./error') +const results = require('./results'); + router.use(bodyParser.urlencoded({extended: true})); router.get('/', home.get); router.get('/welcome', welcome.get); router.get('/background', background.get); router.get('/resources', resources.get); +router.get('/results', results.post); + // trigger 500 error for testing if(process.env.NODE_ENV === 'test') { diff --git a/src/controllers/resources.js b/src/controllers/resources.js index 07942be..04c8190 100644 --- a/src/controllers/resources.js +++ b/src/controllers/resources.js @@ -1,5 +1,7 @@ -exports.get = (req, res) => { - res.render('resources', { - layout: 'scrollable' - }); -} \ No newline at end of file + + exports.get = (req, res) => { + res.render('resources', { + layout: 'scrollable' + }); + } + diff --git a/src/controllers/results.js b/src/controllers/results.js new file mode 100644 index 0000000..f244971 --- /dev/null +++ b/src/controllers/results.js @@ -0,0 +1,103 @@ +const queries = require('../model/index'); +//implement form=post in frontend +exports.post = (req, response) => { + //get params, demo_tags is array + //const {demo_tags} = req.body; + const demo_tags = ['age', 'disability', 'gender']; + queries.getData(demo_tags) + .then((res) => { + //Pure function?? + const userDemoTag = res.rows; + const resultArray = arrangeArray(userDemoTag); + const outArray = filterByType(resultArray); + console.log('OUTARRAY', outArray); + //const resultArray = (res.rows); + + // console.log('AFTER', resultArray); + + response.render('results', { outArray }); + }) + .catch(err => console.log(err)); + //to arrange as ['meetup'= [{},{},...], 'classroom_course'= [{},{},...], 'article'=[{},{},...],.....] + function filterByType(inArray) { + const types = ['meetup', 'online course', 'article', 'classroom course']; + const outArr = [] + types.forEach(mytype => { + //outArr[meetup]= filtered result// setting keys to values from filter function + outArr[mytype] = inArray.filter(function (value) { + return value.resource_type == mytype; + }) + }) + return outArr; + } + + //to arrange repeated resource as {resource_id='1', resource_name='codebar', direct=['age','ethnicity'], indirect=['disability']},.... + function arrangeArray(inArray) { + var resultArray = []; //empty array to start with + for (let i = 0; i < inArray.length; i++) { + if (i == 0) { + //populate new resouce in our result array + resultArrIndx = 0; + // resultArray.push({inArray[i].resource_id, inArray[i].resource_name, inArray[i].url}); + resultArray.push({ + resource_id: inArray[i].resource_id, resource_name: inArray[i].resource_name, + url: inArray[i].url, resource_type: inArray[i].resource_type + }); + + resultArray[resultArrIndx].direct = []; + resultArray[resultArrIndx].indirect = []; + //if incoming demo_tag_relevance is direct push it into resultArray[resultArrIndx].direct array else + //push into resultArray[resultArrIndx].indirect array + if (inArray[i].relevance === 'direct') { + resultArray[resultArrIndx].direct.push(inArray[i].tag_name); + } else { + resultArray[resultArrIndx].indirect.push(inArray[i].tag_name); + } + resultArrIndx++; + // resultArray[resultArrIndx].demo_tag_relevance = []; + // const b = {}; + // b.tag_name = inArray[i].tag_name; + // b.relevance = inArray[i].relevance; + // resultArray[resultArrIndx].demo_tag_relevance.push(b); + + // console.log('RESULTARRAY AFTER', resultArray); + } + else if (resultArray[resultArrIndx - 1].resource_id == inArray[i].resource_id) { + //console.log('PROBLEM', inArray[i].relevance) + // resultArray[resultArrIndx - 1].demo_tag_relevance.push({ 'tag_name': inArray[i].tag_name, 'relevance': inArray[i].relevance }); + if (inArray[i].demo_tag_relevance === 'direct') { + + resultArray[resultArrIndx - 1].direct.push(inArray[i].tag_name); + } else { + resultArray[resultArrIndx - 1].indirect.push(inArray[i].tag_name); + } + } + else { + //populate new resouce in our result array + //create new key demo_tag_relevance and push {tagname:'tagname value', relevance:'rel value'} + resultArray.push({ + resource_id: inArray[i].resource_id, resource_name: inArray[i].resource_name, + url: inArray[i].url, resource_type: inArray[i].resource_type + }); + resultArray[resultArrIndx].direct = []; + resultArray[resultArrIndx].indirect = []; + //if incoming demo_tag_relevance is direct push it into resultArray[resultArrIndx].direct array else + //push into resultArray[resultArrIndx].indirect array + if (inArray[i].relevance === 'direct') { + resultArray[resultArrIndx].direct.push(inArray[i].tag_name); + } else { + resultArray[resultArrIndx].indirect.push(inArray[i].tag_name); + } + resultArrIndx++; + } + + } //eof for + // console.log('resultArray =', resultArray); + // resultArray[2].demo_tag_relevance.forEach(function (officer) { + // console.log(officer.tag_name, ":", officer.relevance); + //}) + + return resultArray; + } + +} \ No newline at end of file diff --git a/src/model/getData.js b/src/model/getData.js index f4f3bdd..a351f4f 100644 --- a/src/model/getData.js +++ b/src/model/getData.js @@ -1,7 +1,7 @@ const db = require("../database/db_connection"); var inputArray = ["age"]; -const getResource = inputArray => { - var dataQuery = `select resource.resource_id ,resource.resource_name,resource.url, demographic_tag. demographic_tag_id, demographic_tag.tag_name, demographic_resource_link.relevance from resource INNER JOIN demographic_resource_link ON resource.resource_id = demographic_resource_link.link_resource_id INNER JOIN demographic_tag ON demographic_resource_link.link_demographic_tag_id = demographic_tag. demographic_tag_id WHERE demographic_tag.tag_name = '${ +const getData = inputArray => { + var dataQuery = `select resource.resource_id ,resource.resource_name,resource.url, resource.resource_type,demographic_tag. demographic_tag_id, demographic_tag.tag_name, demographic_resource_link.relevance from resource INNER JOIN demographic_resource_link ON resource.resource_id = demographic_resource_link.link_resource_id INNER JOIN demographic_tag ON demographic_resource_link.link_demographic_tag_id = demographic_tag. demographic_tag_id WHERE demographic_tag.tag_name = '${ inputArray[0] }'`; // 'refugee' OR demographic_tag.tag_name = 'ethnicity' OR demographic_tag.tag_name = 'age' " ; @@ -18,55 +18,55 @@ const getResource = inputArray => { .catch(err => reject(err)); }); }; -module.exports = getResource; +module.exports = getData; -// getResource(['age', 'disability', 'gender']) +// getData(['age', 'disability', 'gender']) // .then(res => console.log(res.rows)) // .catch(err => console.log(err)) //function invoking/ function call -const resultArr = getResource(["age", "disability", "gender"]) - .then((res) => { - var inArray = res.rows; +// const resultArr = getData(["age", "disability", "gender"]) +// .then((res) => { +// var inArray = res.rows; - var resultArray = []; //empty array to start with +// var resultArray = []; //empty array to start with - for (let i = 0; i < inArray.length; i++) { - if (i == 0) { - //populate new resouce in our result array - //create new key demo_tag_relevance and push {tagname:'tagname value', relevance:'rel value'} - resultArrIndx = 0; - resultArray.push(res.rows[i]); - resultArray[resultArrIndx].demo_tag_relevance = []; - const b = {}; - b.tag_name = inArray[i].tag_name; - b.relevance = inArray[i].relevance; - resultArray[resultArrIndx].demo_tag_relevance.push(b); - resultArrIndx++; +// for (let i = 0; i < inArray.length; i++) { +// if (i == 0) { +// //populate new resouce in our result array +// //create new key demo_tag_relevance and push {tagname:'tagname value', relevance:'rel value'} +// resultArrIndx = 0; +// resultArray.push(res.rows[i]); +// resultArray[resultArrIndx].demo_tag_relevance = []; +// const b = {}; +// b.tag_name = inArray[i].tag_name; +// b.relevance = inArray[i].relevance; +// resultArray[resultArrIndx].demo_tag_relevance.push(b); +// resultArrIndx++; - } - else if (resultArray[resultArrIndx - 1].resource_id == inArray[i].resource_id) { - resultArray[resultArrIndx - 1].demo_tag_relevance.push({ 'tag_name': inArray[i].tag_name, 'relevance': inArray[i].relevance }); - } - else { - //populate new resouce in our result array - //create new key demo_tag_relevance and push {tagname:'tagname value', relevance:'rel value'} - resultArray.push(res.rows[i]); - resultArray[resultArrIndx].demo_tag_relevance = []; - const b = {}; - b.tag_name = inArray[i].tag_name; - b.relevance = inArray[i].relevance; - resultArray[resultArrIndx].demo_tag_relevance.push(b); - resultArrIndx++; - } +// } +// else if (resultArray[resultArrIndx - 1].resource_id == inArray[i].resource_id) { +// resultArray[resultArrIndx - 1].demo_tag_relevance.push({ 'tag_name': inArray[i].tag_name, 'relevance': inArray[i].relevance }); +// } +// else { +// //populate new resouce in our result array +// //create new key demo_tag_relevance and push {tagname:'tagname value', relevance:'rel value'} +// resultArray.push(res.rows[i]); +// resultArray[resultArrIndx].demo_tag_relevance = []; +// const b = {}; +// b.tag_name = inArray[i].tag_name; +// b.relevance = inArray[i].relevance; +// resultArray[resultArrIndx].demo_tag_relevance.push(b); +// resultArrIndx++; +// } - } console.log('resultArray =', resultArray); - resultArray[2].demo_tag_relevance.forEach(function (officer) { - console.log(officer.tag_name, ":", officer.relevance); - }) - }) - .catch(err => console.log(err)); +// } console.log('resultArray =', resultArray); +// resultArray[2].demo_tag_relevance.forEach(function (officer) { +// console.log(officer.tag_name, ":", officer.relevance); +// }) +// }) +// .catch(err => console.log(err)); //var newAns = resultArr.reduce(reducer); diff --git a/src/views/home2.hbs b/src/views/home2.hbs new file mode 100644 index 0000000..1502fde --- /dev/null +++ b/src/views/home2.hbs @@ -0,0 +1,17 @@ + + + + tech-access + + + +
+ diverse people coding +
+ + + + + diff --git a/src/views/results.hbs b/src/views/results.hbs new file mode 100644 index 0000000..22ba535 --- /dev/null +++ b/src/views/results.hbs @@ -0,0 +1,26 @@ + {{!-- +

Meetups

--}} + + + + + + + + \ No newline at end of file