diff options
Diffstat (limited to 'source/cart.js')
| -rw-r--r-- | source/cart.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/source/cart.js b/source/cart.js new file mode 100644 index 0000000..05989f0 --- /dev/null +++ b/source/cart.js @@ -0,0 +1,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()} + |
