Skip to content

Commit

Permalink
fixes multiple challenge acceptance and completion issue
Browse files Browse the repository at this point in the history
Relates #48 #5 #19
  • Loading branch information
SleepySheepy172 committed Sep 17, 2018
1 parent 4d3c70d commit 2aa1448
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 20 deletions.
3 changes: 3 additions & 0 deletions db/db_build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ CREATE TABLE challenge_status
status INTEGER
);

ALTER TABLE challenge_status
ADD CONSTRAINT chall_stat_unique UNIQUE (challenge_id, user_id, status);

COMMIT;
2 changes: 1 addition & 1 deletion src/controllers/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports.get = (req, res) => {

Promise.all([newChallenges, acceptedChallenges, completedChallenges])
.then(challenges => {
console.log(challenges[2]);
// console.log(challenges[2]);

res.render("dashboard", {
newChallenges: challenges[0],
Expand Down
17 changes: 1 addition & 16 deletions src/controllers/get-accepted-challenges.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
// const queries = require("../model/index");
const queries = require("../model/index");

<<<<<<< HEAD
// exports.get = (req, res) => {
// queries.getAcceptedChallenges(userId, challengeStatus).then(challenges => {
// res.render("dashboard", { challenges });
// });
// };
||||||| merged common ancestors
exports.get = (req, res) => {
queries.getAcceptedChallenges(1, 1).then(acceptedChallenges => {
console.log("HELLO");
res.render("dashboard", { acceptedChallenges });
});
};
=======
exports.get = (req, res) => {
queries.getAcceptedChallenges(1, 1).then(acceptedChallenges => {
res.render("dashboard", { acceptedChallenges });
});
};
>>>>>>> master
3 changes: 2 additions & 1 deletion src/model/acceptChallenge.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

// Adds the accepted challenges id, the users id and the status for accepted(1) to the table challenge_status
// Only inserts if the challenge is not already accepted by that user, if it is the query does nothing

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

const acceptChallenge = (challengeId, userId) => new Promise((resolve, reject) => {
db.query(`INSERT INTO challenge_status (challenge_id, user_id, status) VALUES ($1, $2, $3);`, [challengeId, userId, 1])
db.query(`INSERT INTO challenge_status (challenge_id, user_id, status) VALUES ($1, $2, $3) ON CONFLICT ON CONSTRAINT chall_stat_unique DO NOTHING;`, [challengeId, userId, 1])
.then(res => {
resolve(res)
})
Expand Down
8 changes: 6 additions & 2 deletions src/model/completeChallenge.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// completeChallenges

// Deletes the selected challenge from challenge_status if it's already been completed
// then updates the accepted challenge to complete
// this ensures there are never any duplicate completed challenges in the db

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])
db.query('DELETE from challenge_status WHERE challenge_id = $1 AND user_id = $2 AND status = 2; 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))
})
Expand Down

0 comments on commit 2aa1448

Please sign in to comment.