blob: 05989f029b5465e1d807028001c0f496b7285037 (
plain)
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
|
const session = require('session')
const database = require('database')
const cart = new Map()
// Handles cart functionality
class Cart {
constructor() {}
Add(x) {
if (session().Exists(x) === true) {
if (this.Exists(x.cookie) === false) {
this.cart = new Map()
database('EStore').Query('SELECT * FROM Products WHERE id = ' + x.id).then(output => {
if (x.qty <= output[0].qty) {cart.set(x.cookie, this.cart.get(x.id, x.qty))} else {return 'Added'}
})
} else {
this.cart = cart.get(x.cookie)
if (!this.cart.get(x.id)) {
database('EStore').Query('SELECT * FROM Products WHERE id = ' + x.id).then(output => {
if (x.qty <= output[0].qty) {this.cart.set(x.id, x.qty)} else {return 'Added'}
})
} else {return 'Added'}
}
}
}
Exists(x) {return cart.has(x)}
static Set(x) {
this.cart = cart.get(x.cookie)
this.cart.forEach((value, key) => this.cart.delete(key))
}
static GetCart(x) {
this.cart = cart.get(x)
if (this.cart !== undefined) {if (this.cart.size > 0) return this.cart} else {return false}
}
static Remove(x) {if (session().Exists(x.cookie) === true) for (let i = 0; i < x.items.length; i++) cart.get(x.cookie.delete(x.items[i]))}
}
module.exports = () => {return new Cart()}
|