diff --git a/whatsapp-web.js/src/businessLogic/logic.js b/whatsapp-web.js/src/businessLogic/logic.js index 483cb10..489aad5 100644 --- a/whatsapp-web.js/src/businessLogic/logic.js +++ b/whatsapp-web.js/src/businessLogic/logic.js @@ -1,5 +1,6 @@ const { Website } = require("../models/website"); const { data } = require("../models/data"); +const { Product } = require('../models/product'); module.exports.onWG = () => { console.log('hello world'); @@ -17,4 +18,27 @@ module.exports.onCreate = (id, companyName) => { //add the website with the company name user.addWebsite(new Website(companyName)); -} \ No newline at end of file +} +module.exports.addProduct = (id,website) => { + + if(id==null) + { + // Setting up the product id if not given + id = Math.floor((Math.random() * 100000) + 1); + while(website.products.isData(id))// This is will check if the generated id is already used + { + id = Math.floor((Math.random() * 100000) + 1); + } + } + + // Create a product and select it. At this point all the product properties will be default including the id will be empty + var product = new Product(); + product.setId(id); + // Add a product to the selected website. We will add the id based on the count of the products on the website + website.addProduct(product); + return product; +} +module.exports.deleteProduct = (id,website) => { + + return website.deleteProduct(id); +} diff --git a/whatsapp-web.js/src/models/entity.js b/whatsapp-web.js/src/models/entity.js index 8297cb3..0bcce8f 100644 --- a/whatsapp-web.js/src/models/entity.js +++ b/whatsapp-web.js/src/models/entity.js @@ -9,7 +9,7 @@ class Entity { addData(key ,value){ if(!this.isData(key)){ - this.data[key] = value; + this.data.set(key, value); return true; } return false; @@ -20,12 +20,13 @@ class Entity { * @param {string} key */ isData(key){ - return this.data[key] != undefined ? true : false; + return this.data.has(key) ? true : false; } deleteData(key){ if(this.isData(key)){ - return delete this.data[key]; + this.data.delete(key); + return true; } return false; } @@ -37,7 +38,7 @@ class Entity { getData(key){ if(this.isData(key)){ - return this.data[key]; + return this.data.get(key); } return null; } diff --git a/whatsapp-web.js/src/models/product.js b/whatsapp-web.js/src/models/product.js index 8a01ddb..ebbefca 100644 --- a/whatsapp-web.js/src/models/product.js +++ b/whatsapp-web.js/src/models/product.js @@ -8,6 +8,43 @@ class Product { this.cost = cost; this.image = image; } + + /** + * + * @param {Number} id + */ + setId(id){ + return this.id = id; + } + /** + * + * @param {String} name + */ + setName(name){ + return this.name = name; + } + /** + * + * @param {String} description + */ + setDesc(description){ + return this.desc = description; + } + /** + * + * @param {Number} cost + */ + setCost(cost){ + return this.cost = cost; + } + /** + * + * @param {String} image + */ + setImage(image){ + return this.image = image; + } + } module.exports.Product = Product; diff --git a/whatsapp-web.js/src/models/website.js b/whatsapp-web.js/src/models/website.js index 9584f76..86e0f35 100644 --- a/whatsapp-web.js/src/models/website.js +++ b/whatsapp-web.js/src/models/website.js @@ -37,6 +37,14 @@ class Website { return this.products.getData(productId); } + /** + * + * @returns {[]} + */ + getAllProduct(){ + return this.products.getAllData().values(); + } + /** * * @param {string} productId diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index e62c716..d75293c 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -1,6 +1,11 @@ const { Command } = require("../models/commands"); const logic = require("../businessLogic/logic"); +const { Website } = require("../models/website"); +const { Product } = require('../models/product'); + +var selected_product = null;// Will keep track of the selected product +var selected_website = null; // Selected website. /////////// //add the commands here @@ -23,7 +28,235 @@ const commands = [ return message; } }), + new Command({ + //User command + command: `wg product new `, + //function to be executed + callback : (input) => { + + if(selected_website==null) + { + let message = [ + `A website should be selected or created before creating a product ` + ]; + return message; + } + selected_product= logic.addProduct(input['id'],selected_website); + //mesage to be sent to the user + let message = [ + `The product has been created with the id : ` + selected_product.id , + `This product has been selected . \nNow you can make change to the product with the following commands : \n1. wg product info \n2. wg product name \n3. wg product cost \n4. wg product desc \n5. wg product image ` + ]; + + return message; + } + }), + new Command({ + //User command + command: `wg product delete `, + + //function to be executed + callback : (input) => { + + if(selected_website==null) + { + let message = [ + `A website should be selected or created before deleting a product ` + ]; + return message; + } + + if(logic.deleteProduct(input['id'],selected_website) == false) + { + let message = [ + `No product with the given id` + ]; + return message; + } + + let message = [ + `The product has been deleted with the id : ` + id + ]; + return message; + } + }), + new Command({ + //User command + command: `wg product select `, + + //function to be executed + callback : (input) => { + + if(selected_website==null) + { + let message = [ + `A website should be selected or created before selecting a product ` + ]; + return message; + } + // Find the product with the given id + selected_product = selected_website.getProduct(input['id']); + if(selected_product == null) + { + logic.addProduct(input['id'],selected_website); + selected_product = selected_website.getProduct(input['id']); + let message = [ + `There was no product with the give id. So a new product was created and selected with the id : ` + selected_product.id , + `Now you can make change to the product with the following commands : \n1. wg product info \n2. wg product name \n3. wg product cost \n4. wg product desc \n5. wg product image ` + ]; + + return message; + } + + let message = [ + `The product has been selected with the id : ` + selected_product.id , + 'Now you can make change to the product with the following commands : \n1. wg product info \n2. wg product name \n3. wg product cost \n4. wg product desc \n5. wg product image ' + + ]; + + return message; + } + }), + new Command({ + //User command + command: `wg product all`, + + //function to be executed + callback : () => { + + if(selected_website==null) + { + let message = [ + `A website should be selected or created ` + ]; + return message; + } + let message = [ + 'Your website has following products: ' + ]; + for(let p of selected_website.getAllProduct()) + { + let m = 'Product id: ' + p.id + '\nProduct Name : ' + p.name + '\nProduct description : '+ p.desc + '\nProduct Cost: ' + p.cost + '\nProduct Image : '+ p.image; + message.push(m); + } + + return message; + } + }), + new Command({ + //User command + command: `wg product info`, + + //function to be executed + callback : () => { + + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + let message = [ + 'Product id: ' + selected_product.id, + 'Product Name : ' + selected_product.name , + 'Product description : '+ selected_product.desc , + 'Product Cost: ' + selected_product.cost , + 'Product Image : '+ selected_product.image + ]; + + return message; + } + }), + new Command({ + //User command + command: `wg product name `, + + //function to be executed + callback : (input) => { + + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + selected_product.setName(input['product-name']); + let message = [ + 'The product name has been set to '+ selected_product.name + ]; + + return message; + } + }), + new Command({ + //User command + command: `wg product cost `, + + //function to be executed + callback : (input) => { + + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + selected_product.setCost(input['product-cost']); + let message = [ + 'The product cost has been set to '+ selected_product.cost + ]; + + return message; + } + }), + new Command({ + //User command + command: 'wg product desc ', + + //function to be executed + callback : (input) => { + + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + selected_product.setDesc(input['product-desc']); + let message = [ + 'The product description has been set to : '+ selected_product.desc + ]; + + return message; + } + }), + new Command({ + //User command + command: 'wg product image ', + + //function to be executed + callback : (input) => { + + if(selected_product == null) + { + let message = [ + `A product should be selected ` + ]; + return message; + } + selected_product.setImage(input['product-image']); + let message = [ + 'The product image has been set to : '+ selected_product.image + ]; + + return message; + } + }), ]; @@ -46,7 +279,7 @@ module.exports= onMessage = (message, client) => { if(command != null){ let input = {}; if(command.requireInput){ - input = command.getInput(); + input = command.getInput(message.body); } let messageToBeSent = command.callback(input); @@ -57,7 +290,7 @@ module.exports= onMessage = (message, client) => { } } -}; +}; diff --git a/whatsapp-web.js/test/test_command.js b/whatsapp-web.js/test/test_command.js index 405a04d..fff3df5 100644 --- a/whatsapp-web.js/test/test_command.js +++ b/whatsapp-web.js/test/test_command.js @@ -93,7 +93,7 @@ describe('Command', () => { it('Test : equals() with different message', () => { - let message = 'wg website p12'; + let message = 'wg website web'; let a = new Command({ command: "wg create ", diff --git a/whatsapp-web.js/test/test_entity.js b/whatsapp-web.js/test/test_entity.js index f42eb2e..56a3e22 100644 --- a/whatsapp-web.js/test/test_entity.js +++ b/whatsapp-web.js/test/test_entity.js @@ -52,4 +52,17 @@ describe('Entity', () => { expect(a.getData('gada')).to.null; }) + it('Test: getAllData', () => { + let a = new Entity(); + a.addData('hello world', new Website('hello world')); + a.addData('gada', new Website('gada')); + a.addData('pagol', new Website('pagol')); + + let websitegen = a.getAllData().values(); + for(let t of websitegen) + { + console.log(t.companyName); + } + }) + }) \ No newline at end of file