-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
180 lines (160 loc) · 4.39 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
//var MONGO_URL ='mongodb://amdevelop:[email protected]:21026/amdevelop';
// config
var config = require('config.json');
var Schema = mongoose.Schema; //Schema.ObjectId
var app = new (require('express'))();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var router = express.Router();
// Schemas
// Product Model
var Product = new Schema({
title: { type: String, unique: true },
description: { type: String, required: true },
modified: { type: Date, default: Date.now }
});
// validation
Product.path('title').validate(function (v) {
console.log("validate title");
console.log(v);
return v.length > 10 && v.length < 70;
});
Product.path('description').validate(function (v) {
console.log("validate description");
console.log(v);
return v.length > 10;
}, 'Product description should be more than 10 characters');
var ProductModel = mongoose.model('Product', Product);
// start router.js
router.get('/api', function (req, res) {
res.send('Products API is running');
});
// POST to CREATE
router.post('/api/products', function (req, res) {
var product;
// database
mongoose.connect(MONGO_URL);
//mongoose.connect(MONGO_URL);
console.log("POST: ");
console.log(req.body);
product = new ProductModel({
title: req.body.title,
description: req.body.description
});
product.save(function (err) {
mongoose.connection.close();
if (!err) {
return console.log("created");
} else {
return console.log(err);
}
});
return res.send(product);
});
// PUT to UPDATE
// Bulk update
router.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);
}
mongoose.connect(MONGO_URL);
ProductModel.update({ "_id": id }, req.body.products[i][id], function (err, numAffected) {
mongoose.connection.close();
if (err) {
console.log("Error on update");
console.log(err);
} else {
console.log("updated num: " + numAffected);
}
});
}
return res.send(req.body.products);
});
// Single update
router.put('/api/products/:id', function (req, res) {
mongoose.connect(MONGO_URL);
return ProductModel.findById(req.params.id, function (err, product) {
product.title = req.body.title;
product.description = req.body.description;
return product.save(function (err) {
mongoose.connection.close();
if (!err) {
console.log("updated");
} else {
console.log(err);
}
return res.send(product);
});
});
});
// GET to READ
// List products
router.get('/api/products', function (req, res) {
mongoose.connect(MONGO_URL);
return ProductModel.find(function (err, products) {
mongoose.connection.close();
if (!err) {
return res.send(products);
} else {
return console.log(err);
}
});
});
// Single product
router.get('/api/products/:id', function (req, res) {
mongoose.connect(MONGO_URL);
return ProductModel.findById(req.params.id, function (err, product) {
mongoose.connection.close();
if (!err) {
return res.send(product);
} else {
return console.log(err);
}
});
});
// DELETE to DESTROY
// Bulk destroy all products
router.delete('/api/products', function (req, res) {
ProductModel.remove(function (err) {
mongoose.connection.close();
if (!err) {
console.log("removed");
return res.send('');
} else {
console.log(err);
}
});
});
// remove a single product
router.delete('/api/products/:id', function (req, res) {
mongoose.connect(MONGO_URL);
return ProductModel.findById(req.params.id, function (err, product) {
return product.remove(function (err) {
mongoose.connection.close();
if (!err) {
console.log("removed");
return res.send('');
} else {
console.log(err);
}
});
});
});
app.use('/', router);
// launch server
app.listen(config.port);
// Start the server
console.log('Running on port ' + config.port);