-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.js
344 lines (283 loc) · 10.8 KB
/
items.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
Copyright (c) 2008 - 2016 MongoDB, Inc. <http://mongodb.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var MongoClient = require('mongodb').MongoClient,
assert = require('assert');
function ItemDAO(database) {
"use strict";
this.db = database;
this.getCategories = function(callback) {
"use strict";
/*
* TODO-lab1A
*
* LAB #1A: Implement the getCategories() method.
*
* Write an aggregation query on the "item" collection to return the
* total number of items in each category. The documents in the array
* output by your aggregation should contain fields for "_id" and "num".
*
* HINT: Test your mongodb query in the shell first before implementing
* it in JavaScript.
*
* In addition to the categories created by your aggregation query,
* include a document for category "All" in the array of categories
* passed to the callback. The "All" category should contain the total
* number of items across all categories as its value for "num". The
* most efficient way to calculate this value is to iterate through
* the array of categories produced by your aggregation query, summing
* counts of items in each category.
*
* Ensure categories are organized in alphabetical order before passing
* to the callback.
*
*/
this.db.collection("item").aggregate([
{ $group: {
_id: "$category",
num: { $sum: 1 }
} },
{ $sort: { _id: 1 } }
])
.toArray(function(err, categories) {
assert.equal(null, err);
var category = {
_id: "All",
num: 0
};
for( var el in categories ) {
if( categories.hasOwnProperty( el ) ) {
category.num += parseFloat( categories[el].num );
}
}
categories.unshift(category);
// TODO-lab1A Replace all code above (in this method).
// TODO Include the following line in the appropriate
// place within your code to pass the categories array to the
// callback.
callback(categories);
});
}
this.getItems = function(category, page, itemsPerPage, callback) {
"use strict";
/*
* TODO-lab1B
*
* LAB #1B: Implement the getItems() method.
*
* Create a query on the "item" collection to select only the items
* that should be displayed for a particular page of a given category.
* The category is passed as a parameter to getItems().
*
* Use sort(), skip(), and limit() and the method parameters: page and
* itemsPerPage to identify the appropriate products to display on each
* page. Pass these items to the callback function.
*
* Sort items in ascending order based on the _id field. You must use
* this sort to answer the final project questions correctly.
*
* Note: Since "All" is not listed as the category for any items,
* you will need to query the "item" collection differently for "All"
* than you do for other categories.
*
*/
this.db.collection("item")
.find( (category !== "All") ? { category: category } : {} )
.sort( { _id: 1 } )
.skip( itemsPerPage * page )
.limit( itemsPerPage )
.toArray(function(err, pageItems) {
assert.equal(null, err);
// TODO-lab1B Replace all code above (in this method).
// TODO Include the following line in the appropriate
// place within your code to pass the items for the selected page
// to the callback.
callback(pageItems);
});
}
this.getNumItems = function(category, callback) {
"use strict";
var numItems = 0;
/*
* TODO-lab1C:
*
* LAB #1C: Implement the getNumItems method()
*
* Write a query that determines the number of items in a category
* and pass the count to the callback function. The count is used in
* the mongomart application for pagination. The category is passed
* as a parameter to this method.
*
* See the route handler for the root path (i.e. "/") for an example
* of a call to the getNumItems() method.
*
*/
this.db.collection("item")
.find( (category !== "All") ? { category: category } : {} )
.count((err, numItems) => {
assert.equal(null, err);
// TODO Include the following line in the appropriate
// place within your code to pass the count to the callback.
callback(numItems);
});
}
this.searchItems = function(query, page, itemsPerPage, callback) {
"use strict";
/*
* TODO-lab2A
*
* LAB #2A: Implement searchItems()
*
* Using the value of the query parameter passed to searchItems(),
* perform a text search against the "item" collection.
*
* Sort the results in ascending order based on the _id field.
*
* Select only the items that should be displayed for a particular
* page. For example, on the first page, only the first itemsPerPage
* matching the query should be displayed.
*
* Use limit() and skip() and the method parameters: page and
* itemsPerPage to select the appropriate matching products. Pass these
* items to the callback function.
*
* searchItems() depends on a text index. Before implementing
* this method, create a SINGLE text index on title, slogan, and
* description. You should simply do this in the mongo shell.
*
*/
this.db.collection("item")
.find( { $text: { $search: query } } )
.sort( { _id: 1 } )
.skip( itemsPerPage * page )
.limit( itemsPerPage )
.toArray(function(err, items) {
assert.equal(null, err);
// TODO-lab2A Replace all code above (in this method).
// TODO Include the following line in the appropriate
// place within your code to pass the items for the selected page
// of search results to the callback.
callback(items);
});
}
this.getNumSearchItems = function(query, callback) {
"use strict";
var numItems = 0;
/*
* TODO-lab2B
*
* LAB #2B: Using the value of the query parameter passed to this
* method, count the number of items in the "item" collection matching
* a text search. Pass the count to the callback function.
*
* getNumSearchItems() depends on the same text index as searchItems().
* Before implementing this method, ensure that you've already created
* a SINGLE text index on title, slogan, and description. You should
* simply do this in the mongo shell.
*/
this.db.collection("item")
.find( { $text: { $search: query } } )
.count((err, numItems) => {
assert.equal(null, err);
callback(numItems);
});
}
this.getItem = function(itemId, callback) {
"use strict";
/*
* TODO-lab3
*
* LAB #3: Implement the getItem() method.
*
* Using the itemId parameter, query the "item" collection by
* _id and pass the matching item to the callback function.
*
*/
this.db.collection("item")
.findOne( { _id: itemId },
(err, item) => {
assert.equal(null, err);
// TODO-lab3 Replace all code above (in this method).
// TODO Include the following line in the appropriate
// place within your code to pass the matching item
// to the callback.
callback(item);
});
}
this.getRelatedItems = function(callback) {
"use strict";
this.db.collection("item").find({})
.limit(4)
.toArray(function(err, relatedItems) {
assert.equal(null, err);
callback(relatedItems);
});
};
this.addReview = function(itemId, comment, name, stars, callback) {
"use strict";
/*
* TODO-lab4
*
* LAB #4: Implement addReview().
*
* Using the itemId parameter, update the appropriate document in the
* "item" collection with a new review. Reviews are stored as an
* array value for the key "reviews". Each review has the fields:
* "name", "comment", "stars", and "date".
*
*/
this.db.collection("item")
.findOne(
{ _id: itemId },
(err, doc) => {
assert.equal(null, err);
var reviewDoc = {
name: name,
comment: comment,
stars: stars,
date: Date.now()
}
// TODO replace the following two lines with your code that will
// update the document with a new review.
doc.reviews.push(reviewDoc);
this.db.collection("item")
.updateOne(
{ _id: itemId },
doc,
(err, doc) => {
assert.equal(null, err);
// TODO Include the following line in the appropriate
// place within your code to pass the updated doc to the
// callback.
callback(doc);
}
);
}
);
}
this.createDummyItem = function() {
"use strict";
var item = {
_id: 1,
title: "Gray Hooded Sweatshirt",
description: "The top hooded sweatshirt we offer",
slogan: "Made of 100% cotton",
stars: 0,
category: "Apparel",
img_url: "/img/products/hoodie.jpg",
price: 29.99,
reviews: []
};
return item;
}
}
module.exports.ItemDAO = ItemDAO;