diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..45aef93a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.env +/node_modules +/.idea +yarn.lock +package-lock.json diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 00000000..376bd81a --- /dev/null +++ b/doc/README.md @@ -0,0 +1,29 @@ +# NODE JS + + +## Para rodar este projeto +```bash +$ npm install +$ node run start +``` +Acesssar pela url: http://localhost:8000/ #neste caso a porta esta a padrão, mais pode ser alterada se necessário, PS: o link para acesso é mostrado quando o comardo anterior é executado. + + + +## Acesso +- Utilizando o POSTMAN, Insomnia ou alguma outra ferramente do tipo, deve ser feita uma requisição 'POST'. +- url: http://localhost:3000, passando como parametro obrigatório uma matriz quadrada. +- Variável referente a matriz que deve ser passada na requisição deve-se chamar 'matrix'. + +## Exemplo da Variável +{ + "matrix": [ + [1, 2, 3, 6], + [4, 5, 6, 3], + [9, 5, 7, 0], + [4, 8, 9, 0] + ] +} + +## Observação +- A aplicação esta mapeada para a porta 3000. diff --git a/package.json b/package.json new file mode 100644 index 00000000..8c20069e --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "backend-challenge", + "version": "1.0.0", + "description": "Esse repositório é o nosso desafio para avaliar o quão bom desenvolvedor back-end você é.", + "main": "src/server.js", + "dependencies": { + "body-parser": "^1.18.3", + "express": "^4.15.4", + "express-validator": "^6.9.2", + "mongoose": "^5.0.16", + "redis": "^2.8.0" + }, + "devDependencies": { + "nodemon": "^1.17.3" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "nodemon" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/jlaurosouza/backend-challenge.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/jlaurosouza/backend-challenge/issues" + }, + "homepage": "https://github.com/jlaurosouza/backend-challenge#readme" +} diff --git a/src/app.js b/src/app.js new file mode 100644 index 00000000..ba22b4d1 --- /dev/null +++ b/src/app.js @@ -0,0 +1,16 @@ +const express = require('express'); +const app = express(); +const bodyParser = require('body-parser'); + +const router = express.Router(); + +//Rotas +const index = require('./routes/index'); +const homeRoute = require('./routes/homeRoute'); + +app.use(bodyParser.urlencoded({ extended: true })) +app.use(bodyParser.json()) + +app.use('/', homeRoute); + +module.exports = app; \ No newline at end of file diff --git a/src/controllers/homeController.js b/src/controllers/homeController.js new file mode 100644 index 00000000..7d9c6e26 --- /dev/null +++ b/src/controllers/homeController.js @@ -0,0 +1,33 @@ +exports.get = (req, res, next) => { + res.status(200).send('utiliza a rota post'); +}; + +exports.post = (req, res, next) => { + try { + const { body } = req; + const { matrix } = body; + + let lengthMatrix = matrix.length; + let diagonalLeftToRight = 0; + let diagonalRightToLeft = 0; + + for(let i = 0; i < matrix.length; i++){ + diagonalLeftToRight = diagonalLeftToRight + matrix[i][i]; + diagonalRightToLeft = diagonalRightToLeft + matrix[i][--lengthMatrix]; + } + + const diff = diagonalLeftToRight - diagonalRightToLeft; + + + return res.json({ + success: 200, + data: `A diferença da matriz é ${diff}` + }); + } catch (e) { + console.log(e) + return res.json({ + error: 400, + data: 'Não foi possível identificar a diferença da matriz.' + }); + } +} \ No newline at end of file diff --git a/src/helpers/validatorHelper.js b/src/helpers/validatorHelper.js new file mode 100644 index 00000000..df825b5b --- /dev/null +++ b/src/helpers/validatorHelper.js @@ -0,0 +1,12 @@ +const validationResult = require('express-validator').validationResult; + +module.exports = { + check(req, res, next) { + const errors = validationResult(req); + + if (!errors.isEmpty()) { + return res.status(400).json({ message: errors.array() }); + } + return next(); + } +} diff --git a/src/routes/homeRoute.js b/src/routes/homeRoute.js new file mode 100644 index 00000000..0d0a6ddc --- /dev/null +++ b/src/routes/homeRoute.js @@ -0,0 +1,10 @@ +const express = require('express'); +const router = express.Router(); +const homeController = require('../controllers/homeController'); +const homeValidator = require('../validators/homeValidator'); +const ValidatorHelper = require('../helpers/validatorHelper'); + +router.get('/', homeController.get); +router.post('/', homeValidator.postRules, ValidatorHelper.check, homeController.post); + +module.exports = router; \ No newline at end of file diff --git a/src/routes/index.js b/src/routes/index.js new file mode 100644 index 00000000..8d4c81d4 --- /dev/null +++ b/src/routes/index.js @@ -0,0 +1,12 @@ + +const express = require('express'); +const router = express.Router(); + +router.get('/', function (req, res, next) { + res.status(200).send({ + title: "Node Express API", + version: "0.0.1" + }); +}); + +module.exports = router; diff --git a/src/server.js b/src/server.js new file mode 100644 index 00000000..fd3c91cf --- /dev/null +++ b/src/server.js @@ -0,0 +1,20 @@ +const app = require('./app'); + +const port = normalizaPort(process.env.PORT || '3000'); + +function normalizaPort(val) { + const port = parseInt(val, 10); + if (isNaN(port)) { + return val; + } + + if (port >= 0) { + return port; + } + + return false; +} + +app.listen(port, function () { + console.log(`app listening on port ${port}`) +}) \ No newline at end of file diff --git a/src/validators/homeValidator.js b/src/validators/homeValidator.js new file mode 100644 index 00000000..abad7f5d --- /dev/null +++ b/src/validators/homeValidator.js @@ -0,0 +1,19 @@ +const check = require('express-validator').check; + +module.exports = { + postRules:[ + check('matrix') + .exists().withMessage("Por favor informa uma matriz quadrada") + .custom((matrix) => { + return new Promise((resolve, reject) => { + const lengthMatrix = matrix.length; + matrix.map(mat => { + if (mat.length != lengthMatrix){ + reject(); + } + }) + resolve(); + }); + }).withMessage("A matriz não é quadrada, favor nformar uma outra matriz") + ] +};