-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from lortmorris/master
update
Showing
23 changed files
with
470 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
|
||
const vendors = []; | ||
const customers = []; | ||
const sales = []; | ||
const names = ['Pepe', 'Luis', 'Carlos', 'Miguel']; | ||
const lnames = ['Rodriguez', 'Casas', 'Perez', 'Suarez']; | ||
const countries = ['USA', 'BR', 'ARG', 'MX', 'JPG']; | ||
const letters = ['AK', 'CK', 'N', 'SK']; | ||
|
||
|
||
const getRandomName = data => data[ Math.floor(Math.random()* data.length) ]; | ||
|
||
const generateVendors = total => { | ||
for(let x=0; x<total; x++) vendors.push({ | ||
Id: Math.floor(Math.random()* 3000), | ||
fname: getRandomName(names), | ||
lname: getRandomName(lnames), | ||
}); | ||
} | ||
|
||
const generateCustomers = total => { | ||
for(let x=0; x<total; x++) customers.push({ // x 1000 | ||
Id: Math.floor(Math.random()*10000), | ||
fname: getRandomName(names), | ||
lname: getRandomName(lnames), | ||
passport: Math.floor(Math.random()*1000000)+getRandomName(letters), | ||
country: getRandomName(countries), | ||
}); | ||
} | ||
|
||
|
||
const generateSales = limit =>{ | ||
for(let x=0; x<limit; x++) sales.push({ | ||
date: new Date(), | ||
vendorId: getRandomName(vendors).Id, | ||
customerId: getRandomName(customers).Id, | ||
flyId: 'KML102', | ||
price: (Math.random() * 1000) + 1 | ||
}); | ||
} | ||
generateVendors(500); | ||
generateCustomers(1000); | ||
generateSales(100000); | ||
|
||
const mySales = { | ||
vendors: vendors, | ||
customers: customers, | ||
sales: sales | ||
}; | ||
|
||
class Results { | ||
constructor(results){ | ||
this.data = results; | ||
} | ||
|
||
currency(sym, value){ | ||
this.data = this.data.map(e => { | ||
e[sym] = e.price * value; | ||
return e; | ||
}); | ||
return this; | ||
} | ||
|
||
eq(prop, value){ | ||
this.data = this.data.filter(e => e[prop] === value); | ||
return this; | ||
} | ||
|
||
not(prop, value){ | ||
this.data = this.data.filter(e => e[prop] !== value); | ||
return this; | ||
} | ||
|
||
r(prop, value){ | ||
this.data = this.data.filter( e => e[prop] > value); | ||
return this; | ||
} | ||
|
||
l(prop, value){ | ||
this.data = this.data.filter( e => e[prop] < value); | ||
return this; | ||
} | ||
|
||
in(prop, value){ | ||
this.data = this.data.filter(e => value.indexOf(e[prop]) > -1 ? true: false ); | ||
return this; | ||
} | ||
|
||
nin(prop, value){ | ||
this.data = this.data.filter(e => value.indexOf(e[prop]) === -1 ? true: false ); | ||
return this; | ||
} | ||
} | ||
|
||
|
||
const myResults = new Results(mySales.sales); | ||
|
||
console.log( myResults.r('price', 500) | ||
.l('vendorId', 2000) | ||
.currency('ARG', 15) | ||
.currency('MX', 18) | ||
.nin('price', [500, 501]) | ||
.l('ARG', 10000) | ||
.data.length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const http = require('http'); | ||
const fs = require('fs'); | ||
const urlParams = require('./mws/query'); | ||
const controllers = require('./controllers'); | ||
const Application = { | ||
version: '0.1', | ||
controllers: controllers, | ||
users: [] | ||
}; | ||
const virtualDirs = require('./routes')(Application); | ||
|
||
|
||
fs.readFile('./users', (err, data)=>{ | ||
err ? '' : Application.users = JSON.parse(data); | ||
}) | ||
|
||
const getFile = (req, res, next)=> | ||
fs.readFile('./public' + req.url, (err, data) => | ||
err ? next() : res.end(data.toString()) | ||
); | ||
|
||
const isVirtualDir = (req, res, next) => { | ||
if(`${req.method}${req.url}` in virtualDirs) return virtualDirs[`${req.method}${req.url}`](req, res); | ||
return next(req, res); | ||
} | ||
|
||
const show404 = (req, res)=> res.end('404'); | ||
|
||
|
||
const server = http.createServer((req, res) => { | ||
urlParams(req, res); | ||
req.users = Application.users; | ||
//req.url = req.url === '/' ? '/index.html' : req.url; | ||
//getFile(req, res, ()=> isVirtualDir(req, res, show404)); | ||
|
||
res.end('hola') | ||
}); | ||
|
||
server.listen(5000); | ||
console.log('final'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = Object.assign({}, | ||
require('./users') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const fs = require('fs'); | ||
|
||
module.exports = { | ||
users: { | ||
addUser: (req, res) => { | ||
req.users.push(req.query); | ||
fs.writeFile('./users.json', JSON.stringify(req.users), (err)=>{ | ||
res.end(err ? 'error' : 'saved'); | ||
}); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const urlParams = (req, res)=> { | ||
req.query = {}; | ||
if(req.url.indexOf('?') === -1) return; | ||
let parts = req.url.split('?'); | ||
req.url = parts[0]; | ||
|
||
parts = parts[1].split('&') | ||
.map(v => v.split('=')) | ||
.filter(p=> p.length===2) | ||
.map(p=> (req.query[p[0]] = p[1] )); | ||
|
||
} | ||
|
||
module.exports = urlParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
soy un campeon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<html> | ||
<body> | ||
<h1>Node.js rulez</h1> | ||
<form method='get' action='/users/save'> | ||
|
||
<p>Name: <input type='text' name='name' placeholder='your name' /></p> | ||
<p>DNI: <input type='text' name='dni' placeholder='your dni' /></p> | ||
<p>Course: <input type='text' name='course' placeholder='your course' /></p> | ||
<p><input type='submit' value="Add+" /></p> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
let counter = 0; | ||
|
||
const virtualDirs = { | ||
'GET/counter': (req, res)=> res.end('' + counter++), | ||
'GET/today': (req, res)=> res.end(new Date().toString()), | ||
'POST/counter': (req, res) => res.end(''+counter * 100), | ||
'GET/echo': (req, res)=> res.end(JSON.stringify(req.query)) | ||
}; | ||
|
||
|
||
module.exports = App =>{ | ||
Object.assign(virtualDirs, require('./users')(App)); | ||
return virtualDirs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = App => { | ||
return { | ||
'GET/users/save': (req, res)=> App.controllers.users.addUser(req, res), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"name":"","dni":"","course":""},{"name":"pepe","dni":"30303030","course":"node"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const http = require('http'); | ||
const express = require('express'); | ||
const exphbs = require('express-handlebars'); | ||
const bodyParser = require('body-parser'); | ||
const helmet = require('helmet'); | ||
const compression = require('compression') | ||
const fs = require('fs'); | ||
const app = express(); | ||
const server = http.createServer(app); | ||
let users = []; | ||
let counter = 0; | ||
|
||
app.engine('handlebars', exphbs({defaultLayout: 'main'})); | ||
app.set('view engine', 'handlebars'); | ||
app.use(helmet()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(bodyParser.json()); | ||
app.use( express.static('./public') ); | ||
app.use( compression() ); | ||
fs.readFile('./users.json', (err, data)=>{ | ||
try{ | ||
users = JSON.parse(data.toString()) | ||
}catch(e){ | ||
users = []; | ||
} | ||
server.listen(5000); | ||
}); | ||
|
||
|
||
app.get('/', (req, res)=>{ | ||
res.render('list', { | ||
users: users | ||
}); | ||
}); | ||
|
||
app.get('/api/users', (req, res)=> res.json(users)); | ||
app.get('/add', (req, res)=> res.render('form') ); | ||
|
||
const addUser = (req, res) => { | ||
users.push(req.body); | ||
fs.writeFile('./users.json', JSON.stringify(users), (err)=>{ | ||
res.redirect('/'); | ||
}); | ||
} | ||
|
||
app.get('/echo', (req, res)=> res.end(JSON.stringify(req.query))); | ||
app.get('/today', (req, res)=> res.end(new Date().toString())); | ||
app.get('/counter', (req, res)=> res.end(''+counter++)); | ||
app.post('/counter', (req, res)=> res.end(''+counter * 100)); | ||
app.post('/users/save', addUser); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "onion", | ||
"version": "1.0.0", | ||
"description": "The express example", | ||
"main": "app.js", | ||
"scripts": { | ||
"test": "mocha", | ||
"start": "node app.js" | ||
}, | ||
"keywords": [ | ||
"onion" | ||
], | ||
"author": "Cesar Casas <cesarcasas@bsdsolutions.com.ar> (https://ar.linkedin.com/in/cesarcasas)", | ||
"license": "ISC", | ||
"dependencies": { | ||
"body-parser": "^1.17.1", | ||
"compression": "^1.6.2", | ||
"express": "^4.15.2", | ||
"express-handlebars": "^3.0.0", | ||
"helmet": "^3.5.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<html> | ||
<head> | ||
<title>Users</title> | ||
</head> | ||
<body> | ||
<ul id='list'></ul> | ||
<script type="text/javascript"> | ||
fetch('http://localhost:5000/api/users') | ||
.then(response=> response.json()) | ||
.then(data => { | ||
document.getElementById('list').innerHTML = ''; | ||
data.forEach(item=> { | ||
let el = document.createElement('li'); | ||
document.getElementById('list').appendChild(el); | ||
el.innerHTML = `<span>${item.name}</span> | <span>${item.dni}</span> | <span>${item.course}</span>`; | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"name":"pepe","dni":"123123","course":"node"},{"name":"cacho","dni":"123123123","course":"C++"},{"name":"estella","dni":"02029292","course":"Ingles"},{"name":"tito","dni":"333333","course":"ninja"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
<form method='post' action='/users/save'> | ||
<p>Name: <input type='text' name='name' placeholder='your name' /></p> | ||
<p>DNI: <input type='text' name='dni' placeholder='your dni' /></p> | ||
<p>Course: <input type='text' name='course' placeholder='your course' /></p> | ||
<p><input type='submit' value="Add+" /></p> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<html> | ||
<body> | ||
<h1>Node.js rulez, class 3 </h1> | ||
<a href='/add'>Add</a> | <a href='/'>List</a> | ||
{{{body}}} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<table> | ||
<tr> | ||
<th>Name</th> | ||
<th>DNI</th> | ||
<th>Course</th> | ||
</tr> | ||
{{#each users}} | ||
<tr> | ||
<td>{{this.name}}</td> | ||
<td>{{this.dni}}</td> | ||
<td>{{this.course}}</td> | ||
</tr> | ||
{{/each}} | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
home: last 50 sales [date, customers.fname, customer.lname, passport, price] | ||
search: { | ||
vendor: fname or lnames | ||
customer: passsport | ||
} | ||
|
||
item de result: click -> detail : customer, vendor, etc. | ||
|
||
vendors: | ||
ABM: | ||
click vendor -> All sales | ||
stats: total money | ||
total sales |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const http = require('http'); | ||
const express = require('express'); | ||
const mongojs = require('mongojs'); | ||
const exphbs = require('express-handlebars'); | ||
const db = mongojs('mongodb://localhost:27017/sales', ['vendors', 'customers', 'sales']); | ||
const app = express(); | ||
|
||
app.engine('.hbs', exphbs({defaultLayout: 'main', extname: '.hbs'})); | ||
app.set('view engine', '.hbs'); | ||
|
||
const server = http.createServer(app); | ||
app.use( express.static('./public') ); | ||
|
||
|
||
app.get('/', (req, res)=>{ | ||
const qtype = req.query.qtype || null; | ||
const query = {}; | ||
if(qtype){ | ||
switch(qtype){ | ||
case 'vendor': | ||
query['$or'] = [{'vendor.fname' : req.query.param}, {'vendor.lname' : req.query.param}]; | ||
break | ||
case 'passport': | ||
query['customer.passport'] = req.query.param; | ||
break; | ||
} | ||
} | ||
db.sales.find(query, {}).sort({_id: 1}).limit(50, (err, sales)=>{ | ||
res.render('home', {sales}); | ||
}); | ||
|
||
}); | ||
|
||
|
||
app.get('/vendors', (req, res)=>{ | ||
const query = {}; | ||
if(req.query.param){ | ||
query['$or'] = [{'fname' : req.query.param}, {'lname' : req.query.param}]; | ||
} | ||
|
||
db.vendors.find(query, {}, (err, vendors)=>{ | ||
res.render('vendors', {vendors}); | ||
}); | ||
|
||
}); | ||
|
||
server.listen(5000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "sales", | ||
"version": "1.0.0", | ||
"description": "sales abm and stats", | ||
"main": "app.js ", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"keywords": [ | ||
"expressjs", | ||
"mongodb", | ||
"mongojs", | ||
"sales", | ||
"stats" | ||
], | ||
"author": "Cesar Casas <cesarcasas@bsdsolutions.com.ar> (https://ar.linkedin.com/in/cesarcasas)", | ||
"license": "ISC", | ||
"dependencies": { | ||
"body-parser": "^1.17.1", | ||
"compress": "^0.99.0", | ||
"express": "^4.15.2", | ||
"express-handlebars": "^3.0.0", | ||
"helmet": "^3.6.0", | ||
"mongojs": "^2.4.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
<form method="get" action="/"> | ||
<input type="text" name="param" placeholder="Query? " /> | ||
<select name="qtype"> | ||
<option value="vendor">Vendor</option> | ||
<option value="passport">Passport</option> | ||
</select> | ||
<input type="submit" value="Search" /> | ||
</form> | ||
<table> | ||
|
||
<tr> | ||
<th>Date</th> | ||
<th>Fname</th> | ||
<th>Lname</th> | ||
<th>Passport</th> | ||
<th>Price</th> | ||
</tr> | ||
{{#each sales}} | ||
<tr> | ||
<td>{{this.date}}</td> | ||
<th>{{this.customer.fname}}</th> | ||
<th>{{this.customer.lname}}</th> | ||
<th>{{this.customer.passport}}</th> | ||
<th>{{this.price}}</th> | ||
</tr> | ||
{{/each}} | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
|
||
<head> | ||
|
||
</head> | ||
<body> | ||
{{{body}}} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
<form method="get" action="/vendors"> | ||
<input type="text" name="param" placeholder="Query? " /> | ||
<input type="submit" value="Search" /> | ||
</form> | ||
|
||
<table> | ||
<tr> | ||
<th>Fname</th> | ||
<th>Lname</th> | ||
<th>Age</th> | ||
</tr> | ||
{{#each vendors}} | ||
<tr> | ||
<th>{{this.fname}}</th> | ||
<th>{{this.lname}}</th> | ||
<th>{{this.age}}</th> | ||
</tr> | ||
{{/each}} | ||
</table> |