-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
157 lines (141 loc) · 3.95 KB
/
app.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
// load the modules needed for the API
var application_root = __dirname,
express = require('express'),
path = require('path'),
mongoose = require('mongoose'),
port = process.env.PORT || 4242;
// create the web server
var app = express();
// hooking up the database
mongoose.connect('mongodb://localhost/ecomm_database');
// config
app.configure(function () {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(application_root, "public"))); // sets up the public directory to use static files
app.use(express.errorHandler({ dumpExceptions: true, showStack: true})); // sets up the display of errors on the command line
});
// Schema
var Schema = mongoose.Schema;
// Product model
var Product = new Schema({
title: { type: String, required: true },
description: { type: String, required: true },
style: { type: String, unique: true },
modified: { type: Date, default: Date.now }
});
// use the model
var ProductModel = mongoose.model('Product', Product);
// Add the CRUD methods
// Read a list of products
app.get('/api/products', function (req, res) {
return ProductModel.find(function (err, products) {
if (!err) {
return res.send(products);
} else {
return console.log(err);
}
});
});
// Create a single product
app.post('/api/products', function (req, res) {
var product;
console.log("POST: ");
console.log(req.body);
product = new ProductModel({
title: req.body.title,
description: req.body.description,
style: req.body.style
});
product.save(function (err) {
if(!err) {
return console.log("created");
} else {
return console.log(err);
}
});
});
// Read a single product by ID
app.get('/api/products/:id', function (req, res) {
return ProductModel.findById(req.params.id, function (err, product) {
if (!err) {
return res.send(product);
} else {
return console.log(err);
}
});
});
// Update a single product by ID
app.put('/api/products/:id', function (req, res) {
return ProductModel.findById(req.params.id, function (err, product) {
product.title = req.body.title;
product.description = req.body.description;
product.style = req.body.style;
product.images = req.body.images;
return product.save(function (err) {
if (!err) {
console.log("updated");
} else {
console.log(err);
}
return res.send(product);
});
});
});
// bulk update
app.put('/api/products', function (req, res) {
var i, len = 0;
console.log("is Array req.body.products");
console.log(Array.isArray(req.body.products));
console.log("PUT: (products)");
console.log(req.body.products);
if (Array.isArray(req.body.products)) {
len = req.body.products.length;
}
for (i = 0; i < len; i++) {
console.log("Update product by id: ");
for (var id in req.body.products[i]) {
console.log(id);
}
ProductModel.update({ "_id": id}, req.body.products[i][id], function (err, numAffected) {
if (err) {
console.log("Error on update");
console.log(err);
} else {
console.log("updated num: " + numAffected);
}
});
}
return res.send(req.body.products);
});
// Delete a single product by ID
app.delete('/api/products/:id', function (req, res) {
return ProductModel.findById(req.params.id, function (err, product) {
return product.remove(function (err) {
if (!err) {
console.log("removed");
return res.send('');
} else {
console.log(err);
}
});
});
});
// bulk delete
app.delete('/api/products', function (req, res) {
ProductModel.remove(function (err) {
if (!err) {
console.log("All products have been removed");
return res.send('');
} else {
console.log(err);
}
});
});
app.get('/api', function (req, res) {
res.send('eCommerce API is running.');
});
app.listen(port, function() {
console.log("Listening on " + port);
});