Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
Untitled-1
9 changes: 9 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"development": {
"username": "postgres",
"password":"test123",
"database": "test",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
66 changes: 66 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<html>

<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function () {
$("input[type='button']").click(function () {
var radioValue = $("input[name='type']:checked").val();
if (radioValue == "text") {
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:3000/",
"method": "POST",
"headers": {
"Content-Type": "text/plain",
"cache-control": "no-cache",
},
"data": document.getElementById('data').value
}

$.ajax(settings).done(function (response) {
let str="";
for(let i of response.stmt){
str+=i+"<br>"
}
document.getElementById('data1').innerHTML=`${response.name}<br>${str}Sales:${response.sales}<br>Total:${response.total}`
});
}
else {
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:3000/",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"cache-control": "no-cache",
},
"data": document.getElementById('data').value
}

$.ajax(settings).done(function (response) {
let str="";
for(let i of response.stmt){
str+=i+"<br>"
}
document.getElementById('data1').innerHTML=`${response.name}<br>${str}Sales:${response.sales}<br>Total:${response.total}`
});
}
});

});
</script>
</head>

<body>
<input type="radio" name="type" value="json" />json
<input type="radio" name="type" value="text" />text
<input type="text" name="data" id="data">
<input type="button" value="Get Value">
<p id='data1'></p>

</body>

</html>
93 changes: 93 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const express=require('express');
const {insert}=require('./sequelizeorder');
const Sequelize = require('sequelize');
const bodyParser=require('body-parser')
const app = express();
const tax = require('./sales_imported');
const text_tax = require('./sales_imported_text');
app.use("/", bodyParser.json());
app.use("/", bodyParser.text())
app.get("/", (req, res) =>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting?

console.count('homepage');
res.sendFile('/home/asd/myapp/index.html', (err) =>{
if (err) {
console.log(err);
}
});
});
app.post('/', (req, res, next) =>{
if (req.headers["content-type"] == "application/json") {
console.log(req.body);
var array = req.body.items;
var str = []
let total = 0;
let sales = 0;
let reqtime=new Date().getTime();
for (let item of array) {
str.push(`${item.quantity} ${tax.isimported(item)} ${item.name}: ${item.price + tax.salestax(item) + tax.importtax(item)}`);
sales += tax.salestax(item) + tax.importtax(item);
priceall=item.price + tax.salestax(item) + tax.importtax(item);
total += item.price + tax.salestax(item) + tax.importtax(item);
insert(req.body.name,item,priceall,tax.isimported(item))
}
res.json({
"name":req.body.name,
"stmt":str,
"total":total,
"sales":sales

});
console.dir({
"name":req.body.name,
"stmt":str,
"total":total,
"sales":sales

})
let restime=new Date().getTime();
res.end();
} if (req.headers["content-type"] == "text/plain") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same line?

next();
}
}, (req, res) =>{
let reqtime=new Date().getTime();
let str = req.body;
orderNo = str.split(":");
sentences = orderNo[1].split("\\n");
quantity = [];
body = [];
price = [];
for (let i = 1; i < sentences.length; i++) {
regex = new RegExp('(\\d+)');
temp = sentences[i].split(regex);
quantity.push(temp[1]);
lastind=temp[2].lastIndexOf("at");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh ugh

body.push(temp[2].substr(0,lastind)+":");
price.push(temp[3]+temp[4]+temp[5]);
}
stmt=[];
total=0;
sales=0
for(let i=1;i<sentences.length;i++){
sales+=text_tax.sales_tax(quantity[i-1],body[i-1],price[i-1])+text_tax.imported_tax(quantity[i-1],body[i-1],price[i-1]);
aftertax=parseFloat(price[i-1])+text_tax.sales_tax(quantity[i-1],body[i-1],price[i-1])+text_tax.imported_tax(quantity[i-1],body[i-1],price[i-1]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not just a murder. This is genocide!

stmt.push(`${quantity[i-1]} ${body[i-1]} ${aftertax}`);
total+=aftertax;
console.log(body[i-1].split(':'))
item={
'name':body[i-1].split(':')[0],
'category':text_tax.category(body[i-1]),
'quantity':quantity[i-1]
}
insert(orderNo[0],item,aftertax,text_tax.imported_category(body[i-1]))
}
res.json({
"name":orderNo[0],
"stmt":stmt,
"total":total,
"sales":sales
});
let restime=new Date().getTime();
res.end("hello");
});
app.listen(3000);
27 changes: 27 additions & 0 deletions migrations/20190415072048-create-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('orders', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
orderName: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('orders');
}
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOF?

49 changes: 49 additions & 0 deletions migrations/20190415080724-order_attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return Promise.all(
[
queryInterface.addColumn('orders', 'itemName', {
type: Sequelize.STRING,
allowNull: false
}),
queryInterface.addColumn('orders', 'itemCategory', {
type: Sequelize.STRING,
allowNull: false
}),
queryInterface.addColumn('orders', 'quantity', {
type: Sequelize.INTEGER,
allowNull: false
}),
queryInterface.addColumn('orders', 'price', {
type: Sequelize.FLOAT,
allowNull: false
}),
queryInterface.addColumn('orders', 'imported', {
type: Sequelize.STRING,
allowNull: true
})
]
)
},

down: (queryInterface, Sequelize) => {
return Promise.all(
[
queryInterface.removeColumn('orders', 'itemName'),
queryInterface.removeColumn('orders', 'itemCategory'),
queryInterface.removeColumn('orders', 'quantity'),
queryInterface.removeColumn('orders', 'price'),
queryInterface.removeColumn('orders', 'imported')
]
)
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity. ,.

Example:
return queryInterface.dropTable('users');
*/
}
};
37 changes: 37 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
30 changes: 30 additions & 0 deletions models/order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const order = sequelize.define('order', {
orderName: DataTypes.STRING,
itemName: {
type: DataTypes.STRING,
allowNull: false
},
itemCategory: {
type: DataTypes.STRING,
allowNull: false
},
quantity: {
type: DataTypes.INTEGER,
allowNull: false
},
price: {
type: DataTypes.FLOAT,
allowNull: false
},
imported: {
type: DataTypes.STRING,
allowNull: true
},
}, {});
order.associate = function (models) {
// associations can be defined here
};
return order;
};
Loading