-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
180 lines (147 loc) · 6.21 KB
/
cart.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
/*
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,
var assert = require('assert');
function CartDAO(database) {
'use strict';
this.db = database;
this.getCart = function(userId, callback) {
'use strict';
this.db.collection('cart')
.find({ userId: userId })
.limit(1)
.next((err, doc) => {
assert.equal(err, null);
callback(doc);
});
};
this.itemInCart = function(userId, itemId, callback) {
'use strict';
/*
*
* TODO-lab6
*
* LAB: #6
*
* Write a query that will determine whether or not the cart associated
* with the userId contains an item identified by itemId. If the cart
* does contain the item, pass the item to the callback. If it does not,
* pass the value null to the callback.
*
* NOTE: You should pass only the matching item to the callback. Do not
* pass an array of one or more items or the entire cart.
*
* SUGGESTION: While it is not necessary, you might find it easier to
* use the $ operator in a projection document in your call to find() as
* a means of selecting the matching item. Again, take care to pass only
* the matching item (not an array) to the callback. See:
* https://docs.mongodb.org/manual/reference/operator/projection/positional/
*
* As context for this method to better understand its purpose, look at
* how cart.itemInCart is used in the mongomart.js app.
*
*/
//callback(null);
var projection = { "items.$": 1, _id: 0 };
this.db.collection('cart')
.find({ userId: userId, "items._id": { $eq: itemId } })
.project(projection)
.limit(1)
.next((err, doc) => {
assert.equal(err, null);
if (doc) {
callback(doc.items[0]);
} else {
callback(null);
}
});
};
/*
* This solution is provide as an example to you of several query
* language features that are valuable in update operations.
* This method adds the item document passed in the item parameter to the
* user's cart. Note that this solution works regardless of whether the
* cart already contains items or is empty. addItem will be called only
* if the cart does not already contain the item. The route handler:
* router.post("/user/:userId/cart/items/:itemId"...
* handles this. Please review how that method works to have a complete
* understanding of how addItem is used.
*
* NOTE: One may use either updateOne() or findOneAndUpdate() to
* write this method. We did not discuss findOneAndUpdate() in class,
* but it provides a very straightforward way of solving this problem.
* See the following for documentation:
* http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findOneAndUpdate
*
*/
this.addItem = function(userId, item, callback) {
"use strict";
// Will update the first document found matching the query document.
this.db.collection("cart").findOneAndUpdate(
// query for the cart with the userId passed as a parameter.
{userId: userId},
// update the user's cart by pushing an item onto the items array
{"$push": {items: item}},
// findOneAndUpdate() takes an options document as a parameter.
// Here we are specifying that the database should insert a cart
// if one doesn't already exist (i.e. "upsert: true") and that
// findOneAndUpdate() should pass the updated document to the
// callback function rather than the original document
// (i.e., "returnOriginal: false").
{
upsert: true,
returnOriginal: false
},
// Because we specified "returnOriginal: false", this callback
// will be passed the updated document as the value of result.
function(err, result) {
assert.equal(null, err);
// To get the actual document updated we need to access the
// value field of the result.
callback(result.value);
});
/*
Without all the comments this code looks written as follows.
this.db.collection("cart").findOneAndUpdate(
{userId: userId},
{"$push": {items: item}},
{
upsert: true,
returnOriginal: false
},
function(err, result) {
assert.equal(null, err);
callback(result.value);
});
*/
};
this.updateQuantity = (userId, itemId, quantity, callback) => {
'use strict';
// @todo: provide error handling
quantity = quantity < 0 ? 0 : quantity;
var updateOperation = quantity === 0 ? { $pull: { items: { _id: itemId } } } : { $set: { "items.$.quantity": quantity } };
this.db.collection('cart')
.findOneAndUpdate(
{ userId: userId, "items._id": { $eq: itemId } },
updateOperation,
{
upsert: true,
returnOriginal: false
},
(err, result) => {
assert.equal(err, null);
callback(result.value);
});
};
}
module.exports.CartDAO = CartDAO;