Created
July 30, 2017 00:56
-
-
Save sandrooliveira/eca886db660f16853cc0863f1426b82e to your computer and use it in GitHub Desktop.
Fullstack-Academy-2: Exercicio 1
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>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="resutado"> | |
<h2>Resultado</h2> | |
<p>R$ <%= resultado.total.toFixed(2) %></p> | |
<h2>Evolution</h2> | |
<table> | |
<tr> | |
<th>Month</th> | |
<th>Value</th> | |
</tr> | |
<tbody> | |
<% resultado.evolution.forEach((ev) => { %> | |
<tr> | |
<td><%= ev.month %></td> | |
<td>R$ <%= ev.value.toFixed(2) %></td> | |
</tr> | |
<% }) %> | |
</tbody> | |
</table> | |
</div> | |
<% } %> | |
<% include footer %> |
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) | |
} | |
}) | |
}) | |
} | |
app.get('/operacoes', async (req, res) => { | |
const operacoes = await findAll(app.db, 'operacoes') | |
res.render('operacoes', { operacoes }) | |
}) | |
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...') }) | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Certinho.