From cc7386a06523fd99e3f075cb2a160f58e31ab580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Ricoy?= Date: Sat, 25 Aug 2018 17:59:19 -0300 Subject: [PATCH 01/10] Update teorico.md --- teorico.md | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/teorico.md b/teorico.md index 8900235..85a95bd 100644 --- a/teorico.md +++ b/teorico.md @@ -2,33 +2,35 @@ 1\) Qual a diferença do operador `==` para o operador `===` em JavaScript? -[Resposta] +O operador '===' além de verificar se os valores são iguais, ele também verifica se são do mesmo tipo, já o operador '==' apenas verificas se os valores são iguais, sem considerar a tipagem. + 1.1) Dê 2 exemplos de quando os operadores produziriam resultados diferentes -```js -// Resposta -``` +'1' == 1; // true +'1' === 1; // false +undefined == false; // true +undefined === false; // false --- 2\) Qual recurso javascript é mais recomendado para tratar chamadas asíncronas? -[Resposta] +O recurso mais recomendado para tratar chamadas assíncronas são as Promises. 2.1) Justifique -[Resposta] +Com o uso das Promises, é possível controlar o fluxo de execução do código javascript, permitindo decidir o que será retornado caso uma Promise seja aceita ou rejeitada executar o callback correto através do método then() da Promise que pode inclusive ser encadeado a outros métodos then(). --- 3\) Existem threads em Node? -[Resposta] +Node é single thread. 3.1) Explique -[Resposta] +Node trabalha com single thread não bloqueante e executa funções de maneiras assíncronas através de um event-loop que recebe todas as requisições. Porém é possível destribuir essas requisições para novos processos através do uso de clusterização. --- @@ -51,7 +53,7 @@ getUserByName('jonh doe') .then(user => console.log(user)) ``` -[Resposta] +Resultado: undefined 4.2) ```js @@ -76,9 +78,7 @@ getData() }) ``` -``` -[Resposta] -``` +Resultado : second --- @@ -86,12 +86,18 @@ getData() 1\) Qual a diferença do operador `==` para o operador `===` em PHP? -[Resposta] +O operador '===' além de verificar se os valores são iguais, ele também verifica se são do mesmo tipo, já o operador '==' apenas verificas se os valores são iguais, sem considerar a tipagem. + 1.1) Dê 2 exemplos de quando os operadores produziriam resultados diferentes -```php -// Resposta +``` +$a = '1'; +$a == 1; // true +$a === 1; // false +$a == true; // true +$a === true; // false + ``` --- @@ -146,4 +152,4 @@ class B extends A { B::test(); ``` -[Resposta] \ No newline at end of file +[Resposta] From 602fa22b6aa22c27287dcb5e5bf1af045b30dc3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Ricoy?= Date: Sat, 25 Aug 2018 18:19:07 -0300 Subject: [PATCH 02/10] Update teorico.md --- teorico.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/teorico.md b/teorico.md index 85a95bd..549d135 100644 --- a/teorico.md +++ b/teorico.md @@ -104,17 +104,17 @@ $a === true; // false 2\) Qual a função do apache ou nginx em uma aplicação PHP? -[Resposta] +A função do apache / nginx é criar um servidor web para receber as requisições HTTP, buscar os arquivos necessários e / ou executar scripts PHP e devolver o html ao cliente. --- 3\) Existem threads em PHP? -[Resposta] +Sim, PHP é uma linguagem multi thread. 3.1) Explique -[Resposta] +PHP suporta processamento em paralelo através do uso da extensão pthreads. --- @@ -129,10 +129,10 @@ class Test { echo Test::prop; ``` -[Resposta] +1337 4.2) -```js +```php class A { public static function foo() { return 'bar'; @@ -152,4 +152,4 @@ class B extends A { B::test(); ``` -[Resposta] +'bar' From 949dfd38103e49cfa68d93ae4ab3c6038ec921cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Ricoy?= Date: Sat, 25 Aug 2018 18:57:55 -0300 Subject: [PATCH 03/10] Update pratico.md --- pratico.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pratico.md b/pratico.md index bf801f2..56cae62 100644 --- a/pratico.md +++ b/pratico.md @@ -4,7 +4,9 @@ ```js // Resposta - +Array.prototype.last = function() { + return this[this.length - 1]; +}; // Teste/Exemplos const array1 = [1,2,3,4,5,6,7,8,9] @@ -240,4 +242,4 @@ Content-Type: applicaton/json "totalBRL": 1650, "totalUSD": 435.84 }] -``` \ No newline at end of file +``` From 6dbd84a8bd03afe4f2dfaa1e669856ac37334873 Mon Sep 17 00:00:00 2001 From: romulo Date: Sat, 25 Aug 2018 23:43:08 -0300 Subject: [PATCH 04/10] initial version --- node/index.js | 43 +++++++++++++++++++++++++++++++++++++++++ node/package.json | 14 ++++++++++++++ php/brl-usd.php | 16 +++++++++++++++ php/composer.json | 13 +++++++++++++ php/orders.php | 30 ++++++++++++++++++++++++++++ php/src/BRLExchange.php | 26 +++++++++++++++++++++++++ php/src/Order.php | 35 +++++++++++++++++++++++++++++++++ pratico.md | 6 ++---- 8 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 node/package.json create mode 100644 php/composer.json create mode 100644 php/src/BRLExchange.php create mode 100644 php/src/Order.php diff --git a/node/index.js b/node/index.js index e69de29..9b1bfcf 100644 --- a/node/index.js +++ b/node/index.js @@ -0,0 +1,43 @@ +var express = require('express'), + bodyParser = require('body-parser'), + app = express(); + +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({extended: true})); + +app.get('/api/brl-usd', function(req, res) { + res.send({ + 'brl' : 1, + 'usd' : 4.4 + }); +}); + +app.post('/api/orders', function(req, res) { + res.send({ + "id": 1, + "createdAt": "...", + "totalBRL": 1650, + "totalUSD": 435.84 + }); +}); + +app.get('/api/orders', function(req, res) { + res.send([ + { + "id": 1, + "createdAt": "...", + "totalBRL": 1650, + "totalUSD": 435.84 + }, + { + "id": 2, + "createdAt": "...", + "totalBRL": 1650, + "totalUSD": 435.84 + } + ]); +}); + +app.listen(3000, function() { + console.log('Server load'); +}); \ No newline at end of file diff --git a/node/package.json b/node/package.json new file mode 100644 index 0000000..fc8265c --- /dev/null +++ b/node/package.json @@ -0,0 +1,14 @@ +{ + "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" + } +} diff --git a/php/brl-usd.php b/php/brl-usd.php index e69de29..6293d3b 100644 --- a/php/brl-usd.php +++ b/php/brl-usd.php @@ -0,0 +1,16 @@ +get('/', function() use ($app, $urlAPI) { + $price = BRLExchange::getDollarPrice(); + + return $app->json([ + 'brl' => 1, + 'usd' => (float) number_format($price->value[0]->cotacaoVenda, 2, '.', '') + ]); +}); + +$app->run(); \ No newline at end of file diff --git a/php/composer.json b/php/composer.json new file mode 100644 index 0000000..a3c0f52 --- /dev/null +++ b/php/composer.json @@ -0,0 +1,13 @@ +{ + "require": { + "silex/silex": "^2.2", + "guzzlehttp/guzzle": "^6.3" + }, + "autoload": { + "psr-4" : { + "NodePHPTest\\" : [ + "src/" + ] + } + } +} diff --git a/php/orders.php b/php/orders.php index e69de29..f9c0bd9 100644 --- a/php/orders.php +++ b/php/orders.php @@ -0,0 +1,30 @@ +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(); \ No newline at end of file diff --git a/php/src/BRLExchange.php b/php/src/BRLExchange.php new file mode 100644 index 0000000..4ce15e3 --- /dev/null +++ b/php/src/BRLExchange.php @@ -0,0 +1,26 @@ +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(); + } + } +} \ No newline at end of file diff --git a/php/src/Order.php b/php/src/Order.php new file mode 100644 index 0000000..1f98e0a --- /dev/null +++ b/php/src/Order.php @@ -0,0 +1,35 @@ + Date: Sun, 26 Aug 2018 15:55:11 -0300 Subject: [PATCH 05/10] initial version --- node/Order.js | 25 ++++++++++++++++++ node/index.js | 65 +++++++++++++++++++++++++++-------------------- node/package.json | 5 +++- php/src/Order.php | 6 ++--- 4 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 node/Order.js diff --git a/node/Order.js b/node/Order.js new file mode 100644 index 0000000..34a1881 --- /dev/null +++ b/node/Order.js @@ -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; + diff --git a/node/index.js b/node/index.js index 9b1bfcf..78bd017 100644 --- a/node/index.js +++ b/node/index.js @@ -1,43 +1,54 @@ -var express = require('express'), +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', function(req, res) { - res.send({ - 'brl' : 1, - 'usd' : 4.4 +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', function(req, res) { - res.send({ - "id": 1, - "createdAt": "...", - "totalBRL": 1650, - "totalUSD": 435.84 +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', function(req, res) { - res.send([ - { - "id": 1, - "createdAt": "...", - "totalBRL": 1650, - "totalUSD": 435.84 - }, - { - "id": 2, - "createdAt": "...", - "totalBRL": 1650, - "totalUSD": 435.84 - } - ]); +app.get('/api/orders', (req, res) => { + res.json(Order.getAll()); }); -app.listen(3000, function() { +app.listen(3000, () => { console.log('Server load'); }); \ No newline at end of file diff --git a/node/package.json b/node/package.json index fc8265c..bf7fcaf 100644 --- a/node/package.json +++ b/node/package.json @@ -9,6 +9,9 @@ "express-load": "^1.1.15", "express-session": "^1.13.0", "forever-monitor": "^1.7.0", - "method-override": "^2.3.5" + "method-override": "^2.3.5", + "moment": "^2.22.2", + "request": "^2.88.0", + "request-promise": "^4.2.2" } } diff --git a/php/src/Order.php b/php/src/Order.php index 1f98e0a..48c9452 100644 --- a/php/src/Order.php +++ b/php/src/Order.php @@ -9,7 +9,7 @@ public static function save($order) $nextID = 1; $orders = []; - $file = file_get_contents(__DIR__ . '/../orders.json'); + $file = file_get_contents(__DIR__ . '/../../orders.json'); if(!empty($file)) { $orders = json_decode(($file)); @@ -18,13 +18,13 @@ public static function save($order) $order['id'] = $nextID; $orders[] = $order; - file_put_contents(__DIR__ . '/../orders.json', json_encode($orders), LOCK_EX); + file_put_contents(__DIR__ . '/../../orders.json', json_encode($orders), LOCK_EX); } public static function getAll() { $orders = []; - $file = file_get_contents(__DIR__ . '/../orders.json'); + $file = file_get_contents(__DIR__ . '/../../orders.json'); if(!empty($file)) { $orders = json_decode(($file)); From df8ca44c3fb36cdc6c7c5ae3a173590e2df426dc Mon Sep 17 00:00:00 2001 From: romulo Date: Sun, 26 Aug 2018 16:13:37 -0300 Subject: [PATCH 06/10] initial version --- php/brl-usd.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/php/brl-usd.php b/php/brl-usd.php index 6293d3b..00afca2 100644 --- a/php/brl-usd.php +++ b/php/brl-usd.php @@ -4,13 +4,18 @@ use \NodePHPTest\BRLExchange; $app = new Silex\Application(); -$app->get('/', function() use ($app, $urlAPI) { +$app->get('/', function() use ($app) { $price = BRLExchange::getDollarPrice(); - return $app->json([ - 'brl' => 1, - 'usd' => (float) number_format($price->value[0]->cotacaoVenda, 2, '.', '') - ]); + if(!empty($price->value)) { + return $app->json([ + 'brl' => 1, + 'usd' => (float) number_format($price->value[0]->cotacaoVenda, 2, '.', '') + ]); + } + else { + return $app->json([]); + } }); $app->run(); \ No newline at end of file From 58366e4f2a26deffcc95d61dd9b16eca6e4b9699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Ricoy?= Date: Sun, 26 Aug 2018 16:27:02 -0300 Subject: [PATCH 07/10] Update pratico.md --- pratico.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pratico.md b/pratico.md index bf801f2..38d0a0d 100644 --- a/pratico.md +++ b/pratico.md @@ -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] @@ -240,4 +240,4 @@ Content-Type: applicaton/json "totalBRL": 1650, "totalUSD": 435.84 }] -``` \ No newline at end of file +``` From 09f3ed75531c58f66570e086755cc3bc1b2e0228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Ricoy?= Date: Sun, 26 Aug 2018 20:21:12 -0300 Subject: [PATCH 08/10] Update pull_request_template.md --- pull_request_template.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pull_request_template.md b/pull_request_template.md index c09ac93..a45d84d 100644 --- a/pull_request_template.md +++ b/pull_request_template.md @@ -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 \ No newline at end of file +Versão escolhida: 5.6 From 62f9bcc32fefff030e2e9b6a5e61d5f4f57f7496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Ricoy?= Date: Sun, 26 Aug 2018 20:37:56 -0300 Subject: [PATCH 09/10] Update pratico.md --- pratico.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pratico.md b/pratico.md index 38d0a0d..d59bbea 100644 --- a/pratico.md +++ b/pratico.md @@ -157,7 +157,17 @@ 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); + ``` --- From ea880199fdef2f5d2e402aa7d7f24667eb326ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Ricoy?= Date: Sun, 26 Aug 2018 20:38:48 -0300 Subject: [PATCH 10/10] Update pratico.md --- pratico.md | 1 + 1 file changed, 1 insertion(+) diff --git a/pratico.md b/pratico.md index d59bbea..89aa7d4 100644 --- a/pratico.md +++ b/pratico.md @@ -167,6 +167,7 @@ function login($username, $password) { $p_sql = $pdo->prepare($sql); $p_sql->execute([':username' => $username, ':password' => $password]); return $p_sql->fetch(PDO::FETCH_ASSOC); +} ```