diff --git a/src/model/acceptChallenge.js b/src/model/acceptChallenge.js index 7ed285b..acdb651 100644 --- a/src/model/acceptChallenge.js +++ b/src/model/acceptChallenge.js @@ -1 +1,12 @@ -// Accepted Challenges \ No newline at end of file + +// 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; diff --git a/src/model/completeChallenge.js b/src/model/completeChallenge.js index aaa8789..c28cbc8 100644 --- a/src/model/completeChallenge.js +++ b/src/model/completeChallenge.js @@ -1 +1,10 @@ -// completeChallenges \ No newline at end of file +// 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; \ No newline at end of file diff --git a/src/model/getAcceptedChallenges.js b/src/model/getAcceptedChallenges.js index 8c9cb7b..622f345 100644 --- a/src/model/getAcceptedChallenges.js +++ b/src/model/getAcceptedChallenges.js @@ -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; diff --git a/src/model/getAllChallenges.js b/src/model/getAllChallenges.js deleted file mode 100644 index a7f4161..0000000 --- a/src/model/getAllChallenges.js +++ /dev/null @@ -1,4 +0,0 @@ -const dbConnection = require('../../db/db_connection'); - -module.exports = () => - dbConnection.query('SELECT * FROM challenges'); \ No newline at end of file diff --git a/src/model/getChallenges.js b/src/model/getChallenges.js index c0aa171..5cbc907 100644 --- a/src/model/getChallenges.js +++ b/src/model/getChallenges.js @@ -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; diff --git a/src/model/getCompletedChallenges.js b/src/model/getCompletedChallenges.js index e08478e..1c19b9e 100644 --- a/src/model/getCompletedChallenges.js +++ b/src/model/getCompletedChallenges.js @@ -1,3 +1,4 @@ +// can use getAcceptedChallenges for this so delete later const getCompletedChallenges = () => { return new Promise((resolve, reject) => { const result = [ diff --git a/src/model/getSingleChallenge.js b/src/model/getSingleChallenge.js index 442d692..c9f77f7 100644 --- a/src/model/getSingleChallenge.js +++ b/src/model/getSingleChallenge.js @@ -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; diff --git a/src/model/index.js b/src/model/index.js index 5fefce4..49f0b21 100644 --- a/src/model/index.js +++ b/src/model/index.js @@ -1,16 +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 getSingleChallenge = require("./getSingleChallenge"); +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, - getSingleChallenge -}; + acceptChallenge, + completeChallenge, + getAcceptedChallenges, + getChallenges, + getCompletedChallenges, + getSingleChallenge +} diff --git a/test/queries-dummy-data.js b/test/queries-dummy-data.js new file mode 100644 index 0000000..7d6b3ef --- /dev/null +++ b/test/queries-dummy-data.js @@ -0,0 +1,15 @@ +const challengeObject = { + 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 +}; + +module.exports = challengeObject; \ No newline at end of file diff --git a/test/queries.test.js b/test/queries.test.js index 5302333..c1a7347 100644 --- a/test/queries.test.js +++ b/test/queries.test.js @@ -1,24 +1,80 @@ -// go tests - +/* eslint-disable */ const db = require("../db/db_connection"); const initialiseTestDatabase = require("../db/test_build"); const clearTestDatabase = require("../db/test_clear"); -const getChallenges = require("../src/model/getAllChallenges"); -// const runDbTestBuild = require('../db/test_build'); -// const pg = require('pg'); +const queries = require('../src/model/index') +const challengeObject = require('./queries-dummy-data') + beforeEach(() => initialiseTestDatabase()); afterEach(() => clearTestDatabase()); afterAll(() => db.$pool.end()); -console.log("db ", db); +// console.log("db ", db); -describe("getting challenges", () => { +describe("getting all challenges", () => { test("get challenges from db", () => { expect.assertions(1); - return getChallenges().then(res => { - console.log(res); + return queries.getChallenges().then(res => { expect(res).toBeTruthy(); }); }); + test('returns an object', () => { + expect.assertions(1); + return queries.getChallenges().then(res => { + expect(res[1]).toMatchObject(challengeObject); + }) + }) }); + +describe("accept challenge", () => { + test("inserting challlenge id into challenge status", () => { + expect.assertions(1); + queries.acceptChallenge(1, 1); + return queries.getAcceptedChallenges(1, 1).then(res => { + expect(res.length).toBe(2); + }) + }) +}) + +describe('get accepted challenges', () => { + test('returns the accepted challenges of specific user', () => { + expect.assertions(1); + return queries.getAcceptedChallenges(1, 1).then(res => { + // console.log(res); + expect(res).toBeTruthy(); + }) + }) + test('returns an array of objects', () => { + expect.assertions(1); + return queries.getAcceptedChallenges(1, 1).then(res => { + expect(res[0]).toMatchObject(challengeObject); + }) + }) +}) + +describe('get single challenge', () => { + test('returns the information for a specified challenge', () => { + expect.assertions(1); + return queries.getSingleChallenge(2).then(res => { + expect(res).toBeTruthy(); + }) + }) + test('returns an object', () => { + expect.assertions(1); + return queries.getSingleChallenge(2).then(res => { + expect(res[0]).toMatchObject(challengeObject); + }) + }) +}) + +describe('complete a challenge', () => { + test('change the challenge status to complete', () => { + expect.assertions(1); + return queries.completeChallenge(1, 1).then(res => { + // console.log(res); + expect(res).toBeTruthy(); + }) + }) +}) +