Skip to content

Commit

Permalink
completed several more model queries
Browse files Browse the repository at this point in the history
related to #19
  • Loading branch information
Armand-Lluka committed Sep 13, 2018
1 parent b44961e commit 3d27932
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 34 deletions.
26 changes: 25 additions & 1 deletion src/model/acceptChallenge.js
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;


3 changes: 2 additions & 1 deletion src/model/completeChallenge.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// completeChallenges
// completeChallenges
const db = require('../../db/db_connection');
14 changes: 13 additions & 1 deletion src/model/getAcceptedChallenges.js
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;
22 changes: 11 additions & 11 deletions src/model/getChallenges.js
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;
3 changes: 2 additions & 1 deletion src/model/getCompletedChallenges.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// getCompletedChalllenges
// getCompletedChalllenges
const db = require('../../db/db_connection');
Empty file added src/model/getSingleChallenge.js
Empty file.
47 changes: 28 additions & 19 deletions test/queries.test.js
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();
})
})
})

0 comments on commit 3d27932

Please sign in to comment.