-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
192 lines (165 loc) · 5.26 KB
/
routes.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
let multiparty = require('multiparty')
let then = require('express-then')
let fs = require('fs')
let isLoggedIn = require('./middleware/isLoggedIn')
let User = require('./model/user')
let Post = require('./model/post')
let util = require('util')
let DataUri = require('datauri')
module.exports = (app) => {
let passport = app.passport
app.get('/', (req, res) => {
res.render('index.ejs')
})
app.get('/login', (req, res) => {
res.render('login.ejs', {message: req.flash('error')})
})
app.post('/login', passport.authenticate('local', {
successRedirect: '/profile',
failureRedirect: '/login',
failureFlash: true
}))
app.get('/signup', (req, res) => {
res.render('signup.ejs', {message: req.flash('error')})
})
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect: '/profile',
failureRedirect: '/signup',
failureFlash: true
}))
app.get('/profile', isLoggedIn, then(async(req, res) => {
let posts = await Post.promise.find({user_id: req.user._id})
//console.log("profile posts", posts)
let comments = []
for (let post of posts) {
if (post.comments && post.comments.length > 0) {
// take the last comment in the array as the latest comment
let comment = post.comments[post.comments.length-1]
comments.push({
content: comment.content.substr(0, 124),
username: comment.username,
created: comment.created,
postLink: "/post/" + post._id
})
}
}
res.render('profile.ejs', {
user: req.user,
posts: posts,
comments: comments,
message: req.flash('error')
})
}))
app.get('/logout', (req, res) => {
req.logout()
res.redirect('/')
})
app.get('/post/:postId?', isLoggedIn, then(async(req, res) => {
let postId = req.params.postId
if (!postId) {
res.render('post.ejs', {
post: {},
verb: 'Create'
})
return
}
let post = await Post.promise.findById(postId)
if (!post) res.status(404).send('Not found')
let dataUri = new DataUri()
let image
let imageData
if (post.image.data) {
image = dataUri.format('.' + post.image.contentType.split('/').pop(), post.image.data)
imageData = `data:${post.image.contentType};base64,${image.base64}`
//console.log(image)
}
res.render('post.ejs', {
post: post,
verb: 'Edit',
image: imageData
})
}))
app.post('/post/:postId?', isLoggedIn, then(async(req, res) => {
let postId = req.params.postId
let post
if (!postId) {
post = new Post()
} else {
post = await Post.promise.findById(postId)
if (!post) res.status(404).send('Not found')
}
let [{title: [title], content: [content]}, {image: [file]}] = await new multiparty.Form().promise.parse(req)
post.title = title
post.content = content
if (file.originalFilename !== '') {
post.image.data = await fs.promise.readFile(file.path)
post.image.contentType = file.headers['content-type']
}
//console.log(file, title, content, post)
if (!postId) {
// assign user id to the post
post.user_id = req.user._id
}
await post.save()
res.redirect('/blog/' + encodeURI(req.user.blogTitle))
}))
app.delete('/post/:postId', isLoggedIn, (req, res) => {
async ()=> {
let postId = req.params.postId
let post = await Post.promise.findById(postId)
if (post) await post.promise.remove()
res.end()
}().catch(e => console.log('err', e))
})
app.get('/blog/:blogTitle', (req, res) => {
async ()=> {
//console.log('blog title', req.params.blogTitle)
// first find the user by blogTitle
let user = await User.promise.findOne({blogTitle: req.params.blogTitle})
//console.log('blog user', user)
if (!user) res.status(404).send('Not found')
// get the posts by user id
let posts = await Post.promise.find({user_id: user._id})
//console.log('blog posts', posts)
let dataUri = new DataUri()
let image
for (let post of posts) {
if (post.image.data) {
image = dataUri.format('.' + post.image.contentType.split('/').pop(), post.image.data)
// console.log(image)
post.imageData = `data:${post.image.contentType};base64,${image.base64}`
}
}
res.render('blog.ejs', {
posts: posts,
message: req.flash('error')
})
}().catch(e => console.log('err', e))
})
app.post('/comment', isLoggedIn, then(async(req, res) => {
//console.log('comment req.body', req.body)
let post = await Post.promise.findById(req.body.postId)
if (post) {
post.comments.push({
content: req.body.comment,
username: req.user.username
})
await post.save()
}
res.end()
}))
app.post('/logincomment', passport.authenticate('local'), then(async(req, res) => {
//console.log('comment req.body', req.body)
// get the post document
let post = await Post.promise.findById(req.body.postId)
if (post) {
post.comments.push({
content: req.body.comment,
username: req.user.username
})
await post.save()
}
res.end()
}))
}