-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
143 lines (116 loc) · 2.96 KB
/
server.js
File metadata and controls
143 lines (116 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var express = require('express');
var bodyParser = require('body-parser');
var handlebars = require('express-handlebars');
var sequelize = require('sequelize');
var Posts = require('./models')['Posts'];
var Categories = require('./models')['Categories'];
var Comments = require('./models')['Comments'];
var models = require('./models');
var sequelizeConnection = models.sequelize
// We run this query so that we can drop our tables even though they have foreign keys
sequelizeConnection.query('SET FOREIGN_KEY_CHECKS = 0')
// make our tables
// note: force:true drops the table if it already exists
.then(function(){
return sequelizeConnection.sync({})
}).then(function() {
Categories.findAll({}).then(function(results) {
console.log(results);
app.locals.categories = results;
});
})
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({
extended: false
}));
app.engine('handlebars', handlebars({
defaultLayout: 'main'
}));
app.set('view engine', 'handlebars');
//home page
app.get('/', function(req, res) {
Posts.findAll({
order: 'score DESC'
}).then(function(result) {
console.log(result);
return res.render('index', {
posts: result
});
});
});
app.get('/categories/:category', function(req, res) {
var categoryName = req.params.category;
Categories.findAll({
where: {
title: categoryName
},
include: [{model: models.Posts, required: true}]
}).then(function(result) {
console.log('rending category');
return res.render('categories', {
categoryName: categoryName,
category: result
});
});
});
//form page
app.get('/new-post', function(req, res) {
res.render('new');
});
app.post('/:postId/comment', function(req, res) {
var body = req.body;
console.log('body', body);
var comment = body.comment;
Comments.create({
comment: comment,
PostId: parseInt(req.params.postId, 10)
}).then(function(data) {
res.redirect('/posts/' + req.params.postId);
});
});
app.post('/new-post', function(req, res) {
var body = req.body;
//create the post in the database
Posts.create({
title: body.title,
url: body.url,
image: body.image,
score: 0,
CategoryId: parseInt(body.category, 10),
description: body.description
}).then(function(data) {
console.log('data', data);
//redirect to the posts/:id page
res.redirect('/posts/' + data.dataValues.id);
});
});
app.get('/posts/:id', function(req, res) {
var id = req.params.id;
Posts.findOne({
where: {
id: id
},
include: [models.Comments]
}).then(function(post) {
console.log('post', post);
res.render('post', {
post: post
});
});
});
app.post('/posts/:id/:vote', function(req, res) {
var operation = '+';
if (req.params.vote === 'downvote') {
operation = '-';
}
Posts.update({
score: sequelize.literal('score ' + operation + ' 1')},
{where: {id: req.params.id}}).then(function(post) {
res.end();
});
});
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log('connected to', port);
});