From 9fa4aa179abc06621b97db33f4d8ede456fc63f3 Mon Sep 17 00:00:00 2001 From: Braeden57 Date: Mon, 4 Jan 2021 10:18:19 -0800 Subject: [PATCH] Update --- .env.example | 5 + .gitignore | 9 +- README.md | 25 +- app.js | 5 +- config/keys.js | 6 +- models/Quest.js | 28 +++ node_modules/dotenv/package.json | 12 +- routes/index.js | 21 +- routes/users.js | 58 ++++- views/quests.ejs | 36 +++ views/studentDashboard.ejs | 161 +++++++++++- views/teacherDashboard.ejs | 406 +++++++++++++++++++------------ 12 files changed, 590 insertions(+), 182 deletions(-) create mode 100644 .env.example create mode 100644 models/Quest.js create mode 100644 views/quests.ejs diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..082005f --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +# Port to start server on. +PORT=5000 + +# MongoDB connection URI. +DB_URI=mongodb://localhost/knoldus diff --git a/.gitignore b/.gitignore index ee4c926..33d32f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,8 @@ -/test +# Dependencies. +node_modules/* + +# Tests folder. +test/* + +# Enviroment variables +.env diff --git a/README.md b/README.md index 5cd0563..e57ba86 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,28 @@ # XP-System + School Project This school project is for my teacher to have students log in and check out their XP and assignments. -(Still not done)! + +### Requirements : +1. [Node.js](https://nodejs.org/en/) +1. [MongoDB](https://docs.mongodb.com/manual/administration/install-community/) + +### Getting Started with Code : +1. [Set Up MongoDB](#setting-up-mongodb) and start mongodb +2. Clone repo from https://github.com/Braeden57/XP-System.git +3. Run `npm install` to install dependencies. +4. Duplicate `.env.example` and rename the new file to `.env`. Edit to your configurations. +1. Run `npm start` to boot up server. +1. Go to http://localhost:5000. + +### Setting up MongoDB +1. Install mongodb via brew. `brew install mongodb` +1. Start mongodb via brew. `brew services restart mongodb` +2. Or Install mongodb compass https://www.mongodb.com/try/download/compass + +### Accessing the Database +1. Locally this will use the knoldus db (or whatever you specify manually) +1. launch mongo via your command-line: `mongo` +1. Use `show dbs` to see all that are available. You should see `knoldus` in the list. +1. Lets use that db: `use knoldus`. diff --git a/app.js b/app.js index 4c83c00..c7ab50a 100644 --- a/app.js +++ b/app.js @@ -7,7 +7,8 @@ const session = require('express-session'); const bodyParser = require('body-parser'); var path = require('path'); var fs = require('fs'); -require('dotenv/config'); +var dotenv = require('dotenv'); +dotenv.config(); const app = express(); @@ -20,7 +21,7 @@ const db = require('./config/keys').mongoURI; // Connect to MongoDB mongoose .connect( - db, + process.env.DB_URI || 'mongodb://localhost/knoldus', { useNewUrlParser: true, useUnifiedTopology: true} ) .then(() => console.log('MongoDB Connected')) diff --git a/config/keys.js b/config/keys.js index 97523e1..8be02f9 100644 --- a/config/keys.js +++ b/config/keys.js @@ -1,4 +1,8 @@ -let dbPassword = 'mongodb+srv://client:backyardpassclient@backyard-pass-data.nbikf.mongodb.net/XPmockDB?retryWrites=true&w=majority'; +const dotenv = require('dotenv'); +// Load environment variables into process +dotenv.config() + +let dbPassword = process.env.DB_URI; module.exports = { mongoURI: dbPassword diff --git a/models/Quest.js b/models/Quest.js new file mode 100644 index 0000000..131f70b --- /dev/null +++ b/models/Quest.js @@ -0,0 +1,28 @@ +const mongoose = require('mongoose'); + +const QuestSchema = new mongoose.Schema({ + title: { + type: String, + required: true + }, + campaign: { + type: String, + required: true + }, + expiry: { + type: Date, + default: Date.now + }, + xp: { + type: Number, + required: true + }, + instruction: { + type: String, + required: true + } +}); + +const Quest = mongoose.model('Quest', QuestSchema); + +module.exports = Quest; diff --git a/node_modules/dotenv/package.json b/node_modules/dotenv/package.json index 4ceba1e..94993c8 100644 --- a/node_modules/dotenv/package.json +++ b/node_modules/dotenv/package.json @@ -1,19 +1,19 @@ { - "_from": "dotenv", + "_from": "dotenv@^8.2.0", "_id": "dotenv@8.2.0", "_inBundle": false, "_integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==", "_location": "/dotenv", "_phantomChildren": {}, "_requested": { - "type": "tag", + "type": "range", "registry": true, - "raw": "dotenv", + "raw": "dotenv@^8.2.0", "name": "dotenv", "escapedName": "dotenv", - "rawSpec": "", + "rawSpec": "^8.2.0", "saveSpec": null, - "fetchSpec": "latest" + "fetchSpec": "^8.2.0" }, "_requiredBy": [ "#USER", @@ -21,7 +21,7 @@ ], "_resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", "_shasum": "97e619259ada750eea3e4ea3e26bceea5424b16a", - "_spec": "dotenv", + "_spec": "dotenv@^8.2.0", "_where": "C:\\Users\\flami\\OneDrive\\Desktop\\Projects\\XP-System", "bugs": { "url": "https://github.com/motdotla/dotenv/issues" diff --git a/routes/index.js b/routes/index.js index dd611b6..35712ad 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,19 +1,28 @@ const express = require('express'); const router = express.Router(); const { ensureAuthenticated, forwardAuthenticated } = require('../config/auth'); -var UserModel = require('../models/User.js'); +// DB Config +const db = require('../config/keys').mongoURI; +let MongoClient = require('mongodb').MongoClient; +let UserModal = require('../models/User.js'); +let QuestModal = require('../models/Quest.js') // Welcome Page router.get('/', forwardAuthenticated, (req, res) => res.render('welcome')); // Dashboard router.get('/dashboard', ensureAuthenticated, function(req, res) { - UserModel.find({}, function(err, data) { - res.render('dashboard', { - user : req.user, - members: data - }); + // Collects Quest Data + QuestModal.find({}, function(err, quests) { + // Collects User Data + UserModal.find({}, function(err, data) { + res.render('dashboard', { + user : req.user, + members: data, + quests: quests + }); }); + }); }); module.exports = router; diff --git a/routes/users.js b/routes/users.js index 4ebcfda..9a73bd1 100644 --- a/routes/users.js +++ b/routes/users.js @@ -5,6 +5,7 @@ const bcrypt = require('bcryptjs'); const passport = require('passport'); const MongoClient = require('mongodb').MongoClient; const ObjectId = require('mongodb').ObjectId; +const Quest = require('../models/Quest'); // Load Profanity filter const Filter = require('bad-words'); @@ -33,6 +34,15 @@ let storage = multer.diskStorage({ let upload = multer({ storage: storage }); +let questsList; +// Collects Quest Data +Quest.find({}, function(err, quests) { + questsList = quests; +}); + +// Show Quest page +router.get('/quests', (req, res) => res.render('quests', { quests: questsList })); + // Login Page router.get('/login', forwardAuthenticated, (req, res) => res.render('login')); @@ -42,6 +52,52 @@ router.get('/register', forwardAuthenticated, (req, res) => res.render('register // Edit page router.get('/edit', (req, res) => res.render('edit')); +// Create Quest +router.post('/createQuest', (req, res, next) => { + const { title, amount, instructions, campaign} = req.body; + + let errors = []; + + // Checks if Quest with title exists + Quest.findOne({ title: title }).then(quest => { + if (quest) { + errors.push({ msg: 'Quest Already Exists' }); + // Redirect to dashboard page with errors + res.render('dashboard', { + errors, + name, + email, + password, + password2 + }); + } else { + + try { + // Creates new User Object + const newQuest = new Quest({ + title: title, + campaign: campaign, + xp: amount, + instruction: instructions + }); + newQuest + .save() + .then(user => { + req.flash( + 'success_msg' + ); + // Takes to Dashboard on success + let value = encodeURIComponent('createdQuest') + res.redirect('/dashboard?successRate=' + value); + }) + .catch(err => console.log(err)); + } catch (err) { + if (err) throw err; + } + } + }); +}); + // Add XP router.post('/addXP', (req, res, next) => { const { _id_add, current_xp, amount } = req.body; @@ -59,7 +115,7 @@ router.post('/addXP', (req, res, next) => { } if (amount < 50 && amount > 0 && current_xp < 800) { - let newXP = current_xp + amount; + let newXP = parseInt(current_xp) + parseInt(amount); if (newXP > 800) { newXP = 800; } diff --git a/views/quests.ejs b/views/quests.ejs new file mode 100644 index 0000000..183aa02 --- /dev/null +++ b/views/quests.ejs @@ -0,0 +1,36 @@ + + + + +Back to Dashboard + + +
diff --git a/views/studentDashboard.ejs b/views/studentDashboard.ejs index b12c829..9b1be96 100644 --- a/views/studentDashboard.ejs +++ b/views/studentDashboard.ejs @@ -1,5 +1,23 @@
@@ -78,10 +131,97 @@ Logout - Report Bug + Report Bug
+
+

Quests

+
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + diff --git a/views/teacherDashboard.ejs b/views/teacherDashboard.ejs index 75fef3e..4e933b2 100644 --- a/views/teacherDashboard.ejs +++ b/views/teacherDashboard.ejs @@ -5,12 +5,43 @@ display: inline-block; } .head { - margin-bottom: 100px; + position: absolute; + width: 345px; + height: 600px; + top: 25px; + left: 25px; + border: solid #5b94f0 2px; + border-style: solid; + border-radius: 10px; + background-color: rgba(112, 196, 255, .3); + } + .head-content { + position: absolute; + left: 15px; } .teacher-container { position: absolute; width: 300px; - left: 205px; + } + .members-container { + position: absolute; + left: 400px; + } + .quests-container { + position: absolute; + left: 800px; + background: rgba(0, 0, 0, .2); + border-radius: 24px; + border-style: solid; + border-color: rgba(0, 0, 0, .2); + } + .quest-content { + margin-left: 15px; + margin-top: 10px; + margin-right: 15px; + } + .quest { + color: #e05f3a; } #pro-pic { position: absolute; @@ -35,14 +66,13 @@ } .buttons-container { position: absolute; - width: 174px; - height: 500px; - top: 25px; - right: 25px; - border: solid #5b94f0 2px; - border-style: solid; - border-radius: 10px; - background-color: rgba(112, 196, 255, .3); + top: 425px; + right: 180px; + } + .button-content { + width: 127px; + margin-left: 25px; + margin-top: 10px; } #edit-btn { width: 127px; @@ -99,9 +129,9 @@ -

Teacher Dashboard

-
+
+

Teacher Dashboard

Welcome <%= user.name %>

Logout - - Report Bug + Logout + + Report Bug +
- - -
+ + +

-
- + // Students should't be on this page sooooo... + console.log("Students Shouldnt be here"); + - -
+ +
- - +
- + if (xp >= 800) { + // Construct and show Max XP Alert + message = name + ' has reached max XP!'; + createDiv('message', message); + } + } + // Catching url Parameters + window.onload = function () { + const queryString = window.location.search; + const urlParams = new URLSearchParams(queryString); + const successRate = urlParams.get('successRate') + if(successRate == 'addXP > 50') { + createDiv('error', "Can't add more than 50 XP") + } + if(successRate == 'addXP <= 0') { + createDiv('error', "XP Must be Greater than 0"); + } + if(parseInt(successRate) > 0 && parseInt(successRate) < 50 || parseInt(successRate) == 50) { + createDiv('message', "Gave " + successRate + "XP to a Student"); + } + if(successRate == 'createdQuest') { + createDiv('message', "Successfully Created Quest"); + } + } + + +

Studen Members

@@ -342,6 +427,15 @@
+
+ +
+

All Quests

+ <% quests.forEach(function (quest) { %> +

<%= quest.title %>

+ <% }) %> +
+