Skip to content

Commit

Permalink
Merge pull request #43 from fac-14/query
Browse files Browse the repository at this point in the history
Adds DB Queries
  • Loading branch information
Simon Duprée authored Sep 14, 2018
2 parents 219daf0 + 2f3ea68 commit bfadb2f
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 170 deletions.
13 changes: 12 additions & 1 deletion src/model/acceptChallenge.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
// Accepted Challenges

// Adds the accepted challenges id, the users id and the status for accepted(1) to the table challenge_status

const db = require('../../db/db_connection');

const acceptChallenge = (id, userId) => new Promise((resolve, reject) => {
db.query(`INSERT INTO challenge_status(user_id, challenge_id, status) VALUES ($1, $2, $3)`, [id, userId, 1])
.then(res => resolve(res))
.catch(err => reject(err))
})

module.exports = acceptChallenge;
11 changes: 10 additions & 1 deletion src/model/completeChallenge.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
// completeChallenges
// completeChallenges
const db = require('../../db/db_connection');

const completeChallenge = (challengeId, userId) => new Promise((resolve, reject) => {
db.query('UPDATE challenge_status SET status = 2 WHERE challenge_id = $1 AND user_id = $2 RETURNING *', [challengeId, userId])
.then(res => resolve(res))
.catch(err => reject(err))
})

module.exports = completeChallenge;
110 changes: 62 additions & 48 deletions src/model/getAcceptedChallenges.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,64 @@

// Gets the users accepted or completed challenges depending on the status code given (1 = accepted and 2 = complete)

const db = require('../../db/db_connection');

const getAccepetedChallenges = (userId, challengeStatus) => new Promise((resolve, reject) => {
db.query(`SELECT * FROM challenges INNER JOIN challenge_status ON challenges.id = challenge_status.challenge_id WHERE user_id = $1 AND status = $2;`, [userId, challengeStatus])
.then(res => resolve(res))
.catch(err => reject(err))
})

// should work for accepted and completed challenges just change challengeStatus

module.exports = 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);
// });
// };

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;
// module.exports = getAcceptedChallenges;
4 changes: 0 additions & 4 deletions src/model/getAllChallenges.js

This file was deleted.

156 changes: 84 additions & 72 deletions src/model/getChallenges.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,88 @@
// DUMMY CODE!!!!!!

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
}
];
// Gets all info for all the challenges

const db = require('../../db/db_connection');

resolve(result);
});
};
const getChallenges = () => new Promise((resolve, reject) => {
db.query(`SELECT * FROM challenges;`)
.then(res => resolve(res))
.catch(err => reject(err))
})

module.exports = getChallenges;

// DUMMY CODE!!!!!!
// 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;
1 change: 1 addition & 0 deletions src/model/getCompletedChallenges.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// can use getAcceptedChallenges for this so delete later
const getCompletedChallenges = () => {
return new Promise((resolve, reject) => {
const result = [
Expand Down
57 changes: 35 additions & 22 deletions src/model/getSingleChallenge.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
// 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);
});
};
// Gets all the info for a single challenge using the specified id

const db = require('../../db/db_connection');

const getSingleChallenge = (challenge_id) => new Promise((resolve, reject) => {
db.query(`SELECT * FROM challenges WHERE id = $1;`, [challenge_id])
.then(res => resolve(res))
.catch(err => reject(err))
})

module.exports = getSingleChallenge;

// 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;
Loading

0 comments on commit bfadb2f

Please sign in to comment.