Created
July 23, 2017 15:26
-
-
Save helton/4126dd40297de52b0328772d7375793c to your computer and use it in GitHub Desktop.
Full Stack Academy - Aula 02 - Exercício 04
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>Contas Mensais</h2> | |
<table> | |
<thead> | |
<tr> | |
<th>Descrição</th> | |
<th>Valor Estimado</th> | |
<th>Dia do Vencimento</th> | |
</tr> | |
</thead> | |
<tbody> | |
<% contasMensais.forEach(contaMensal => { %> | |
<tr> | |
<td><%= contaMensal.descricao %></td> | |
<td class="currency"><%= functions.formatCurrency(contaMensal.valorEstimado) %></td> | |
<td><%= contaMensal.diaVencimento %></td> | |
</tr> | |
<% }) %> | |
<tbody> | |
<tfoot> | |
<tr> | |
<td>Total</td> | |
<td class="currency"><%= functions.formatCurrency(functions.sum(contasMensais, ({valorEstimado}) => valorEstimado)) %></td> | |
<td></td> | |
</tr> | |
</tfoot> | |
</table> | |
<% 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Meu Dinheiro</title> | |
<link href="https://fonts.googleapis.com/css?family=Raleway:400,700" rel="stylesheet"> | |
<link rel="stylesheet" href="css/styles.css"> | |
</head> | |
<body style="margin: 0"> | |
<section class="header"> | |
<img src="/images/meu-dinheiro.png"> | |
<ul> | |
<li><a href="/">Home</a></li> | |
<li><a href="/calculadora">Calculadora</a></li> | |
<li><a href="/operacoes">Operações</a></li> | |
<li><a href="/nova-operacao">Nova Operação</a></li> | |
<li><a href="/contas-mensais">Contas Mensais</a></li> | |
<li><a href="/nova-conta-mensal">Nova Conta Mensal</a></li> | |
</ul> | |
</section> | |
<section class="content"> |
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 | |
} | |
if (req.query.valorInicial && req.query.taxa && req.query.tempo) { | |
resultado.calculado = true | |
resultado.total = calculoJuros( | |
parseFloat(req.query .valorInicial), | |
parseFloat(req.query.taxa/100), | |
parseInt(req.query.tempo) | |
) | |
} | |
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 }) | |
}) | |
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('/') | |
} | |
}) | |
app.get('/contas-mensais', async (req, res) => { | |
const contasMensais = await findAll(app.db, 'contas-mensais') | |
render(res, 'contas-mensais', { contasMensais }) | |
}) | |
app.get('/nova-conta-mensal', (req, res) => { | |
render(res, 'nova-conta-mensal') | |
}) | |
app.post('/nova-conta-mensal', async (req, res) => { | |
const contaMensal = { | |
descricao: req.body.descricao, | |
valorEstimado: parseFloat(req.body.valorEstimado), | |
diaVencimento: parseInt(req.body.diaVencimento) | |
} | |
try { | |
const result = await insert(app.db, 'contas-mensais', contaMensal) | |
console.log(result) | |
res.redirect('contas-mensais') | |
} 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}...`)) | |
} | |
}) |
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>Nova Conta Mensal</h2> | |
<form method="POST"> | |
Descriçāo: <input type="text" name="descricao"><br> | |
Valor Estimado: <input type="text" name="valorEstimado"><br> | |
Dia do Vencimento: <input type="text" name="diaVencimento"><br> | |
<button type="submit">Inserir</button> | |
</form> | |
<% include footer %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Certinho.