Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updatepoints #61

Merged
merged 13 commits into from
Sep 18, 2018
2 changes: 1 addition & 1 deletion db/test_build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ INSERT INTO challenge_status
VALUES
(1, 1, 0),
(2, 1, 1),
(1, 2, 2),
(1, 1, 2),
(2, 2, 1),
(1, 3, 0),
(2, 3, 1);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions public/js/navbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var getPoints = document.querySelector(".getPoints");

function render(number) {
getPoints.innerHTML = number
}

fetch("http://localhost:3000/userpoints/")
.then(function (res) {
return res.json();
}).then(function (myjson) {
render(myjson.user_points.points);
})


2 changes: 1 addition & 1 deletion src/controllers/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ exports.get = (req, res) => {
const newChallenges = queries.getChallenges();
const acceptedChallenges = queries.getAcceptedChallenges(1, 1);
const completedChallenges = queries.getAcceptedChallenges(1, 2);
// const getUserPoints = queries.getUserPoints(1);

Promise.all([newChallenges, acceptedChallenges, completedChallenges])
.then(challenges => {

res.render("dashboard", {
newChallenges: challenges[0],
acceptedChallenges: challenges[1],
Expand Down
8 changes: 8 additions & 0 deletions src/controllers/get-User-Points.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const queries = require("../model/index");

exports.get = (req, res) => {
queries.getUserPoints(1).then(points => {
res.send({ user_points: points[0] });
res.end();
});
};
21 changes: 8 additions & 13 deletions src/controllers/getSingleTopic.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
const topics = require('../model/topics')

exports.get = (req, res, next) => {
const { singleTopic } = req.params;


for( let i =0; i < topics.length; i++){
if (topics[i].title.includes(singleTopic)) {
const topicDetails = topics[i];
exports.get = (req, res) => {
const { singleTopic } = req.params;
for (let i = 0; i < topics.length; i++) {
if (topics[i].title.includes(singleTopic)) {
const topicDetails = topics[i];
return res.render('singletopic', { topicDetails });
}
}


next();
};

}
};

17 changes: 6 additions & 11 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ const challSelect = require("./challSelect");
const inventory = require("./inventory");
const suggestedItem = require("./suggested-item-select");
const boughtItem = require("./bought-item-select");

const learn = require("./learn");
const userStats = require("./user-stats");

const queries = require('../model/index')
const singleTopic = require('./getSingleTopic');


const queries = require("../model/index");
const singleTopic = require("./getSingleTopic");
const errorRoute = require("./error-route");
const error = require("./error");
const getUserPoints = require("./get-User-Points");

router.get("/", landing.get);
// router.get("signup", signup.get);
Expand All @@ -36,13 +33,13 @@ router.get("/comp-challenges/:id", challSelect.get);
router.get("/inventory", inventory.get);
router.get("/inventory/suggested-item/:id", suggestedItem.get);
router.get("/inventory/bought-item/:id", boughtItem.get);

router.get("/make-error", errorRoute);
router.get("/learn", learn.get);
router.get("/stats", userStats.get);
router.get("/learn/:singleTopic", singleTopic.get);


// Sending the points object
router.get("/userpoints", getUserPoints.get);

// accepting and completing challenges
router.post("/challenge/accepted/", (req, res) => {
Expand All @@ -53,11 +50,9 @@ router.post("/challenge/accepted/", (req, res) => {
});

router.post("/challenge/completed/", (req, res) => {
console.log(req.body);

// insert query here to add completed challenge to database
queries.completeChallenge(req.body.challenge_id, 1);
console.log("Challenge completed!");
// console.log("Challenge completed!");
res.redirect(302, "/dashboard");
res.end();
});
Expand Down
3 changes: 2 additions & 1 deletion src/model/completeChallenge.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

const updatePoints = require('./updatePoints')
// 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
Expand All @@ -7,6 +7,7 @@ const db = require('../../db/db_connection');

const completeChallenge = (challengeId, userId) => new Promise((resolve, reject) => {
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 => updatePoints(res[0].challenge_id, res[0].user_id))
.then(res => resolve(res))
.catch(err => reject(err))
})
Expand Down
13 changes: 13 additions & 0 deletions src/model/getUserPoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const db = require("../../db/db_connection");

const getUserPoints = (user_id) =>
new Promise((resolve, reject) => {
db.query(
'SELECT points FROM users WHERE id = $1', [user_id]
)
.then(res => resolve(res))
.catch(err => reject(err));
});


module.exports = getUserPoints;
6 changes: 5 additions & 1 deletion src/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const getBoughtItems = require("./getBoughtItems");
const getSuggestedItems = require("./getSuggestedItems");
const getSingleItem = require("./getSingleItem");
const buyItem = require("./buyItem");
const updatePoints = require("./updatePoints");
const getUserPoints = require("./getUserPoints");

module.exports = {
acceptChallenge,
Expand All @@ -20,5 +22,7 @@ module.exports = {
getBoughtItems,
getSuggestedItems,
getSingleItem,
buyItem
buyItem,
updatePoints,
getUserPoints
};
13 changes: 13 additions & 0 deletions src/model/updatePoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const db = require("../../db/db_connection");

const updatePoints = (challenge_id, user_id) =>
new Promise((resolve, reject) => {
db.query(
"update users set points = points + reward_points FROM challenges where challenges.id = $1 AND users.id = $2",
[challenge_id, user_id]
)
.then(res => resolve(res))
.catch(err => reject(err));
});

module.exports = updatePoints;
114 changes: 58 additions & 56 deletions src/views/dashboard.hbs
Original file line number Diff line number Diff line change
@@ -1,64 +1,66 @@
{{!-- <body onload="document.getElementById('defaultOpen').click()"> --}}
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'new-challenge')" id="defaultOpen">New Challenges</button>
<button class="tablinks" onclick="openCity(event, 'accepted-challenge')">Accepted Challenges</button>
<button class="tablinks" onclick="openCity(event, 'completed-challenge')">Completed Challenges</button>
</div>


<section class="challenges-container">
{{!--

<!-- New Challenges -->
<section id="new-challenge" class="challenges-container tabcontent">
<header class="challenges-header">
<h2 class="main-title">New Challenges</h2>
</header>
<div class="challenges-container">
{{#each newChallenges}}
<div class="challenge-overview">
{{!-- <img src='{{this.img_link}}'> --}}
<h3 class="newChallH3">{{this.title}} : {{this.id}}</h3>
<p>Reward Points: {{this.reward_points}}</p>
<button class="new-challenge-button" id="{{this.id}}" onclick="window.location.href='/new-challenges/{{this.id}}'">
Find out more
</button>
<body onload="document.getElementById('defaultOpen').click()"> --}}
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'new-challenge')" id="defaultOpen">New Challenges</button>
<button class="tablinks" onclick="openCity(event, 'accepted-challenge')">Accepted Challenges</button>
<button class="tablinks" onclick="openCity(event, 'completed-challenge')">Completed Challenges</button>
</div>
{{/each}}

</section>
<!-- New Challenges -->
<section id="new-challenge" class="challenges-container tabcontent">

<!-- Accepted Challenges -->
<header class="challenges-header">
<h2 class="main-title">New Challenges</h2>
</header>
<div class="challenges-container">
{{#each newChallenges}}
<div class="challenge-overview">
{{!-- <img src='{{this.img_link}}'> --}}
<h3 class="newChallH3">{{this.title}} : {{this.id}}</h3>
<p>Reward Points: {{this.reward_points}}</p>
<button class="new-challenge-button" id="{{this.id}}" onclick="window.location.href='/new-challenges/{{this.id}}'">
Find out more
</button>
</div>
{{/each}}

<section id="accepted-challenge" class="challenges-container tabcontent">
<header class="challenges-header">
<h2 class="main-title">Accepted Challenges</h2>
</header>
<div class="challenges-container">
{{#each acceptedChallenges}}
<div class="challenge-overview">
{{!-- <img src='{{this.img_link}}'>
--}}
<h3>{{this.title}} : {{ this.challenge_id }}</h3>
<p>Reward Points: {{this.reward_points}}</p>
<button class="acc-challenge-button" id="{{this.id}}" onclick="window.location.href='/acc-challenges/{{this.challenge_id}}'">
Find out more </button> {{/each}}
</div>
</section>
</section>

<!-- Accepted challenge -->
<!-- Accepted Challenges -->

<section id="completed-challenge" class="challenges-container tabcontent">
<header class="challenges-header">
<h2 class="main-title">Completed Challenges</h2>
</header>
<div class="challenges-container">
{{#each completedChallenges}}
<div class="challenge-overview">
<h3>{{this.title}} : {{ this.challenge_id }}</h3>
<p>Reward Points: {{this.reward_points}}</p>
<button class="challenge-button" id="id-{{this.id}}" onclick="window.location.href='/comp-challenges/{{this.challenge_id}}'">
Find out more
</button>
{{/each}}
</div>
<section id="accepted-challenge" class="challenges-container tabcontent">
<header class="challenges-header">
<h2 class="main-title">Accepted Challenges</h2>
</header>
<div class="challenges-container">
{{#each acceptedChallenges}}
<div class="challenge-overview">
{{!-- <img src='{{this.img_link}}'>
--}}
<h3>{{this.title}} : {{ this.challenge_id }}</h3>
<p>Reward Points: {{this.reward_points}}</p>
<button class="acc-challenge-button" id="{{this.id}}" onclick="window.location.href='/acc-challenges/{{this.challenge_id}}'">
Find out more </button> {{/each}}
</div>
</section>

<!-- Accepted challenge -->

<section id="completed-challenge" class="challenges-container tabcontent">
<header class="challenges-header">
<h2 class="main-title">Completed Challenges</h2>
</header>
<div class="challenges-container">
{{#each completedChallenges}}
<div class="challenge-overview">
<h3>{{this.title}} : {{ this.challenge_id }}</h3>
<p>Reward Points: {{this.reward_points}}</p>
<button class="challenge-button" id="id-{{this.id}}" onclick="window.location.href='/comp-challenges/{{this.challenge_id}}'">
Find out more
</button>
{{/each}}
</div>

</section>
</section>
9 changes: 0 additions & 9 deletions src/views/helpers/getPoints.js

This file was deleted.

Empty file added src/views/intro-slides.hbs
Empty file.
5 changes: 3 additions & 2 deletions src/views/partials/navbar.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<header>
<nav>
<div id="xp-section">
<h3 class="getPoints"></h3>
<h3>XP</h3>
<progress max="100" value="50" id="xp-bar"></progress>
</div>
<div id="avatar-section">
<h3 class="level">Waste Warrior</h3>
<img id="profile-pic" src="/public/imgs/Avatar.png" alt="Profile picture">
<h3 class="level">Waste Warrior</h3>
<img id="profile-pic" src="/public/imgs/Avatar.png" alt="Profile picture">
</div>
</nav>
</header>
3 changes: 2 additions & 1 deletion src/views/partials/scripts.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<script src="/public/js/challenges.js"></script>
<script src="/public/js/dom.js"></script>
<script src="/public/js/dom.js"></script>
<script src="/public/js/navbar.js"></script>
1 change: 1 addition & 0 deletions src/views/test.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2>{{{user_points.points}}}</h2>
12 changes: 11 additions & 1 deletion test/queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,21 @@ describe("buy an item", () => {
});
});

describe("Update points for single user", () => {
test("Check if we can get reward points for user 1", () => {
expect.assertions(1);
return queries.updatePoints(1, 1).then(res => {
console.log(res)
expect(res).toBeTruthy();
})
})
})

describe("Get single query", () => {
test("Check that a single challenge is returned", () => {
expect.assertions(1);
return queries.getSingleChallenge(1.1).then(res => {
expect(res).toBeTruthy();
});
});
});
});
Loading