-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
140 lines (125 loc) · 4.2 KB
/
server.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
var path = require('path');
var express = require('express');
var app = express();
var exphbs = require('express-handlebars');
var port = process.env.PORT || 3000;
var plantData = require('./plantData');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var mongoHost = "classmongo.engr.oregonstate.edu";
var mongoPort = process.env.MONGO_PORT || '27017';
var mongoUsername = "cs290_noonanj";
var mongoPassword = "cs290_noonanj";
var mongoDBName = "cs290_noonanj";
var mongoURL = "mongodb://" +
mongoUsername + ":" + mongoPassword + "@" + mongoHost + ":" + mongoPort + "/" + mongoDBName;
var mongoDB = null;
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
app.use(bodyParser.json());
app.use(express.static('public'));
app.get(['/', '/plants'], function (req, res, next) {
var plantCollection = mongoDB.collection('plants');
plantCollection.find({}).toArray(function (err, plantDocs) {
if (err) {
res.status(500).send("Error communicating with DB.");
}
res.status(200).render('plantPage', {
plants: plantDocs
});
});
});
app.get('/plants/:plant', function (req, res, next) {
var plant = req.params.plant.toLowerCase();
var plantCollection = mongoDB.collection('plants');
plantCollection.find({ name: plant }).toArray(function (err, plantDocs) {
if (err) {
res.status(500).send("Error communicating with DB.");
}
else if (plantDocs.length > 0) {
res.status(200).render('plantPage', plantDocs[0]);
}
else {
next();
}
});
});
app.post('/plants/addPlant', function (req, res, next) {
//var plant = req.params.plant.toLowerCase();
console.log("ADDING A NEW PLANT", req.body);
if (req.body && req.body.photoURL && req.body.about && req.body.name) {
var plantCollection = mongoDB.collection('plants');
plantCollection.insertOne({
"photoURL": req.body.photoURL,
"about": req.body.about,
"name": req.body.name
},
function (err, result) {
if (err) {
res.status(500).send("Error deleting plant");
}
else if (result.matchedCount > 0) {
res.status(200).send("Success");
}
else {
next();
}
}
);
}
});
app.post('/plants/deletePlant', function (req, res, next) {
//var plant = req.params.plant.toLowerCase();
if (req.body.name) {
var plantCollection = mongoDB.collection('plants');
plantCollection.deleteOne(
{ "name": req.body.name },
function (err, result) {
if (err) {
res.status(500).send("Error deleting plant");
}
else if (result.matchedCount > 0) {
res.status(200).send("Success");
}
else {
next();
}
}
);
}
});
app.post('/plants/:plant/renamePlant', function (req, res, next) {
var plant = req.params.plant;
console.log("RENAME: ", req.body.name);
console.log("OLDNAME: ", plant);
if (req.body.name) {
var plantCollection = mongoDB.collection('plants');
plantCollection.updateOne(
{ "name": plant },
{ "$set": { "name": req.body.name}}
// function (err, result) {
// if (err) {
// res.status(500).send("Error renaming plant");
// }
// else if (result.matchedCount > 0) {
// res.status(200).send("Success");
// }
// else {
// next();
// }
// }
);
}
});
app.get('*', function (req, res, next) {
res.status(404).render('404');
});
MongoClient.connect(mongoURL, function (err, client) {
if (err) {
throw err;
}
mongoDB = mongoDBDatabase = client.db(mongoDBName);
app.listen(port, function () {
console.log("== Server is listening on port", port);
});
});