Skip to content

Commit

Permalink
refactor + mode
Browse files Browse the repository at this point in the history
  • Loading branch information
laxyapahuja committed Sep 1, 2020
1 parent 9fe36da commit 329b5bd
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 350 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
.vscode
10 changes: 10 additions & 0 deletions config/constructiveCriticismAlgo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const emotional = require('emotional')

function consCriticism(sentence) {
emotional.load(function () {
emotional.get("sentence")
emotional.positive("sentence")
});
}

module.exports = consCriticism;
9 changes: 4 additions & 5 deletions models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ const PostSchema = new mongoose.Schema({
type: Number,
required: false
},
value: {
type: Number,
required: false,
default: 0
},
reviewers_usernames: {
type: Array,
required: false
Expand All @@ -46,6 +41,10 @@ const PostSchema = new mongoose.Schema({
type: Date,
default: Date.now
},
recommendations: {
type: Object,
required: false
}
})

const Post = mongoose.model('Post', PostSchema);
Expand Down
84 changes: 84 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
"connect-flash": "^0.1.1",
"dotenv": "^8.2.0",
"ejs": "^3.1.3",
"emotional": "0.0.3",
"express": "^4.17.1",
"express-ejs-layouts": "^2.5.0",
"express-session": "^1.17.1",
"mongoose": "^5.10.0",
"nodemailer": "^6.4.11",
"passport": "^0.4.1",
"passport-local": "^1.0.0"
"passport-local": "^1.0.0",
"stats-lite": "^2.2.0"
},
"devDependencies": {
"nodemon": "^2.0.4"
Expand Down
Empty file added public/css/index.css
Empty file.
23 changes: 13 additions & 10 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const bcrypt = require('bcryptjs')
const passport = require('passport')
const { ensureAuthenticated } = require('../config/auth');
const nodemailer = require('nodemailer')
const consCriticism = require('../config/constructiveCriticismAlgo')
const stats = require('stats-lite')

const transporter = nodemailer.createTransport({
service: 'gmail',
Expand Down Expand Up @@ -87,9 +89,9 @@ router.get('/dashboard', ensureAuthenticated, (req, res) => {



router.get('/login', (req, res) => res.render('login'));
router.get('/login', (req, res) => res.render('login', {layout: false}));

router.get('/register', (req, res) => res.render('register'));
router.get('/register', (req, res) => res.render('register', {layout: false}));

router.post('/register', (req, res) => {
const { username, email, password, name, age, gender } = req.body;
Expand Down Expand Up @@ -282,17 +284,14 @@ router.post('/post', ensureAuthenticated, (req, res) => {
})

router.post('/rate', ensureAuthenticated, (req, res) => {
const { post_id, value } = req.body
const { post_id, rating } = req.body
Post.findById(post_id, function(err, post) {
post.reviewers = post.reviewers + 1
const n_reviewers = post.reviewers
const old_value = post.value
post.value = Number(old_value) + Number(value)
post.reviewers ++
post.save(function (err) {
if(err) {
console.log(err)
}
Post.findByIdAndUpdate(post_id, { $push: { reviews: {'username': req.user.username, 'value': value } } }, function(err, success) {
Post.findByIdAndUpdate(post_id, { $push: { reviews: {'username': req.user.username, 'rating': rating } } }, function(err, success) {
if(err) {
console.log(err)
}
Expand Down Expand Up @@ -347,11 +346,15 @@ router.get('/profile', ensureAuthenticated, (req, res) => {
router.post('/results', ensureAuthenticated, (req, res) => {
const { post_id } = req.body;
Post.findById(post_id, function(err, post) {
const avg_value = Number(post.value)/Number(post.reviewers)
const reviews = post.reviews;
let reviews_array = [];
reviews.forEach((element) => {
reviews_array.push(Number(element['value']))
})
res.render('results', {
name: req.user.username,
post: post,
avg_value: Math.trunc(avg_value)
rating: stats.mode(reviews_array)
})
})
})
Expand Down
7 changes: 0 additions & 7 deletions views/dashboard.ejs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lexend Deca">
<link rel="stylesheet" href="/css/dashboard.css">
<nav class="navbar">
<img class="logo" src="https://cdn.discordapp.com/attachments/745141823210782720/749743996519317594/Group_65.png">
<a class="nav-item" href="/dashboard">Dashboard</a>
<a class="nav-item" href="/leaderboard">Leaderboard</a>
<a class="nav-item" href="/profile">Profile</a>
<a href="/post"><button>Post</button></a>
</nav>
<div class="card-grid">
<% if(webd_posts.length == 0) { %>
<% } else { %>
Expand Down
9 changes: 9 additions & 0 deletions views/layout.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<html>
<body>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lexend Deca">
<link rel="stylesheet" href="/css/post.css">
<nav class="navbar">
<img class="logo" src="https://cdn.discordapp.com/attachments/745141823210782720/749743996519317594/Group_65.png">
<a class="nav-item" href="/dashboard">Dashboard</a>
<a class="nav-item" href="/leaderboard">Leaderboard</a>
<a class="nav-item" href="/profile">Profile</a>
<a href="/post"><button>Post</button></a>
</nav>
<%- body %>
</body>
</html>
7 changes: 0 additions & 7 deletions views/leaderboard.ejs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lexend Deca">
<link rel="stylesheet" href="/css/leaderboard.css">
<nav class="navbar">
<img class="logo" src="https://cdn.discordapp.com/attachments/745141823210782720/749743996519317594/Group_65.png">
<a class="nav-item" href="/dashboard">Dashboard</a>
<a class="nav-item" href="/leaderboard">Leaderboard</a>
<a class="nav-item" href="/profile">Profile</a>
<a href="/post"><button>Post</button></a>
</nav>
<h1 class="header">Leaderboard</h1>
<table>
<tr>
Expand Down
Loading

0 comments on commit 329b5bd

Please sign in to comment.