Skip to content

Commit

Permalink
implement getData and data structure >relates #28
Browse files Browse the repository at this point in the history
  • Loading branch information
sunuwars committed Sep 18, 2018
1 parent c3669ef commit ac9178b
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 47 deletions.
53 changes: 53 additions & 0 deletions Documents/bower_initial_css_install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

<!-- This is the terminal snapshot of steps of initial bower/? css install steps after git pull-->
```
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 [email protected]: 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
+ [email protected]
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
```
2 changes: 1 addition & 1 deletion src/controllers/home.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.get = (req, res) => {
res.render('home', {activePage:{home: true}})
res.render('home2', {activePage:{home2: true}})
}
4 changes: 4 additions & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
12 changes: 7 additions & 5 deletions src/controllers/resources.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
exports.get = (req, res) => {
res.render('resources', {
layout: 'scrollable'
});
}

exports.get = (req, res) => {
res.render('resources', {
layout: 'scrollable'
});
}

103 changes: 103 additions & 0 deletions src/controllers/results.js
Original file line number Diff line number Diff line change
@@ -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;
}

}
82 changes: 41 additions & 41 deletions src/model/getData.js
Original file line number Diff line number Diff line change
@@ -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' " ;
Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/views/home2.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>tech-access</title>
</head>
<body>
<nav>
<h2>Tech Access App</h2>
</nav>
<figure>
<img src="/images/unsplashpeoplecoding.jpeg" alt="diverse people coding" width:"200" height:"200">
</figure>

</body>

</html>

26 changes: 26 additions & 0 deletions src/views/results.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{!--
<h3>Meetups</h3> --}}
<ul>
{{#each outArray.meetup}}
<li>{{this.resource_name}} {{this.resource_type}}</li>
{{/each}}
</ul>

<ul>
{{#each outArray.article}}
<li>{{this.resource_name}} {{this.resource_type}}</li>
{{/each}}
</ul>


<ul>
{{#each outArray.[classroom course]}}
<li>{{this.resource_name}} {{this.resource_type}}</li>
{{/each}}
</ul>

<ul>
{{#each outArray.[online course] }}
<li>{{this.resource_name}} {{this.resource_type}}</li>
{{/each}}
</ul>

0 comments on commit ac9178b

Please sign in to comment.