Created
July 30, 2017 00:58
-
-
Save sandrooliveira/e55681360f01b8fb8726d1384b3878ce to your computer and use it in GitHub Desktop.
Fullstack-Academy-2: Exercicio 2
This file contains hidden or 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
const express = require('express') | |
const app = express() | |
const port = 3000 | |
const bodyParser = require('body-parser') | |
app.use(bodyParser.urlencoded({ extended: false })) | |
const MongoClient = require('mongodb').MongoClient | |
const mongoUri = 'mongodb://<USER>:<PASSWORD>@meu-dinheiro-shard-00-00-mrafo.mongodb.net:27017,meu-dinheiro-shard-00-01-mrafo.mongodb.net:27017,meu-dinheiro-shard-00-02-mrafo.mongodb.net:27017/meu-dinheiro-live?ssl=true&replicaSet=meu-dinheiro-shard-0&authSource=admin' | |
app.use(express.static('public')) | |
const path = require('path') | |
//Onde estão os templates | |
app.set('views', path.join(__dirname, 'views')) | |
//Tipo de Template | |
app.set('view engine', 'ejs') | |
app.get('/', (req, res) => { | |
const image = 'images/meu-dinheiro.png' | |
res.render('home') | |
}) | |
const calculoJuros = (p, i, n) => p * Math.pow(1 + i, n) | |
const calcEvolution = (p,i,n) => { | |
const months = Array.from(new Array(n), (n,i) => i + 1) | |
return months.map((month) => { | |
return { | |
month:month, | |
value:calculoJuros(p,i,month) | |
} | |
}) | |
} | |
app.get('/calculadora', (req, res) => { | |
const resultado = { | |
calculado: false | |
} | |
if (req.query.valorInicial && req.query.taxa && req.query.tempo) { | |
const valorInicial = parseFloat(req.query.valorInicial) | |
const taxa = parseFloat(req.query.taxa) / 100 | |
const tempo = parseInt(req.query.tempo) | |
resultado.calculado = true | |
resultado.total = calculoJuros(valorInicial,taxa,tempo) | |
resultado.evolution = calcEvolution(valorInicial,taxa,tempo) | |
} | |
res.render('calculadora', { resultado }) | |
}) | |
const findAll = (db, collectionName) => { | |
const collection = db.collection(collectionName) | |
const cursor = collection.find({}) | |
const documents = [] | |
return new Promise((resolve, reject) => { | |
cursor.forEach( | |
(doc) => documents.push(doc), | |
() => resolve(documents) | |
) | |
}) | |
} | |
const insert = (db, collectionName, document) => { | |
const collection = db.collection(collectionName) | |
return new Promise((resolve, reject) => { | |
collection.insert(document, (err, doc) => { | |
if(err){ | |
reject(err) | |
}else{ | |
resolve(doc) | |
} | |
}) | |
}) | |
} | |
const calcExtrato = (operacoes) => { | |
const opsWithSubTotal = [] | |
const total = operacoes.reduce((prev,curr) => { | |
const valor = parseFloat(curr.valor) | |
opsWithSubTotal.push({ | |
description:curr.descricao, | |
value:valor, | |
subTotal:prev + valor | |
}) | |
return prev + valor; | |
},0) | |
return { | |
total:total, | |
operacoes:opsWithSubTotal | |
} | |
} | |
app.get('/operacoes', async (req, res) => { | |
const operacoes = await findAll(app.db, 'operacoes') | |
const extrato = calcExtrato(operacoes) | |
res.render('operacoes', { extrato }) | |
}) | |
app.get('/nova-operacao', (req, res) => res.render('nova-operacao')) | |
app.post('/nova-operacao', async (req, res) => { | |
const operacao = { | |
descricao: req.body.descricao, | |
valor: parseFloat(req.body.valor) | |
} | |
const newOperacao = await insert(app.db,'operacoes',operacao) | |
res.redirect('/operacoes') | |
}) | |
MongoClient.connect(mongoUri, (err, db) => { | |
if (err) { | |
return | |
} else { | |
app.db = db | |
app.listen(port, () => { console.log('Server Running...') }) | |
} | |
}) | |
This file contains hidden or 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
<% include header %> | |
<h2>Operaçoes</h2> | |
<table> | |
<thead> | |
<tr> | |
<th>Description</th> | |
<th>Value</th> | |
<th>Subtotal</th> | |
</tr> | |
</thead> | |
<tbody> | |
<% extrato.operacoes.forEach((operacao) => { %> | |
<tr> | |
<td><%= operacao.description %></td> | |
<td>R$ <%= operacao.value %></td> | |
<td>R$ <%= operacao.subTotal %></td> | |
</tr> | |
<% }) %> | |
<tr> | |
<td></td> | |
<td>Saldo</td> | |
<td><%= extrato.total %></td> | |
</tr> | |
</tbody> | |
</table> | |
<% include footer %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Certinho.