This commit is contained in:
Mohammed Ashab Uddin
2020-07-27 10:15:48 +04:00
4 changed files with 70 additions and 25 deletions

View File

@@ -33,12 +33,12 @@ module.exports.addProduct = (id,website) => {
// Create a product and select it. At this point all the product properties will be default including the id will be empty // 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(); var product = new Product();
product.id =id; 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 // 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); website.addProduct(product);
return product; return product;
} }
module.exports.deleteProduct = (id,website) => { module.exports.deleteProduct = (id,website) => {
return website.products.deleteData(id); return website.deleteProduct(id);
} }

View File

@@ -1,13 +1,50 @@
class Product { class Product {
constructor({ id = '', name = '', desc = '', cost = 0, image = null }) { constructor({ id = '', name = '', desc = '', cost = 0, image = null } = {}) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.desc = desc; this.desc = desc;
this.cost = cost; this.cost = cost;
this.image = image; 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; module.exports.Product = Product;

View File

@@ -37,6 +37,14 @@ class Website {
return this.products.getData(productId); return this.products.getData(productId);
} }
/**
*
* @returns {[]}
*/
getAllProduct(){
return this.products.getAllData().values();
}
/** /**
* *
* @param {string} productId * @param {string} productId

View File

@@ -5,7 +5,7 @@ const { Website } = require("../models/website");
const { Product } = require('../models/product'); const { Product } = require('../models/product');
var selected_product = null;// Will keep track of the selected product var selected_product = null;// Will keep track of the selected product
var selected_website = new Website; // Selected website. var selected_website = null; // Selected website.
/////////// ///////////
//add the commands here //add the commands here
@@ -42,7 +42,7 @@ const commands = [
]; ];
return message; return message;
} }
selected_product= logic.addProduct(input,selected_website); selected_product= logic.addProduct(input['id'],selected_website);
//mesage to be sent to the user //mesage to be sent to the user
let message = [ let message = [
`The product has been created with the id : ` + selected_product.id , `The product has been created with the id : ` + selected_product.id ,
@@ -54,7 +54,7 @@ const commands = [
}), }),
new Command({ new Command({
//User command //User command
command: `wg delete product <id>`, command: `wg product delete <id>`,
//function to be executed //function to be executed
callback : (input) => { callback : (input) => {
@@ -67,7 +67,7 @@ const commands = [
return message; return message;
} }
if(logic.deleteProduct(input,selected_website) == false) if(logic.deleteProduct(input['id'],selected_website) == false)
{ {
let message = [ let message = [
`No product with the given id` `No product with the given id`
@@ -83,7 +83,7 @@ const commands = [
}), }),
new Command({ new Command({
//User command //User command
command: `wg product <id>`, command: `wg select product <id>`,
//function to be executed //function to be executed
callback : (input) => { callback : (input) => {
@@ -96,7 +96,7 @@ const commands = [
return message; return message;
} }
// Find the product with the given id // Find the product with the given id
selected_product = selected_website.products.getData(input); selected_product = selected_website.getProduct(input['id']);
if(selected_product == null) if(selected_product == null)
{ {
logic.addProduct(id,selected_website); logic.addProduct(id,selected_website);
@@ -109,7 +109,7 @@ const commands = [
} }
let message = [ let message = [
`The product has been selected with the id : ` + id , `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 <product-name> \n3. wg product cost <product-cost> \n4. wg product desc <product-desc> \n5. wg product image <product-image>' 'Now you can make change to the product with the following commands : \n1. wg product info \n2. wg product name <product-name> \n3. wg product cost <product-cost> \n4. wg product desc <product-desc> \n5. wg product image <product-image>'
]; ];
@@ -132,9 +132,9 @@ const commands = [
return message; return message;
} }
let message = [ let message = [
'Your website has following products' 'Your website has following products: '
]; ];
for(let p of selected_website.products.getAllData().values()) 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 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); message.push(m);
@@ -158,11 +158,11 @@ const commands = [
return message; return message;
} }
let message = [ let message = [
'Product id: ' + p.id, 'Product id: ' + selected_product.id,
'Product Name : ' + p.name , 'Product Name : ' + selected_product.name ,
'Product description : '+ p.desc , 'Product description : '+ selected_product.desc ,
'Product Cost: ' + p.cost , 'Product Cost: ' + selected_product.cost ,
'Product Image : '+ p.image 'Product Image : '+ selected_product.image
]; ];
return message; return message;
@@ -182,9 +182,9 @@ const commands = [
]; ];
return message; return message;
} }
selected_product.name= input; selected_product.setName(input['product-name']);
let message = [ let message = [
'The product name has been set to '+ given_name 'The product name has been set to '+ selected_product.name
]; ];
return message; return message;
@@ -204,9 +204,9 @@ const commands = [
]; ];
return message; return message;
} }
selected_product.cost= input; selected_product.setCost(input['product-cost']);
let message = [ let message = [
'The product cost has been set to '+ given_cost 'The product cost has been set to '+ selected_product.cost
]; ];
return message; return message;
@@ -226,9 +226,9 @@ const commands = [
]; ];
return message; return message;
} }
selected_product.desc = input; selected_product.setDesc(input['product-desc']);
let message = [ let message = [
'The product description has been set to : '+ given_desc 'The product description has been set to : '+ selected_product.desc
]; ];
return message; return message;
@@ -248,9 +248,9 @@ const commands = [
]; ];
return message; return message;
} }
selected_product.image = input; selected_product.setImage(input['product-image']);
let message = [ let message = [
'The product image has been set to : '+ given_image 'The product image has been set to : '+ selected_product.image
]; ];
return message; return message;