structured the logic.js

This commit is contained in:
Mohammed Ashab Uddin
2020-07-30 11:27:37 +04:00
parent e936c4d312
commit d0655737f1
4 changed files with 276 additions and 202 deletions

View File

@@ -1,44 +1,275 @@
const { Website } = require("../models/website");
const { data } = require("../models/data");
const { Product } = require('../models/product');
const { User } = require('../models/user');
module.exports.onWG = () => {
console.log('hello world');
/**
* user to be sent the message to
* @type {User}
*/
let user = null;
/**
* input to be used in the function if exists
* @type {string}
*/
let input = null;
const addProduct = (id) => {
// Create a product and select it.
//At this point all the product properties will be default
var product = new Product({id : id});
// Add a product to the selected website.
user.getSelectedWebsite().addProduct(product);
}
module.exports.onCreate = (id, companyName) => {
const checkSelectedWebsiteExist= () => {
//get the user
let user = data.getUser(id);
if(user == undefined)
if(user.getSelectedWebsite() == null)
{
return [
`A website should be selected or created
To create website, please use the following command : `,
`*wg create <company-name>*`,
];
}
return '';
}
const checkSelectedProductExist = () => {
if(user.getSelectedWebsite().getSelectedProduct() == null)
{
return [
`A product should be created or selected`
]
}
return '';
}
module.exports.setUser = (id) =>{
if(!data.isUser(id))
{
data.addUser(id);
user = data.getUser(id);
}
user = data.getUser(id)
};
module.exports.setInput= (value) => {
input = value;
}
//add the website with the company name
user.addWebsite(new Website(companyName));
}
module.exports.addProduct = (id,website) => {
if(id==null)
module.exports.onCreateProduct = () => {
let check = checkSelectedWebsiteExist();
if(check)
{
// 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
//add the product
addProduct(input['id']);
let selectedId = user.getSelectedWebsite().getSelectedProduct().id;
//mesage to be sent to the user
let message = [
`The product has been created with the id : ${selectedId}` ,
`
This product has been selected.
Now you can make change to the product with the following commands :
1. wg product info
2. wg product name <product-name>
3. wg product cost <product-cost>
4. wg product desc <product-desc>
5. wg product image <product-image>
`
];
return message;
}
return check;
}
module.exports.onDeleteProduct = () => {
let check = checkSelectedWebsiteExist();
if(check)
{
id = Math.floor((Math.random() * 100000) + 1);
if(!user.getSelectedWebsite().deleteProduct(input['id']))
{
return [
`No product with the given id`
];
}
return [
`The product has been deleted with the id : ` + input['id']
];
}
return check;
}
module.exports.onSelectProduct = () => {
let check = checkSelectedWebsiteExist();
if(check)
{
// Find the product with the given id
if(!user.getSelectedWebsite().selectProduct(input['id']))
{
return [
`There was no product with the given id. Use the following command to get the list of all id's`,
`wg product all`
];
}
return [
`The product has been selected with the id : ${user.getSelectedWebsite().getSelectedProduct()}` ,
`
This product has been selected.
Now you can make change to the product with the following commands :
1. wg product info
2. wg product name <product-name>
3. wg product cost <product-cost>
4. wg product desc <product-desc>
5. wg product image <product-image>
`
];
}
return check;
}
module.exports.onGetAllProducts = () => {
let check = checkSelectedWebsiteExist();
if(check)
{
let message = [
'Your website has following products: '
];
for(let p of user.getSelectedWebsite().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;
}
return check;
}
module.exports.onGetProductInfo = () => {
let check = checkSelectedWebsiteExist();
if(check)
{
let check = checkSelectedProductExist();
if(check)
{
let selected_product = user.getSelectedWebsite().getSelectedProduct();
return [
`
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 check;
}
module.exports.onSetProductName = () => {
let check = checkSelectedWebsiteExist();
if(check)
{
let check = checkSelectedProductExist();
if(check)
{
let selected_product = user.getSelectedWebsite().getSelectedProduct();
selected_product.setName(input['product-name']);
let message = [
'The product name has been set to '+ selected_product.name
];
return message;
}
}
// 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;
return check;
}
module.exports.deleteProduct = (id,website) => {
return website.deleteProduct(id);
module.exports.onSetProductCost = () => {
let check = checkSelectedWebsiteExist();
if(check)
{
let check = checkSelectedProductExist();
if(check)
{
let selected_product = user.getSelectedWebsite().getSelectedProduct();
selected_product.setCost(input['product-cost']);
let message = [
'The product cost has been set to '+ selected_product.cost
];
return message;
}
}
return check;
}
module.exports.onSetProductDesc = () => {
let check = checkSelectedWebsiteExist();
if(check)
{
let check = checkSelectedProductExist();
if(check)
{
let selected_product = user.getSelectedWebsite().getSelectedProduct();
selected_product.setDesc(input['product-desc']);
let message = [
'The product cost has been set to '+ selected_product.desc
];
return message;
}
}
return check;
}
module.exports.onSetProductImage = () => {
let check = checkSelectedWebsiteExist();
if(check)
{
let check = checkSelectedProductExist();
if(check)
{
let selected_product = user.getSelectedWebsite().getSelectedProduct();
selected_product.setImage(input['product-image']);
let message = [
'The product cost has been set to '+ selected_product.image
];
return message;
}
}
return check;
}

View File

@@ -34,6 +34,9 @@ class User{
return this.websites.selectData(companyName);
}
/**
* @returns {Website}
*/
getSelectedWebsite(){
return this.websites.selectedData;
}

View File

@@ -39,7 +39,7 @@ class Website {
/**
*
* @returns {[]}
* returns all the products in the website
*/
getAllProduct(){
return this.products.getAllData().values();
@@ -58,6 +58,9 @@ class Website {
return this.products.selectData(productId);
}
/**
* @returns {Product}
*/
getSelectedProduct()
{
return this.products.selectedData;

View File

@@ -31,231 +31,64 @@ const commands = [
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;
}
callback : () => logic.onCreateProduct()
}),
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;
}
callback : () => logic.onDeleteProduct()
}),
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;
}
callback : () => logic.onSelectProduct()
}),
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;
}
callback : () => logic.onGetAllProducts()
}),
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;
}
callback : () => logic.onGetProductInfo()
}),
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;
}
callback : () => logic.onSetProductName()
}),
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;
}
callback : () => logic.onSetProductCost()
}),
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;
}
callback : () => logic.onSetProductDesc()
}),
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;
}
callback : () => logic.onSetProductImage()
}),
];
@@ -282,7 +115,11 @@ module.exports= onMessage = (message, client) => {
input = command.getInput(message.body);
}
let messageToBeSent = command.callback(input);
logic.setUser(message.id.remote);
logic.setInput(input);
let messageToBeSent = command.callback();
for (let msg of messageToBeSent)
{
client.sendMessage(message.from, msg, new Object());