-
Notifications
You must be signed in to change notification settings - Fork 3
/
items.js
186 lines (149 loc) · 5.26 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
var MongoClient = require('mongodb').MongoClient,
assert = require('assert');
function ItemDAO(database) {
"use strict";
this.db = database;
this.getCategories = function(callback) {
"use strict";
/* An aggregation query on the "item" collection to return the
total number of items in each category.
*/
// Run a MongoDB query to group the category and provide a
// sum of the category.
var categories = [];
database.collection("item").aggregate( [ {
$group: {
_id: "$category",
num: { $sum: 1 }
}},
{$sort: {_id: 1 }}]).toArray(function(err, data){
categories.push(...data);
var sum = categories.reduce(function(acc, val){
return acc + val["num"]
}, 0);
// Create a variable to store the value of Sum and _id
var allcategory = {
_id: "All",
num: sum
}
// Push to front of categories array and callback()
categories.unshift(allcategory)
callback(categories);
})
}
this.getItems = function(category, page, itemsPerPage, callback) {
"use strict";
/* A query on the "item" collection to select only the items
that should be displayed for a particular page of a given category.
*/
// Query the Database for the particular Category
// Using the Conditional Ternary Operator
var query = category== 'All' ? {} : {category: category}
var cursor = database.collection("item").find(query);
cursor.sort({_id: 1});
cursor.limit(itemsPerPage);
cursor.skip(itemsPerPage * page )
// Convert Our Query Results to Array.
cursor.toArray(function(err, data) {
// Declare pageItems As an Empty Array for Our Callback
var pageItems = [];
pageItems.push(...data)
// Return the Callback Function
callback(pageItems)
})
}
this.getNumItems = function(category, callback) {
"use strict";
/* A query that determines the number of items in a category
and pass the count to the callback function.
*/
// Get the Query for Category From DB. Using Ternary Operator
var query = category== 'All' ? {} : {category: category}
var cursor = database.collection("item").find(query);
cursor.toArray(function(err, data){
// Convert the Cursor Value to Array to Findout the Length
var numItems = data.length
// callback the numItems
callback(numItems)
})
}
this.searchItems = function(query, page, itemsPerPage, callback) {
"use strict";
// Text Search on MongoDB for the Variable Value of Query
var cursor = database.collection("item").find({
$text: {
$search: query
}
});
// Convert our results to Array Values and Push to Items
cursor.toArray(function(err, data) {
var items = [];
items.push(...data);
callback(items);
})
}
this.getNumSearchItems = function(query, callback) {
"use strict";
var numItems = 0;
// Get the Text Search Results from MongoDB and Convert to Array
var cursor = database.collection("item").find({
$text:{
$search: query
}});
cursor.toArray(function(err, data){
numItems = data.length;
// Callback the Value of the Array.length
callback(numItems);
})
}
this.getItem = function(itemId, callback) {
"use strict";
/* Implement the getItem() method. */
var cursor = database.collection("item").find({
_id: itemId
});
cursor.toArray(function(err, item) {
callback(item[0]);
})
}
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";
/* Implement addReview(). */
var reviewDoc = {
name: name,
comment: comment,
stars: stars,
date: Date.now()
}
database.collection("item").update({ _id:itemId }, {$push: {reviews:reviewDoc}});
var cursor = database.collection("item").find({_id:itemId},{reviews:{$elemMatch:{}}})
cursor.toArray(function(error, data){
var doc = [];
doc.push(...data)
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.00,
reviews: []
};
return item;
}
}
module.exports.ItemDAO = ItemDAO;