diff --git a/whatsapp-web.js/src/businessLogic/logic.js b/whatsapp-web.js/src/businessLogic/logic.js index 483cb10..28c0495 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,31 @@ module.exports.onCreate = (id, companyName) => { //add the website with the company name user.addWebsite(new Website(companyName)); +} +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.id =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.products.deleteData(id); +} + +module.exports.deleteProduct = (id,website) => { + return website.products.deleteData(id); } \ No newline at end of file diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index 4e569f3..157b1f4 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -44,20 +44,7 @@ const commands = [ ]; return message; } - // Create a product and select it. At this point all the product properties will be default including the id will be empty - selected_product = new Product(); - if(id==null) - { - // Setting up the product id if not given - id = Math.floor((Math.random() * 100000) + 1); - while(selected_website.products.isData(id))// This is will check if the generated id is already used - { - id = Math.floor((Math.random() * 100000) + 1); - } - } - selected_product.id =id; - // Add a product to the selected website. We will add the id based on the count of the products on the website - selected_website.addProduct(selected_product) + selected_product= logic.addProduct(id,selected_website); //mesage to be sent to the user let message = [ `The product has been created with the id : ` + selected_product.id , @@ -83,9 +70,8 @@ const commands = [ ]; return message; } - // Find the product with the given id - selected_product = selected_website.products.deleteData(id); - if(selected_product == false) + + if(logic.deleteProduct == false) { let message = [ `No product with the given id` @@ -96,7 +82,6 @@ const commands = [ let message = [ `The product has been deleted with the id : ` + id ]; - selected_product =null; return message; } }), @@ -120,11 +105,7 @@ const commands = [ selected_product = selected_website.products.getData(id); if(selected_product == null) { - selected_product = new Product(); - selected_product.id =id; - // Add a product to the selected website. We will add the id based on the count of the products on the website - selected_website.addProduct(selected_product) - //mesage to be sent to the user + logic.addProduct(id,selected_website); 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 `