From 3d2793224df058942e0030efa5095a70151b7400 Mon Sep 17 00:00:00 2001 From: zeanort Date: Thu, 13 Sep 2018 17:38:28 +0100 Subject: [PATCH] completed several more model queries related to #19 --- src/model/acceptChallenge.js | 26 +++++++++++++++- src/model/completeChallenge.js | 3 +- src/model/getAcceptedChallenges.js | 14 ++++++++- src/model/getChallenges.js | 22 +++++++------- src/model/getCompletedChallenges.js | 3 +- src/model/getSingleChallenge.js | 0 test/queries.test.js | 47 +++++++++++++++++------------ 7 files changed, 81 insertions(+), 34 deletions(-) create mode 100644 src/model/getSingleChallenge.js diff --git a/src/model/acceptChallenge.js b/src/model/acceptChallenge.js index 7ed285b..4918d9a 100644 --- a/src/model/acceptChallenge.js +++ b/src/model/acceptChallenge.js @@ -1 +1,25 @@ -// Accepted Challenges \ No newline at end of file +// Accepted Challenges +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], +// (err,res) => { +// if (err) return reject ('no board found'); +// resolve(res); +// } +// ) +// }) + + +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..c3e9eb7 100644 --- a/src/model/completeChallenge.js +++ b/src/model/completeChallenge.js @@ -1 +1,2 @@ -// completeChallenges \ No newline at end of file +// completeChallenges +const db = require('../../db/db_connection'); diff --git a/src/model/getAcceptedChallenges.js b/src/model/getAcceptedChallenges.js index 90d368c..4a9ef2f 100644 --- a/src/model/getAcceptedChallenges.js +++ b/src/model/getAcceptedChallenges.js @@ -1 +1,13 @@ -// getAccepetedChallenges \ No newline at end of file +// getAccepetedChallenges +const db = require('../../db/db_connection'); + + +const getAccepetedChallenges = (userId, challengeStatus) => new Promise ((resolve,reject)=>{ + db.query(`SELECT challenges.title, challenges.img_link 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, hopefully + +module.exports = getAccepetedChallenges; \ No newline at end of file diff --git a/src/model/getChallenges.js b/src/model/getChallenges.js index 2a2f184..0ee9fcc 100644 --- a/src/model/getChallenges.js +++ b/src/model/getChallenges.js @@ -1,12 +1,12 @@ -const dbConnection = require('../../db/db_connection'); +/* eslint-disable */ +const db = require('../../db/db_connection'); -module.exports = () => - new Promise((resolve,reject) => { - dbConnection.query( - `SELECT * FROM challenges`, - (err,res) =>{ - if (err) return reject(err); - resolve(res); - } - ) - }); \ No newline at end of file +const getChallenge = () => new Promise((resolve,reject)=>{ + db.query(`SELECT * FROM challenges;`) + .then(res=>{ + resolve(res) + }) + .catch(err => reject(err)) + }) + + module.exports = getChallenge; \ No newline at end of file diff --git a/src/model/getCompletedChallenges.js b/src/model/getCompletedChallenges.js index 5708e11..6c94e70 100644 --- a/src/model/getCompletedChallenges.js +++ b/src/model/getCompletedChallenges.js @@ -1 +1,2 @@ -// getCompletedChalllenges \ No newline at end of file +// getCompletedChalllenges +const db = require('../../db/db_connection'); diff --git a/src/model/getSingleChallenge.js b/src/model/getSingleChallenge.js new file mode 100644 index 0000000..e69de29 diff --git a/test/queries.test.js b/test/queries.test.js index 8343fc7..e7051e6 100644 --- a/test/queries.test.js +++ b/test/queries.test.js @@ -1,37 +1,46 @@ -// 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') + beforeEach(() => initialiseTestDatabase()); afterEach(() => clearTestDatabase()); afterAll(() => db.$pool.end()); -console.log("db ", db); +// console.log("db ", db); describe("getting challenges", () => { test("get challenges from db", () => { expect.assertions(1); - return getChallenges().then(res => { - console.log(res); + return queries.getChallenges().then(res => { + // console.log(res); expect(res).toBeTruthy(); }); }); }); -// describe('Test DB is generating', () => { -// test('getting a result db', done => { -// .then(res => { -// console.log('passed db build') -// expect(res).toBeTruthy(); -// done(); -// }) -// .catch(err => { -// console.log('err building db'); -// }) -// }) -// }) +describe("accept challenge", () => { + test("inserting challlenge id into challenge status", () => { + expect.assertions(1); + queries.acceptChallenge(1, 1); + return queries.getAcceptedChallenges(1,1).then(res => { + console.log(res.length); + // getAccepted challenge required here :| + 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(); + }) + }) +}) +