Created
July 23, 2017 13:33
-
-
Save helton/ebaddeb172666a639e82f7d663cf0ffa to your computer and use it in GitHub Desktop.
Full Stack Academy - Aula 02 - Exercício 01
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
<% include header %> | |
<h2>Calculadora</h2> | |
<form method="GET"> | |
Valor inicial: <input type="text" name="valorInicial"><br> | |
Taxa a.m.: <input type="text" name="taxa"><br> | |
Quantos meses: <input type="text" name="tempo"><br> | |
<button type="submit">Calcular</button> | |
</form> | |
<% if (resultado.calculado) { %> | |
<div class="resultado"> | |
<h2>Resultado</h2> | |
<table> | |
<thead> | |
<tr> | |
<th>Mês</th> | |
<th>Valor</th> | |
</tr> | |
</thead> | |
<tbody> | |
<% resultado.totais.forEach(iteracao => { %> | |
<tr> | |
<td><%= iteracao.mes %></td> | |
<td class="currency"><%= functions.formatCurrency(iteracao.valor) %></td> | |
</tr> | |
<% }) %> | |
<tbody> | |
</table> | |
</div> | |
<% } %> | |
<% include footer %> |
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
require('dotenv').config() | |
const express = require('express') | |
const path = require('path') | |
const bodyParser = require('body-parser') | |
const MongoClient = require('mongodb').MongoClient | |
const app = express() | |
const port = 3000 | |
const mongoUri = `mongodb://${process.env.MONGO_DB_USER}:${process.env.MONGO_DB_PASSWORD}@${process.env.MONGO_DB_HOST_NODE_01},${process.env.MONGO_DB_HOST_NODE_02},${process.env.MONGO_DB_HOST_NODE_03}/${process.env.MONGO_DB_NAME}?ssl=${process.env.MONGO_DB_SSL}&replicaSet=${process.env.MONGO_DB_REPLICA_SET}&authSource=${process.env.MONGO_DB_AUTH_SOURCE}` | |
const functions = { | |
formatCurrency: currency => { | |
let formatter = new Intl.NumberFormat('pt-BR', { | |
style: 'currency', | |
currency: 'BRL' | |
}) | |
// Contornando o problema do separador decimal não ser exibido corretamente | |
return ( | |
formatter | |
.format(currency) | |
.replace(/\./g, '#') | |
.replace(/,/g, '.') | |
.replace(/#/g, ',') | |
) | |
}, | |
sum: (collection, fn) => collection.reduce((acc, val) => acc + fn(val), 0) | |
} | |
const render = (response, view, data={}) => { | |
response.render(view, Object.assign(data, { functions })) | |
} | |
app.use(bodyParser.json()) | |
app.use(bodyParser.urlencoded({ extended: true })) | |
app.use(express.static('public')) | |
app.set('views', path.join(__dirname, 'views')) | |
app.set('view engine', 'ejs') | |
app.get('/', (req, res) => { | |
render(res, 'home') | |
}) | |
app.get('/calculadora', (req, res) => { | |
const calculoJuros = (p, i, n) => p * Math.pow(1 + i, n) | |
const resultado = { calculado: false } | |
let { valorInicial, taxa, tempo } = req.query | |
if (valorInicial && taxa && tempo) { | |
valorInicial = parseFloat(valorInicial) | |
taxa = parseFloat(taxa)/100 | |
tempo = parseInt(tempo) | |
const meses = Array.from(new Array(tempo), (tempo, i) => i) | |
resultado.totais = meses.map(mes => ({ | |
mes: mes + 1, | |
valor: calculoJuros(valorInicial, taxa, mes + 1) | |
})) | |
resultado.calculado = true | |
} | |
render(res, '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, doc) => { | |
const collection = db.collection(collectionName) | |
return new Promise((resolve, reject) => { | |
collection.insert(doc, (err, res) => err ? reject(err) : resolve(res)) | |
}) | |
} | |
app.get('/operacoes', async (req, res) => { | |
const operacoes = await findAll(app.db, 'operacoes') | |
render(res, 'operacoes', { operacoes, functions }) | |
}) | |
app.get('/nova-operacao', (req, res) => { | |
render(res, 'nova-operacao') | |
}) | |
app.post('/nova-operacao', async (req, res) => { | |
const descricao = req.body.descricao, | |
valor = parseFloat(req.body.valor) | |
const operacao = { descricao, valor } | |
try { | |
const result = await insert(app.db, 'operacoes', operacao) | |
console.log(result) | |
res.redirect('/operacoes') | |
} catch (ex) { | |
console.error(ex) | |
res.redirect('/') | |
} | |
}) | |
MongoClient.connect(mongoUri, (err, db) => { | |
if (err) { | |
console.error(err) | |
} else { | |
app.db = db | |
app.listen(port, () => console.log(`Servidor rodando na porta ${port}...`)) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Certinho.