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

Thoreddit solution #25

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
npm-debug.log
queries.js
backup.handlebars
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node app.js
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# assignment_thoreddit
A social news web application for Viking thunder Gods

Name: Thomas Hauge
83 changes: 83 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const express = require('express');
const app = express();

// Body Parser
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

// Session/Cookies
const cookieSession = require('cookie-session');
app.use(
cookieSession({
name: 'session',
keys: ['asdf1234']
})
);

app.use((req, res, next) => {
res.locals.session = req.session;
res.locals.currentUser = req.session.currentUser;
next();
});

// Method Override
const methodOverride = require('method-override');
const getPostSupport = require('express-method-override-get-post-support');
app.use(methodOverride(getPostSupport.callback, getPostSupport.options));

// Referrer
app.use((req, res, next) => {
req.session.backUrl = req.header('Referer') || '/';
next();
});

// Logging
const morgan = require('morgan');
app.use(morgan('tiny'));

// Mongoose
const mongoose = require('mongoose');
app.use((req, res, next) => {
if (mongoose.connection.readyState) {
next();
} else {
require('./mongo')().then(() => next());
}
});

// Routes
const sessionsRoutes = require('./controllers/sessions')(app);
const usersRoutes = require('./controllers/users');
const postsRoutes = require('./controllers/posts');
const commentsRoutes = require('./controllers/comments');

app.use('/', sessionsRoutes);
app.use('/users', usersRoutes);
app.use('/posts', postsRoutes);
app.use('/comments', commentsRoutes);

// Template Engine
const expressHandlebars = require('express-handlebars');

const hbs = expressHandlebars.create({
partialsDir: 'views/',
defaultLayout: 'application'
});

app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

// Server
const port = process.env.PORT || process.argv[2] || 3000;
const host = 'localhost';

let args;
process.env.NODE_ENV === 'production' ? (args = [port]) : (args = [port, host]);

args.push(() => {
console.log(`Listening: http://${host}:${port}`);
});

app.listen.apply(app, args);

module.exports = app;
13 changes: 13 additions & 0 deletions config/mongo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"development": {
"database": "assignment_thoreddit_development",
"host": "localhost"
},
"test": {
"database": "assignment_thoreddit_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGODB_URI"
}
}
220 changes: 220 additions & 0 deletions controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
const express = require('express');
const mongoose = require('mongoose');
const models = require('./../models');

const router = express.Router();
const User = mongoose.model('User');
const Post = mongoose.model('Post');
const Comment = mongoose.model('Comment');
const Vote = mongoose.model('Vote');

// New
router.get('/:id/new', (req, res) => {
let parentType;
Comment.findById(req.params.id).then(comment => {
if (comment) {
parentType = 'comment';
res.render('comments/new', { comment, parentType });
} else {
Post.findById(req.params.id).then(post => {
let comment = {
body: post.body,
author: post.author,
post: post.id
};
parentType = 'post';
res.render('comments/new', { comment, parentType });
});
}
});
});

// Create
router.post('/', (req, res) => {
let comment;

User.findOne({ username: req.session.currentUser.username }).then(user => {
if (req.body.comment.parent) {
comment = new Comment({
body: req.body.comment.body,
author: user,
post: req.body.comment.post,
parent: req.body.comment.parent,
score: 0
});
} else {
comment = new Comment({
body: req.body.comment.body,
author: user,
post: req.body.comment.post,
score: 0
});
}

comment
.save()
.then(comment => {
if (req.body.comment.parent) {
return Comment.findByIdAndUpdate(req.body.comment.parent, {
$push: { children: comment }
});
}
})
.then(() => {
res.redirect(`/posts/${req.body.comment.post}`);
})
.catch(e => res.status(500).send(e.stack));
});
});

// Upvote
router.get(
'/:id/up',
(req, res, next) => {
Vote.findOne({
user: req.session.currentUser.id,
comment: req.params.id
})
.then(vote => {
if (vote) {
if (vote.count === 1) {
Vote.findByIdAndUpdate(vote.id, {
user: req.session.currentUser.id,
comment: req.params.id,
count: 0
})
.then(() => {
return Comment.findByIdAndUpdate(req.params.id, {
$inc: { score: -1 }
});
})
.then(() => {
next();
});
} else if (vote.count === 0) {
Vote.findByIdAndUpdate(vote.id, {
user: req.session.currentUser.id,
comment: req.params.id,
count: 1
})
.then(() => {
return Comment.findByIdAndUpdate(req.params.id, {
$inc: { score: 1 }
});
})
.then(() => {
next();
});
} else if (vote.count === -1) {
Vote.findByIdAndUpdate(vote.id, {
user: req.session.currentUser.id,
comment: req.params.id,
count: 1
})
.then(() => {
return Comment.findByIdAndUpdate(req.params.id, {
$inc: { score: 2 }
});
})
.then(() => {
next();
});
}
} else {
const vote = new Vote({
user: req.session.currentUser.id,
comment: req.params.id,
count: 1
});

vote.save().then(() => {});
return Comment.findByIdAndUpdate(req.params.id, {
$inc: { score: 1 }
});
}
})
.then(() => {
next();
});
},
(req, res) => {
res.redirect('back');
}
);

// Downvote
router.get(
'/:id/down',
(req, res, next) => {
Vote.findOne({
user: req.session.currentUser.id,
comment: req.params.id
})
.then(vote => {
if (vote) {
if (vote.count === -1) {
Vote.findByIdAndUpdate(vote.id, {
user: req.session.currentUser.id,
comment: req.params.id,
count: 0
})
.then(() => {
return Comment.findByIdAndUpdate(req.params.id, {
$inc: { score: 1 }
});
})
.then(() => {
next();
});
} else if (vote.count === 0) {
Vote.findByIdAndUpdate(vote.id, {
user: req.session.currentUser.id,
comment: req.params.id,
count: -1
})
.then(() => {
return Comment.findByIdAndUpdate(req.params.id, {
$inc: { score: -1 }
});
})
.then(() => {
next();
});
} else if (vote.count === 1) {
Vote.findByIdAndUpdate(vote.id, {
user: req.session.currentUser.id,
comment: req.params.id,
count: -1
})
.then(() => {
return Comment.findByIdAndUpdate(req.params.id, {
$inc: { score: -2 }
});
})
.then(() => {
next();
});
}
} else {
const vote = new Vote({
user: req.session.currentUser.id,
comment: req.params.id,
count: -1
});

vote.save().then(() => {});
return Comment.findByIdAndUpdate(req.params.id, {
$inc: { score: -1 }
});
}
})
.then(() => {
next();
});
},
(req, res) => {
res.redirect('back');
}
);

module.exports = router;
Loading