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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node/node_modules/
php/vendor/
173 changes: 173 additions & 0 deletions node/arrayClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
"use strict"

class Array {

constructor( elements ) {

this.setElements( elements )
}

setElements( elements )
{
if( elements != null ) {
this.elements = elements
}else {
this.elements = []
}
}

last( ) {

if( this.elements.length > 0 ) {
return this.elements[ this.elements.length - 1 ]
} else {
return undefined;
}
}
}

let ar1 = new Array( )
let ar2 = new Array( [1, 2, 3, 4, 5] )

console.log( ar1.last( ) ) //undefined
console.log( ar2.last( ) ) //5

function getTransactions() {
return new Promise((resolve, reject) => {
fetch(BASE_URL + '/api/transacoes')
.then(result => {
result.json().then(transactions_result => {
const transactions = []

for (let i in transactions_result) {
if (transactions_result[i].realizada) {
transactions.push({
id: transactions_result[i].id,
value: transactions_result[i].valor,
type: transactions_result[i].valor < 0 ? 'transference' : 'deposit',
})
}
}

return resolve(transactions)
})
})
.catch(e => {
return reject(e)
})
})
}



class ResponseTransaction {

constructor( elements ) {

this.json( )
}

json( ) {

this.elements =

[
{ id: 1, valor: -1, realizada: true },
{ id: 1, valor: 50, realizada: true },
{ id: 1, valor: 181, realizada: false }
]

return this.elements

}

}


const BASE_URL = 'http://teste.desenvolvimento.nodejs';

function fetch( url ) {

console.log( url )
return new Promise((resolve, reject) => {

const response_transaction = new ResponseTransaction( );
//console.log( response_transaction.json( ) )
return resolve(response_transaction);

});

}

function resolvePromiseFetchResult( result ) {


return new Promise((resolve, reject) => {

if( !result ) {

return reject( "Invalid result" )
}

return resolve( result.json() )
});
}

function buildArraySuccessfully( arrayTransactionsResult ) {



return new Promise((resolve, reject) => {

if( !arrayTransactionsResult ) {

return reject( "Invalid array transaction result" )

}

let transactions = []

for (let i in arrayTransactionsResult) {

if (arrayTransactionsResult[i].realizada) {
transactions.push({
id: arrayTransactionsResult[i].id,
value: arrayTransactionsResult[i].valor,
type: arrayTransactionsResult[i].valor < 0 ? 'transference' : 'deposit',
})
}
}

return resolve(transactions)

});

}



function getTransactions2( ) {

return new Promise((resolve, reject) => {

const url = BASE_URL + '/api/transacoes';

return resolve( url )

});

}


getTransactions2( )
.then( fetch )
.then( resolvePromiseFetchResult )
.then( buildArraySuccessfully )
.then(
(result) => {
console.log(result);
},

(error) => {
console.log(`ocorreu o seguinte erro: [ ${error} ]`);
});
14 changes: 14 additions & 0 deletions node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const express = require( 'express' )
const bodyParser = require( 'body-parser' )
const morgan = require( 'morgan' )
const app = express( )

app.use( bodyParser.urlencoded( { extended : false } ) )
app.use( bodyParser.json( ) )
app.use( morgan( 'dev' ) )

require( './src/index' )( app )

app.listen( 9000, ( ) => {
console.log( 'Express has been started' )
} )
17 changes: 17 additions & 0 deletions node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.3",
"morgan": "^1.9.0",
"node-cache": "^4.2.0"
}
}
144 changes: 144 additions & 0 deletions node/src/business/cache-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
"use strict"

const nodeCache = require( "node-cache" )
const cacheManager = new nodeCache( )

class CacheManager {


constructor( dollarQuotation ) {

this.dollarQuotation = dollarQuotation

}

recoverDataDollarQuotation( ) {


return new Promise((resolve, reject) => {

cacheManager.get( "dollar_quotation", ( err, value ) => {

if( !err ){

return resolve( value )

}else {

return reject ( err )

}

} )

});

}
getDataDollarQuotation( value ) {


return new Promise((resolve, reject) => {


cacheManager.get( "dollar_quotation", ( err, value ) => {

if( !err ){

if(value == undefined){

this.dollarQuotation.requestQuotationBcb( )
.then( this.dollarQuotation.proccessDataQuotation )
.then( this.dollarQuotation.calculateDollarQuotation )
.then(
(result) => {

cacheManager.set( "dollar_quotation", result, 14400, ( err, success) => {
if( !err && success ){

return resolve( result )
}
});
},

(error) => {
console.log(`ocorreu o seguinte erro: [ ${error} ]`);
})

}else{

return resolve( value )
}

}else {

return reject ( err )

}

} )




});

}

getDataOrdersCache( ) {

return new Promise((resolve, reject) => {


cacheManager.get( "orders", ( err, value ) => {

if( !err ){

if(value == undefined){

return resolve( [ ] )

}else{

return resolve( value )
}

}else {

return reject ( err )

}

} )




});

}

recordDataOrdersCache( dataOrders ) {

return new Promise((resolve, reject) => {
cacheManager.set( "orders", dataOrders, 14400, ( err, success) => {
if( !err && success ){

return resolve( success )
}else {
reject( "Error to record orders" )
}

});




} )




}
}

module.exports = CacheManager
Loading