-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfreebiedb.js
152 lines (143 loc) · 5.61 KB
/
freebiedb.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
var mongo = require('mongodb');
var server = new mongo.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongo.Db('freebiedb', server, {safe: true});
// TODO: PAGINATION
db.open(function(err, db) {
if (! err) {
console.log("Connected to 'freebiedb' database");
db.collection('freebies', {safe: true}, function(err, collection) {
collection.count(function (err, count) {
if (! err && count === 0) {
console.log("The 'freebies' collection is empty. Creating it with sample data...");
populateFreebies();
}
});
});
}
});
// See: http://expressjs.com/api.html
// req.params can only contain specific IDs assigned by MongoDB for update and delete.
// req.body will always be the parameters for search, add, and update.
exports.findAll = function(req, res) {
var size = 20;
console.log('Retrieving all freebies');
db.collection('freebies', function(err, collection) {
collection.find().toArray(function(err, items) {
if (req.query.hasOwnProperty('page')) {
var start = parseInt(req.query.page) * size;
res.send(items.slice(start, start + size));
} else {
res.send(items);
}
});
});
};
exports.search = function(req, res) {
var urlParams = req.query;
var mongoParams; // Ugh is there a way to make this less messy? I hate javascript >_<
if (urlParams.hasOwnProperty('category') && urlParams.hasOwnProperty('type')) {
orParams = new Array();
if (urlParams.category instanceof Array) {
orParams[0] = { 'item.category': { $in: urlParams.category }};
} else {
orParams[0] = { 'item.category': urlParams.category };
}
if (urlParams.type instanceof Array) {
orParams[1] = { 'item.type': { $in: urlParams.type }};
} else {
orParams[1] = { 'item.type': urlParams.type };
}
mongoParams = { $or: orParams };
} else if (urlParams.hasOwnProperty('category')) {
if (urlParams.category instanceof Array) {
mongoParams = { 'item.category': { $in: urlParams.category }};
} else {
mongoParams = { 'item.category': urlParams.category };
}
} else if (urlParams.hasOwnProperty('type')) {
if (urlParams.type instanceof Array) {
mongoParams = { 'item.type': { $in: urlParams.type }};
} else {
mongoParams = { 'item.type': urlParams.type };
}
} else {
res.send({'error': 'No search parameters detected. Is it empty or malformed?'});
return;
}
db.collection('freebies', function(err, collection) {
collection.find(mongoParams).toArray(function(err, items) {
res.send(items);
});
});
};
exports.addFreebie = function(req, res) {
var freebie = req.body;
console.log('Adding freebie: ' + JSON.stringify(freebie));
db.collection('freebies', function(err, collection) {
collection.insert(freebie, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred'});
} else {
console.log('Success: ' + JSON.stringify(result[0]));
res.send(result[0]);
}
});
});
}
exports.upsertFreebie = function(req, res) {
var freebie = req.body;
console.log('Upserting freebie');
console.log(JSON.stringify(freebie));
db.collection('freebies', function(err, collection) {
collection.update({'urls.link': freebie['urls']['link']}, freebie,
{upsert: true, safe: true}, function(err, result) {
if (err) {
console.log('Error upserting freebie: ' + err);
res.send({'error':'An error has occurred'});
} else {
res.send(freebie);
}
});
});
}
exports.deleteFreebie = function(req, res) {
var id = req.params.id;
console.log('Deleting freebie: ' + id);
db.collection('freebies', function(err, collection) {
collection.remove({'_id':new mongo.BSONPure.ObjectID(id)}, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred - ' + err});
} else {
console.log('' + result + ' document(s) deleted');
res.send(req.params.id);
}
});
});
}
/*--------------------------------------------------------------------------------------------------------------------*/
// Populate database with sample data -- Only used once: the first time the application is started.
// You'd typically not find this code in a real-life app, since the database would already exist.
var populateFreebies = function() {
var freebies = [
{
urls: {link: 'http://pantry.twiningsusa.com/', img: 'http://www.mysavings.com/img/link/large/23457.jpg'},
item: {category: 'Food', type: 'tea'},
name: 'Twinings Tea',
brand: 'Twinings'
},
{
urls: {link: 'http://www.isatoritech.com/freeeatsmartbar/default.aspx', img: 'http://www.mysavings.com/img/link/large/26726.jpg'},
item: {category: 'Food', type: 'protein bar'},
name: 'iSatori Protein Bar',
brand: 'iSatori',
restrictions: 'Printable GNC coupon.'
}
];
db.collection('freebies', function(err, collection) {
collection.insert(freebies, {safe: true}, function(err, result) {
if (err) {
console.log(err);
}
});
});
};