-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
198 lines (183 loc) · 6.47 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
193
194
195
196
197
198
"use strict";
// Routes, with inline controllers for each route.
const express = require("express");
const router = express.Router();
const Project = require("./models").Project;
const strftime = require("strftime");
const bodyParser = require("body-parser");
const moment = require('moment-timezone');
// Example endpoint
router.get("/create-test-project", (req, res) => {
const project = new Project({
title: "I am a test project"
});
project.save(err => {
if (err) {
return res.status(500).json(err);
}
return res.send("Success: created a Project object in MongoDb");
});
});
// Part 1: View all projects
// Implement the GET / endpoint.
router.get("/", (req, res) => {
if (req.query.sort) {
const sortObject = {};
sortObject[req.query.sort] = 1;
Project.find().sort(sortObject).exec((err, array) => {
array.forEach(item => {
item.newStart = moment(item.start).format('MMMM Do, YYYY');
item.newEnd = moment(item.end).format('MMMM Do, YYYY');
})
res.render('index', {items: array});
});
}else {
Project.find((err, array) => {
array.forEach(item => {
item.newStart = moment(item.start).format('MMMM Do, YYYY');
item.newEnd = moment(item.end).format('MMMM Do, YYYY');
})
res.render('index', {items: array});
});
}
});
router.get("/funded", (req, res) => {
if (req.query.sort) {
const sortObject = {};
sortObject[req.query.sort] = 1;
Project.find({percent: 100}).sort(sortObject).exec((err, array) => {
array.forEach(item => {
item.newStart = moment(item.start).format('MMMM Do, YYYY');
item.newEnd = moment(item.end).format('MMMM Do, YYYY');
})
res.render('funded', {items: array});
});
}else {
Project.find({percent: 100}).then(array => {
array.forEach(item => {
item.newStart = moment(item.start).format('MMMM Do, YYYY');
item.newEnd = moment(item.end).format('MMMM Do, YYYY');
})
res.render('funded', {items: array});
});
}
});
router.get("/unfunded", (req, res) => {
if (req.query.sort) {
const sortObject = {};
sortObject[req.query.sort] = 1;
Project.find({percent: { $lt: 100}}).sort(sortObject).exec((err, array) => {
array.forEach(item => {
item.newStart = moment(item.start).format('MMMM Do, YYYY');
item.newEnd = moment(item.end).format('MMMM Do, YYYY');
})
res.render('unfunded', {items: array});
});
}else {
Project.find({percent: { $lt: 100}}).then(array => {
array.forEach(item => {
item.newStart = moment(item.start).format('MMMM Do, YYYY');
item.newEnd = moment(item.end).format('MMMM Do, YYYY');
})
res.render('unfunded', {items: array});
});
}
});
// Part 2: Create project
// Implement the GET /new endpoint
router.get("/new", (req, res) => {
res.render('new');
// YOUR CODE HERE
});
// Part 2: Create project
// Implement the POST /new endpoint
router.post("/new", (req, res) => {
const project = new Project({
title: req.body.title,
goal: req.body.goal,
description: req.body.description,
start: moment.tz(req.body.start, "America/New_York"),
end: moment.tz(req.body.end, "America/New_York"),
category: req.body.category,
total_contributions: 0
});
project.save(err => {
if (err) {
res.locals.errors = err.errors;
res.locals.project = project;
res.locals.startDate = moment(project.start).utc().format("YYYY-MM-DD");
res.locals.endDate = moment(project.end).utc().format("YYYY-MM-DD");
return res.render('new.hbs');
}
return res.redirect('/');
});
});
// Part 3: View single project
// Implement the GET /project/:projectid endpoint
router.get("/project/:projectid", (req, res) => {
Project.findById(req.params.projectid, (err, project) => {
const formatStart = moment(project.start).format('MMMM Do, YYYY');
const formatEnd = moment(project.end).format('MMMM Do, YYYY');
project.percent = Math.round(project.total_contributions / project.goal * 100);
res.render('project.hbs', {project: project, startDate: formatStart, endDate: formatEnd});
});
});
// Part 4: Contribute to a project
// Implement the GET /project/:projectid endpoint
router.post("/project/:projectid", (req, res) => {
Project.findById(req.params.projectid, (err, project) => {
const contribution = {
name: req.body.name,
amount: req.body.amount
}
project.total_contributions += Number(req.body.amount);
project.percent = Math.round(project.total_contributions / project.goal * 100);
if (project.percent > 100) {
project.percent = 100;
}
project.contributions.push(contribution);
project.save(err => {
if (err) {
res.locals.errors = err.errors;
res.locals.project = project;
}
});
const formatStart = moment(project.start).format('MMMM Do, YYYY');
const formatEnd = moment(project.end).format('MMMM Do, YYYY');
res.render('project.hbs', {project: project, startDate: formatStart, endDate: formatEnd});
});
});
// Part 6: Edit project
router.get("/project/:projectid/edit", (req, res) => {
Project.findById(req.params.projectid, (err, project) => {
const startDate = moment(project.start).utc().format("YYYY-MM-DD");
const endDate = moment(project.end).utc().format("YYYY-MM-DD");
res.render('editProject.hbs', {project: project, projectid: req.params.projectid, startDate: startDate, endDate: endDate});
});
});
// Create the GET /project/:projectid/edit endpoint
// Create the POST /project/:projectid/edit endpoint
router.post("/project/:projectid/edit", (req, res) => {
Project.findById(req.params.projectid, (err, project) => {
project.title = req.body.title,
project.goal = req.body.goal,
project.description = req.body.description,
project.start = moment.tz(req.body.start, "America/New_York"),
project.end = moment.tz(req.body.end, "America/New_York"),
project.category = req.body.category
project.percent = Math.round(project.total_contributions / project.goal * 100);
if (project.percent > 100) {
project.percent = 100;
}
project.save(err => {
if (err) {
res.locals.errors = err.errors;
res.locals.project = project;
}
});
const formatStart = moment(project.start).format('MMMM Do, YYYY');
const formatEnd = moment(project.end).format('MMMM Do, YYYY');
res.render('project.hbs', {project: project, startDate: formatStart, endDate: formatEnd});
});
});
module.exports = router;