Skip to content

Commit

Permalink
rename db_variable names, construct getData promise to query database…
Browse files Browse the repository at this point in the history
…, realtes to issue #25
  • Loading branch information
sunuwars committed Sep 14, 2018
1 parent 83c794b commit 6336bf1
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 23 deletions.
14 changes: 7 additions & 7 deletions src/database/db_build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ DROP TYPE IF EXISTS e_relevance CASCADE;
CREATE TYPE e_resource_type AS ENUM ('meetup', 'online course', 'article', 'classroom course' );

CREATE TABLE resource (
id SERIAL PRIMARY KEY,
resource_id SERIAL PRIMARY KEY,
resource_name TEXT NOT NULL,
link TEXT NOT NULL,
url TEXT NOT NULL,
resource_description TEXT NOT NULL,
resource_type e_resource_type,
country VARCHAR(100) DEFAULT NULL,
city VARCHAR(100) DEFAULT NULL

);

INSERT INTO resource (resource_name, link, resource_description, resource_type, country, city) VALUES
INSERT INTO resource (resource_name, url, resource_description, resource_type, country, city) VALUES
('Codebar meetup', 'https://codebar.io/events', 'goal is to enable underrepresented people to learn programming in a safe and collaborative environment and expand their career opportunities..', 'meetup', 'UK', NULL),
('Founders and Coders meetup', 'https://www.meetup.com/founderscoders/','Founders & Coders runs a full-time no-fee coding bootcamp. We run meetups for the general public, but we run even more meetups and workshops for applicants to our programme. ', 'meetup', 'UK', 'london' ),
('Tania Rascia', 'https://www.taniarascia.com/how-i-made-a-career-change-into-web-development/', 'I made a career change into web development in 2015. Before that, I worked as a chef.', 'article', NULL, NULL),
Expand All @@ -32,7 +32,7 @@ INSERT INTO resource (resource_name, link, resource_description, resource_type,
CREATE TYPE e_demographic_tag_name AS ENUM ('age', 'ethnicity', 'disability', 'mental health', 'contacts in industry', 'caring responsibilities', 'education', 'socioeconomic', 'language', 'gender', 'sexuality', 'refugee', 'other');

CREATE TABLE demographic_tag (
id SERIAL PRIMARY KEY,
demographic_tag_id SERIAL PRIMARY KEY,
tag_name e_demographic_tag_name NOT NULL,
demographic_description TEXT
);
Expand All @@ -55,12 +55,12 @@ INSERT INTO demographic_tag (tag_name, demographic_description) VALUES

CREATE TYPE e_relevance AS ENUM ('direct', 'indirect');
CREATE TABLE demographic_resource_link(
resource_id INTEGER REFERENCES resource(id) NOT NULL,
demographic_tag_id INTEGER REFERENCES demographic_tag(id) NOT NULL,
link_resource_id INTEGER REFERENCES resource(resource_id) NOT NULL,
link_demographic_tag_id INTEGER REFERENCES demographic_tag(demographic_tag_id) NOT NULL,
relevance e_relevance
);

INSERT INTO demographic_resource_link (resource_id, demographic_tag_id, relevance) VALUES
INSERT INTO demographic_resource_link (link_resource_id, link_demographic_tag_id, relevance) VALUES
(1, 1, 'direct'),
(1, 2, 'direct'),
(1, 3, 'indirect'),
Expand Down
75 changes: 75 additions & 0 deletions src/model/getData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 = '${
inputArray[0]
}'`;
// 'refugee' OR demographic_tag.tag_name = 'ethnicity' OR demographic_tag.tag_name = 'age' " ;

//iterator appends users selected demographic tags
for (let i = 0; i < inputArray.length - 1; i++) {
dataQuery += ` OR demographic_tag.tag_name = '${inputArray[i]}'`;
}
console.log("DATAQUERY=", dataQuery);

return new Promise((resolve, reject) => {
db.query(dataQuery)
.then(res => resolve(res))
.catch(err => reject(err));
});
};
module.exports = getResource;

console.log("HERE");

// getResource(['age', 'disability', 'gender'])
// .then(res => console.log(res.rows))
// .catch(err => console.log(err))
const resultArr = getResource(["age", "disability", "gender"])
.then(res => {
var resultArr = res.rows;
var newAns = resultArr.reduce(reducer);
console.log("HELLO", newAns);
})
.catch(err => console.log(err));

////////

//myArray.reduce(reducer)
// var myarr = [1, 2, 3];
// const reducer = (accumulator, currentvalue) => {
// return accumulator + currentvalue;
// };

// var sum = myarr.reduce(reducer);
// console.log(sum);
var accumulator = []; //empty array to start with
const reducer = (accumulator, currentvalue) => {
// accumulator.forEach(function(element) {
// if (element.resource_id == currentvalue.resource_id) {
// //if one of the relevance is direct set our result's relevance to direct
// if (element.relevance == "direct" || currentvalue.relevance == "direct") {
// element.relevane = "direct";
// }
// } else {
// //populate new resouce in our result array
// accumulator.push(currentvalue);
// }
// });
for (let i = 0; i < accumulator.length; i++) {
if (accumulator[i].resource_id == currentvalue.resource_id) {
if (
accumulator[i].relevance == "direct" ||
currentvalue.relevance == "direct"
) {
accumulator[i].relevance = "direct";
}
} else {
//populate new resouce in our result array
accumulator.push(currentvalue);
}
}
};

//var newAns = resultArr.reduce(reducer);
//console.log("HELLO", newAns);
13 changes: 0 additions & 13 deletions src/model/get_age_resource.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/model/get_demo_tag_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const db = require("../database/db_connection.js");

const getDemoTagId = demoTag => {
return new Promise((resolve, reject) => {
db.query(
`SELECT demographic_tag.id FROM demographic_tag WHERE tag_name = '${demoTag}'`
)
.then(res => resolve(res))
.catch(err => reject(err));
});
};

module.exports = getDemoTagId;
30 changes: 30 additions & 0 deletions src/model/get_resource_detail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const db = require("../database/db_connection");

const getResourceDetail = resourceId => {
return new Promise((resolve, reject) => {
db.query(`SELECT * from resource WHERE id ='${resourceId}' `)
.then(res => resolve(res))
.catch(err => reject(err));
});
};

module.exports = getResourceDetail;
console.log("herehere");
getResourceDetail(1)
.then(res => console.log(res.rows))
.catch(err => console.log(err));

// const getResourceDetail = resourceId =>{
// let query = `SELECT * FROM resource WHERE id = '${resourceId}'`;
// db.query(query, (err, res) => {
// if(err){
// cb('Error', null);
// } else {
// cb(null, res);
// }
// })
// }

// module.exports = getResourceDetail;

// console.log("HERE", getResourceDetail(1, callback()));
10 changes: 7 additions & 3 deletions src/model/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const getDemoTagId = require('./get_age_resource');
const getDemoTagId = require("./get_demo_tag_id");
const getResourceDetail = require("./get_resource_detail");
const getResource = require("./getResource");

module.exports = {
getDemoTagId: getDemoTagId
};
getDemoTagId: getDemoTagId,
getResourceDetail: getResourceDetail,
getResource: getResource
};

0 comments on commit 6336bf1

Please sign in to comment.