Skip to content

Commit e2327fb

Browse files
committed
doc (examples) refactor shopping-cart example
1 parent bdf4c06 commit e2327fb

File tree

2 files changed

+73
-65
lines changed

2 files changed

+73
-65
lines changed

examples/models/shopping-cart.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* global Backendless */
2+
3+
'use strict';
4+
5+
class ShoppingCart {
6+
constructor(opts) {
7+
opts = opts || {};
8+
9+
this.name = opts.name;
10+
this.items = opts.items || [];
11+
this.___class = ShoppingCart.name;
12+
}
13+
14+
addItem(item) {
15+
item.objectId = null;
16+
17+
this.items.push(item);
18+
}
19+
20+
deleteItem(product) {
21+
const idx = this.items.findIndex(item => item.product === product);
22+
23+
if (idx === -1) {
24+
throw new Error(`No ${product} in cart`);
25+
}
26+
27+
this.items.splice(idx, 1);
28+
29+
return this;
30+
}
31+
32+
setQuantity(product, quantity) {
33+
const productItem = this.items.find(item => item.product === product);
34+
35+
if (!productItem) {
36+
throw new Error(`No [${product}] in cart`);
37+
}
38+
39+
productItem.quantity = quantity;
40+
41+
return this;
42+
}
43+
44+
getItems() {
45+
return this.items;
46+
}
47+
48+
destroy() {
49+
return Backendless.Cache.remove(this.name, this);
50+
}
51+
52+
save() {
53+
return Backendless.Cache.put(this.name, this);
54+
}
55+
56+
static get(name, mustExist) {
57+
Backendless.Cache.setObjectFactory(ShoppingCart.name, ShoppingCart);
58+
59+
return Backendless.Cache.get(name).then(cart => {
60+
if (!cart && mustExist) {
61+
throw new Error(`Shopping cart [${name}] does not exist`);
62+
}
63+
64+
return cart;
65+
});
66+
}
67+
}
68+
69+
Backendless.enablePromises();
70+
71+
module.exports = ShoppingCart;

examples/services/shopping-cart.js

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,8 @@
22

33
'use strict';
44

5-
const Order = require('../models/order');
6-
7-
class ShoppingCart {
8-
constructor(opts) {
9-
opts = opts || {};
10-
11-
this.name = opts.name;
12-
this.items = opts.items || [];
13-
this.___class = ShoppingCart.name;
14-
}
15-
16-
addItem(item) {
17-
item.objectId = null;
18-
19-
this.items.push(item);
20-
}
21-
22-
deleteItem(product) {
23-
const idx = this.items.findIndex(item => item.product === product);
24-
25-
if (idx === -1) {
26-
throw new Error(`No ${product} in cart`);
27-
}
28-
29-
this.items.splice(idx, 1);
30-
31-
return this;
32-
}
33-
34-
setQuantity(product, quantity) {
35-
const productItem = this.items.find(item => item.product === product);
36-
37-
if (!productItem) {
38-
throw new Error(`No [${product}] in cart`);
39-
}
40-
41-
productItem.quantity = quantity;
42-
43-
return this;
44-
}
45-
46-
getItems() {
47-
return this.items;
48-
}
49-
50-
destroy() {
51-
Backendless.Cache.remove(this.name, this);
52-
}
53-
54-
save() {
55-
Backendless.Cache.put(this.name, this);
56-
}
57-
58-
static get(name, mustExist) {
59-
Backendless.Cache.setObjectFactory(ShoppingCart.name, ShoppingCart);
60-
61-
return Backendless.Cache.get(name).then(cart => {
62-
if (!cart && mustExist) {
63-
throw new Error(`Shopping cart [${name}] does not exist`);
64-
}
65-
66-
return cart;
67-
});
68-
}
69-
}
5+
const Order = require('../models/order'),
6+
ShoppingCart = require('../models/shopping-cart');
707

718
class ShoppingCartService {
729

0 commit comments

Comments
 (0)