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()}