Skip to content

Commit

Permalink
Merge pull request #41 from fac-14/challenges-dynamic-content
Browse files Browse the repository at this point in the history
Challenges dynamic content
  • Loading branch information
Simon Duprée authored Sep 13, 2018
2 parents 38b4c0c + 0c11466 commit 219daf0
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 97 deletions.
27 changes: 0 additions & 27 deletions db/db_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,3 @@ const connection =
const db = pgp(connection);

module.exports = db;

// const { Pool } = require('pg');
// const url = require('url');
// const env = require('env2');
// env('./config.env');
// let DB_URL = process.env.DATABASE_URL;

// if (process.env.NODE_ENV === 'test') {
// DB_URL = process.env.TEST_DB_URL;
// }

// if (!DB_URL) throw new Error("Environment vatialbe must be set");

// const params = url.parse(DB_URL);
// const [username, password] = params.auth.split(":");

// const options = {
// host: params.hostname,
// port: params.port,
// database: params.pathname.split("/")[1],
// max: process.env.DB_MAX_CONNECTIONS || 2,
// user: username,
// password,
// ssl: params.hostname !== "localhost"
// };

// module.exports = new Pool(options);
17 changes: 3 additions & 14 deletions db/test_build.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
// go tests
const fs = require('fs');
const dbConnection = require('./db_connection');
const fs = require("fs");
const dbConnection = require("./db_connection");

const sql = fs.readFileSync(`${__dirname}/test_build.sql`).toString();

const initialiseTestDatabase = () => dbConnection.query(sql);

// const runDbTestBuild = () =>
// new Promise((resolve, reject) => {
// dbConnection.query(sql, (err, result) => {
// if (err) {
// reject(err);
// } else {
// resolve(result);
// }
// });
// });

module.exports = initialiseTestDatabase;
module.exports = initialiseTestDatabase;
10 changes: 9 additions & 1 deletion src/controllers/challSelect.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
const queries = require("../model/index");

exports.get = (req, res) => {
res.render("challSelect", { layout: "content-selected" });
queries.getSingleChallenge().then(challengeDetails => {
console.log("challenge details: ", challengeDetails);
res.render("challSelect", {
layout: "content-selected",
challenge: challengeDetails[0]
});
});
};
18 changes: 16 additions & 2 deletions src/controllers/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
const queries = require("../model/index");

exports.get = (req, res) => {
res.render("dashboard");
};

const newChallenges = queries.getChallenges();
const acceptedChallenges = queries.getAcceptedChallenges();
const completedChallenges = queries.getCompletedChallenges();

Promise.all([newChallenges, acceptedChallenges, completedChallenges])
.then(challenges => {
res.render("dashboard", {
newChallenges: challenges[0],
acceptedChallenges: challenges[1],
completedChallenges: challenges[2]
});
})
};
7 changes: 7 additions & 0 deletions src/controllers/get-accepted-challenges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const queries = require("../model/index");

exports.get = (req, res) => {
queries.getAcceptedChallenges().then(acceptedChallenges => {
res.render("dashboard", { acceptedChallenges: acceptedChallenges });
});
};
7 changes: 7 additions & 0 deletions src/controllers/get-completed-challenges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const queries = require("../model/index");

exports.get = (req, res) => {
queries.getCompletedChallenges().then(completedChallenges => {
res.render("dashboard", { completedChallenges: completedChallenges });
});
};
4 changes: 2 additions & 2 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ router.get("/", landing.get);
// router.get("login", login.get);
// router.post("login", login.post);
router.get("/dashboard", dashboard.get);
router.get("/challenge", challSelect.get);
router.get("/challenges/:challenges", challSelect.get);
router.get("/inventory", inventory.get);
router.get("/make-error", errorRoute);
router.get("/learn", learn.get);
Expand All @@ -43,7 +43,7 @@ router.post("/challenge/completed", (req, res) => {
res.end();
});
if (process.env.NODE_ENV === "test") {
router.get("/make-error", errorRoute);
router.get("/make-error", errorRoute);
}
router.use(error.client);
// router.use(error.server);
Expand Down
51 changes: 50 additions & 1 deletion src/model/getAcceptedChallenges.js
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
// getAccepetedChallenges
// DUMMY CODE!!!!!!

const getAcceptedChallenges = () => {
return new Promise((resolve, reject) => {
const result = [
{
id: 1,
inventory_id: 2,
title: "Go to a farmers market",
what: "Go to a farmers market",
why: "you can find organic produces and save packaging waste",
ext_link: "https://www.lfm.org.uk/",
img_link:
"https://s3.eu-central-1.amazonaws.com/lfm-web-prod/images/2016_May_Balham_Herbal_Haven_herbs.0f37de8c.fill-360x360.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=3600&X-Amz-Credential=AKIAJXL7S33WNUYF726A%2F20180912%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-SignedHeaders=host&X-Amz-Date=20180912T140221Z&X-Amz-Signature=3683277a9cc390d6d2d38d8bbad863a38bf16da79f3049a897ac8dad46078746",
reward_points: 10,
repeatable: false
},
{
id: 2,
inventory_id: null,
title: "Dont buy plastic water bottles for a week",
what: "maybe buy refillable bottle",
why: "saving lots of plastic",
ext_link:
"https://www.independent.co.uk/environment/plastic-bottles-waste-recycling-pollution-single-use-keep-britain-tidy-water-a8307591.html",
img_link:
"https://www.banthebottle.net/wp-content/uploads/2009/06/ban-all-bottles-300x225.jpg",
reward_points: 10,
repeatable: true
},
{
id: 3,
inventory_id: null,
title: "Stop trashing you dick",
what: "maybe buy refillable bottle",
why: "saving lots of plastic",
ext_link:
"https://www.independent.co.uk/environment/plastic-bottles-waste-recycling-pollution-single-use-keep-britain-tidy-water-a8307591.html",
img_link:
"https://www.banthebottle.net/wp-content/uploads/2009/06/ban-all-bottles-300x225.jpg",
reward_points: 10,
repeatable: true
}
];

resolve(result);
});
};

module.exports = getAcceptedChallenges;
86 changes: 75 additions & 11 deletions src/model/getChallenges.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,76 @@
const dbConnection = require('../../db/db_connection');
// DUMMY CODE!!!!!!

module.exports = () =>
new Promise((resolve,reject) => {
dbConnection.query(
`SELECT * FROM challenges`,
(err,res) =>{
if (err) return reject(err);
resolve(res);
}
)
});
const getChallenges = () => {
return new Promise((resolve, reject) => {
const result = [
{
id: 1,
inventory_id: 2,
title: "Go to a farmers market",
what: "Go to a farmers market",
why: "you can find organic produces and save packaging waste",
ext_link: "https://www.lfm.org.uk/",
img_link:
"https://s3.eu-central-1.amazonaws.com/lfm-web-prod/images/2016_May_Balham_Herbal_Haven_herbs.0f37de8c.fill-360x360.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=3600&X-Amz-Credential=AKIAJXL7S33WNUYF726A%2F20180912%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-SignedHeaders=host&X-Amz-Date=20180912T140221Z&X-Amz-Signature=3683277a9cc390d6d2d38d8bbad863a38bf16da79f3049a897ac8dad46078746",
reward_points: 10,
repeatable: false
},
{
id: 2,
inventory_id: null,
title: "Dont buy plastic water bottles for a week",
what: "maybe buy refillable bottle",
why: "saving lots of plastic",
ext_link:
"https://www.independent.co.uk/environment/plastic-bottles-waste-recycling-pollution-single-use-keep-britain-tidy-water-a8307591.html",
img_link:
"https://www.banthebottle.net/wp-content/uploads/2009/06/ban-all-bottles-300x225.jpg",
reward_points: 10,
repeatable: true
},
{
id: 3,
inventory_id: null,
title: "Stop trashing you dick",
what: "maybe buy refillable bottle",
why: "saving lots of plastic",
ext_link:
"https://www.independent.co.uk/environment/plastic-bottles-waste-recycling-pollution-single-use-keep-britain-tidy-water-a8307591.html",
img_link:
"https://www.banthebottle.net/wp-content/uploads/2009/06/ban-all-bottles-300x225.jpg",
reward_points: 10,
repeatable: true
},
{
id: 4,
inventory_id: null,
title: "Get a reusable coffee cup",
what: "maybe buy refillable bottle",
why: "saving lots of plastic",
ext_link:
"https://www.independent.co.uk/environment/plastic-bottles-waste-recycling-pollution-single-use-keep-britain-tidy-water-a8307591.html",
img_link:
"https://www.banthebottle.net/wp-content/uploads/2009/06/ban-all-bottles-300x225.jpg",
reward_points: 10,
repeatable: true
},
{
id: 5,
inventory_id: null,
title: "Something else importat",
what: "maybe buy refillable bottle",
why: "saving lots of plastic",
ext_link:
"https://www.independent.co.uk/environment/plastic-bottles-waste-recycling-pollution-single-use-keep-britain-tidy-water-a8307591.html",
img_link:
"https://www.banthebottle.net/wp-content/uploads/2009/06/ban-all-bottles-300x225.jpg",
reward_points: 10,
repeatable: true
}
];

resolve(result);
});
};

module.exports = getChallenges;
36 changes: 35 additions & 1 deletion src/model/getCompletedChallenges.js
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
// getCompletedChalllenges
const getCompletedChallenges = () => {
return new Promise((resolve, reject) => {
const result = [
{
id: 2,
inventory_id: null,
title: "Dont buy plastic water bottles for a week",
what: "maybe buy refillable bottle",
why: "saving lots of plastic",
ext_link:
"https://www.independent.co.uk/environment/plastic-bottles-waste-recycling-pollution-single-use-keep-britain-tidy-water-a8307591.html",
img_link:
"https://www.banthebottle.net/wp-content/uploads/2009/06/ban-all-bottles-300x225.jpg",
reward_points: 10,
repeatable: true
},
{
id: 3,
inventory_id: null,
title: "Stop trashing you dick",
what: "maybe buy refillable bottle",
why: "saving lots of plastic",
ext_link:
"https://www.independent.co.uk/environment/plastic-bottles-waste-recycling-pollution-single-use-keep-britain-tidy-water-a8307591.html",
img_link:
"https://www.banthebottle.net/wp-content/uploads/2009/06/ban-all-bottles-300x225.jpg",
reward_points: 10,
repeatable: true
}
];
resolve(result);
});
};

module.exports = getCompletedChallenges;
25 changes: 25 additions & 0 deletions src/model/getSingleChallenge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// DUMMY CODE!!!!!!

const getSingleChallenge = () => {
return new Promise((resolve, reject) => {
const result = [
{
id: 5,
inventory_id: null,
title: "Something else importat",
what: "maybe buy refillable bottle",
why: "saving lots of plastic",
ext_link:
"https://www.independent.co.uk/environment/plastic-bottles-waste-recycling-pollution-single-use-keep-britain-tidy-water-a8307591.html",
img_link:
"https://www.banthebottle.net/wp-content/uploads/2009/06/ban-all-bottles-300x225.jpg",
reward_points: 10,
repeatable: true
}
];

resolve(result);
});
};

module.exports = getSingleChallenge;
24 changes: 13 additions & 11 deletions src/model/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// booo index.js
const acceptChallenge = require('./acceptChallenge')
const completeChallenge = require('./completeChallenge')
const getAcceptedChallenges = require('./getAcceptedChallenges')
const getChallenges = require('./getChallenges')
const getCompletedChallenges = require ('./getCompletedChallenges')
const acceptChallenge = require("./acceptChallenge");
const completeChallenge = require("./completeChallenge");
const getAcceptedChallenges = require("./getAcceptedChallenges");
const getChallenges = require("./getChallenges");
const getCompletedChallenges = require("./getCompletedChallenges");
const getSingleChallenge = require("./getSingleChallenge");

module.exports = {
acceptChallenge,
completeChallenge,
getAcceptedChallenges,
getChallenges,
getCompletedChallenges
}
acceptChallenge,
completeChallenge,
getAcceptedChallenges,
getChallenges,
getCompletedChallenges,
getSingleChallenge
};
36 changes: 30 additions & 6 deletions src/views/challSelect.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
{{!-- hello again --}}

<h1>this is the selected challenge</h1>

<form action="/challenge/accepted" method="POST">
<h1>Accept challenge?</h1>
<button type="submit">Yes!</button>
</form>
<section class="challenge-page">

<img class="challenge-img" src="{{challenge.img_link}}">

<h2>{{{challenge.title}}}</h2>

<article class="challenge-what">
<h3>What</h3>
<p>
{{{challenge.what}}}
</p>
</article>

<article class="challenge-why">
<h3>Why</h3>
<p>
{{{challenge.why}}}
</p>
</article>

<a href="{{{challenge.ext_link}}}" target="_blank" class="ext-link"><button>Find out more</button></a>

<form action="/challenge/accepted" method="POST">
<h1>Accept challenge?</h1>
<button type="submit">Yes!</button>
</form>



</section>
Loading

0 comments on commit 219daf0

Please sign in to comment.