-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completed several more model queries
related to #19
- Loading branch information
1 parent
b44961e
commit 3d27932
Showing
7 changed files
with
81 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,25 @@ | ||
// Accepted Challenges | ||
// 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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
// completeChallenges | ||
// completeChallenges | ||
const db = require('../../db/db_connection'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
// getAccepetedChallenges | ||
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
) | ||
}); | ||
const getChallenge = () => new Promise((resolve,reject)=>{ | ||
db.query(`SELECT * FROM challenges;`) | ||
.then(res=>{ | ||
resolve(res) | ||
}) | ||
.catch(err => reject(err)) | ||
}) | ||
|
||
module.exports = getChallenge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
// getCompletedChalllenges | ||
// getCompletedChalllenges | ||
const db = require('../../db/db_connection'); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}) | ||
}) | ||
}) | ||
|