forked from Georeactor/city-namer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.js
99 lines (89 loc) · 2.43 KB
/
project.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
const User = require('./models/user');
const Project = require('./models/project');
const endlangs = {
'en': 'English',
'fr': 'French',
'es': 'Spanish',
'ne': 'Nepali',
'ht': 'Haitian Creole',
'zh': 'Chinese'
};
function projectSetup(app, csrfProtection) {
app.get('/projects', csrfProtection, (req, res) => {
var query = Project.find({}).sort('-saved');
var readLanguages = (req.query.readLanguages || '').split(',');
var writeLanguages = (req.query.writeLanguages || '').split(',');
if ((!readLanguages && !writeLanguages) && req.user) {
readLanguages = req.user.readLanguages;
writeLanguages = req.user.writeLanguages;
}
/*
if (readLanguages) {
query = query.find({ fromLanguages: { $in: readLanguages } });
}
if (writeLanguages) {
query = query.find({ toLanguage: { $in: writeLanguages } });
}
*/
query.exec( (err, projects) => {
if (err) {
return res.json(err);
}
var rows = [];
while (projects.length) {
rows.push(projects.splice(0, 3));
}
res.render('projects', {
user: req.user,
rows: rows
});
});
});
app.get('/projects/new', csrfProtection, (req, res) => {
if (!req.user) {
return res.redirect('/login');
}
res.render('new-project', {
csrfToken: req.csrfToken()
});
});
app.post('/projects/new', csrfProtection, (req, res) => {
if (!req.user) {
return res.redirect('/login');
}
var p = new Project({
founding_user_id: req.user._id,
founding_user_osm_id: req.user.osm_id,
fromLanguages: req.body.fromLanguages,
toLanguage: req.body.toLanguage,
directions: req.body.directions,
saved: new Date(),
lat: req.body.lat,
lng: req.body.lng,
zoom: req.body.zoom
});
p.save((err) => {
if (err) {
return res.json(err);
}
res.redirect('/projects/' + p._id);
});
});
app.get('/projects/:id', csrfProtection, (req, res) => {
Project.findById(req.params.id, (err, project) => {
if (err) {
return res.json(err);
}
res.render('naming', {
lat: project.lat,
lng: project.lng,
zoom: project.zoom,
fromLanguages: project.fromLanguages,
toLanguage: project.toLanguage,
endlang: endlangs[project.toLanguage],
csrfToken: req.csrfToken()
});
});
});
}
module.exports = projectSetup;