Merge pull request #8 from Baber-Jan/master

Added all the product methods,
This commit is contained in:
ashab272000
2020-07-28 11:01:05 +04:00
committed by GitHub
7 changed files with 324 additions and 8 deletions

View File

@@ -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));
}
}
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);
}

View File

@@ -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;
}

View File

@@ -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;

View File

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

View File

@@ -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 <id>`,
//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 <product-name> \n3. wg product cost <product-cost> \n4. wg product desc <product-desc> \n5. wg product image <product-image>`
];
return message;
}
}),
new Command({
//User command
command: `wg product delete <id>`,
//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 <id>`,
//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 <product-name> \n3. wg product cost <product-cost> \n4. wg product desc <product-desc> \n5. wg product image <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 <product-name> \n3. wg product cost <product-cost> \n4. wg product desc <product-desc> \n5. wg product image <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 <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 <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 <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 <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) => {
}
}
};
};

View File

@@ -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 <id>",

View File

@@ -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);
}
})
})