Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions node/Order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Order = (() => {
const fs = require('fs');

this.save = (order) => {
let orders = require('../orders.json');
order.id = orders.length ? orders.length + 1 : 1;
orders.push(order);
fs.writeFile('../orders.json', JSON.stringify(orders), { flag: 'w'});
return order;
};

this.getAll = () => {
return require('../orders.json');
};

return {
save : this.save,
getAll : this.getAll
}
})();



module.exports = Order;

54 changes: 54 additions & 0 deletions node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const express = require('express'),
bodyParser = require('body-parser'),
request = require('request-promise'),
Order = require('./Order'),
moment = require('moment'),
app = express();

const urlAPI = 'https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=@dataCotacao)';
const queryString = {
'@dataCotacao' : "'07-20-2018'",
'$top' : '100',
'$format': 'json'
};
const dollarPrice = request.get({ url: urlAPI, qs: queryString, json: true });

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.get('/api/brl-usd', (req, res) => {
dollarPrice.then((price) => {
res.json({
'brl' : 1,
'usd' : price.value[0].cotacaoVenda.toFixed(2)
});
}, () => {
res.json({});
});
});

app.post('/api/orders', (req, res) => {
dollarPrice.then((price) => {
let items = req.body.items || [0];
let total = items.reduce((a, b) => Number(a) + Number(b), 0);
let totalSellPrice = price.value[0].cotacaoVenda * total;
let order = {
'created_at' : moment().format("YYYY-MM-DD HH:mm:ss"),
'total_brl' : total.toFixed(2),
'total_usd' : totalSellPrice.toFixed(2)
};

order = Order.save(order);
res.json(order);
}, () => {
res.json({});
});
});

app.get('/api/orders', (req, res) => {
res.json(Order.getAll());
});

app.listen(3000, () => {
console.log('Server load');
});
17 changes: 17 additions & 0 deletions node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "nodePHPTest",
"version": "0.0.0",
"private": true,
"dependencies": {
"body-parser": "^1.0.0",
"compression": "^1.6.1",
"express": ">4.2.0",
"express-load": "^1.1.15",
"express-session": "^1.13.0",
"forever-monitor": "^1.7.0",
"method-override": "^2.3.5",
"moment": "^2.22.2",
"request": "^2.88.0",
"request-promise": "^4.2.2"
}
}
21 changes: 21 additions & 0 deletions php/brl-usd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';
use \NodePHPTest\BRLExchange;
$app = new Silex\Application();

$app->get('/', function() use ($app) {
$price = BRLExchange::getDollarPrice();

if(!empty($price->value)) {
return $app->json([
'brl' => 1,
'usd' => (float) number_format($price->value[0]->cotacaoVenda, 2, '.', '')
]);
}
else {
return $app->json([]);
}
});

$app->run();
13 changes: 13 additions & 0 deletions php/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"require": {
"silex/silex": "^2.2",
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-4" : {
"NodePHPTest\\" : [
"src/"
]
}
}
}
30 changes: 30 additions & 0 deletions php/orders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use \NodePHPTest\Order;
use \NodePHPTest\BRLExchange;
$app = new Silex\Application();

$app->post('/', function(Request $request) use ($app) {
$price = BRLExchange::getDollarPrice();
$items = $request->get('items');
$total = number_format(array_sum($items), 2, '.', '');
$totalSellPrice = number_format($total * $price->value[0]->cotacaoVenda, 2, '.','');

$order = [
'created_at' => date('Y-m-d H:i:s'),
'total_brl' => (float)$total,
'total_usd' => (float)$totalSellPrice
];

Order::save($order);
return $app->json($order);
});

$app->get('/', function() use ($app) {
$orders = Order::getAll();
return $app->json($orders);
});

$app->run();
26 changes: 26 additions & 0 deletions php/src/BRLExchange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace NodePHPTest;
class BRLExchange
{
protected static $urlAPI = 'https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=@dataCotacao)';

public static function getDollarPrice()
{
try {
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', SELF::$urlAPI, [
'query' => [
'@dataCotacao' => "'07-20-2018'",
'$top' => '100',
'$format' => 'json'
]
]);

$price = json_decode($response->getBody()->getContents());
return $price;
}
catch(\Exception $e) {
die();
}
}
}
35 changes: 35 additions & 0 deletions php/src/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace NodePHPTest;

class Order
{
public static function save($order)
{

$nextID = 1;
$orders = [];
$file = file_get_contents(__DIR__ . '/../../orders.json');

if(!empty($file)) {
$orders = json_decode(($file));
$nextID = count($orders) + 1;
}

$order['id'] = $nextID;
$orders[] = $order;
file_put_contents(__DIR__ . '/../../orders.json', json_encode($orders), LOCK_EX);
}

public static function getAll()
{
$orders = [];
$file = file_get_contents(__DIR__ . '/../../orders.json');

if(!empty($file)) {
$orders = json_decode(($file));
}

return $orders;
}
}
17 changes: 14 additions & 3 deletions pratico.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```js
// Resposta

Array.prototype.last = () => { return this[this.length - 1]; };

// Teste/Exemplos
const array1 = [1,2,3,4,5,6,7,8,9]
Expand Down Expand Up @@ -157,7 +157,18 @@ function login($username, $password) {
```

```php
// Resposta
function login($username, $password) {
$sql = "
select *
from users
where username = :username AND password = :password
";

$p_sql = $pdo->prepare($sql);
$p_sql->execute([':username' => $username, ':password' => $password]);
return $p_sql->fetch(PDO::FETCH_ASSOC);
}

```

---
Expand Down Expand Up @@ -240,4 +251,4 @@ Content-Type: applicaton/json
"totalBRL": 1650,
"totalUSD": 435.84
}]
```
```
4 changes: 2 additions & 2 deletions pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Node
Versão escolhida: 6.x | 8.x
Versão escolhida: 8.11

# PHP
Versão escolhida: 5.4 | 5.5 | 5.6 | 5.7 | 7.0 | 7.1 | 7.2
Versão escolhida: 5.6
Loading