commit fcb5c639bb1e7be5d4609a57988a444a0dc17795 Author: Francisco Pessano Date: Sun Nov 28 00:59:24 2021 -0300 Project ended diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49e0fc6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules +/package-lock.json \ No newline at end of file diff --git a/API/returnMultiplicationTables.js b/API/returnMultiplicationTables.js new file mode 100644 index 0000000..cbec3dc --- /dev/null +++ b/API/returnMultiplicationTables.js @@ -0,0 +1,78 @@ +const express = require('express') +const app = express() + +const port = 3001 + +app.use(express.json()) + +app.get('/', (req, res) => { + + const error = (err) => { + + res.status('400').json({ + + 'status': 'error', + 'message': err + }) + } + + const data = req.body + + if (!data) { + + error(`Data not sended`) + return + } + + //!Template + /* + + { + "numberToMultiply" = 5, + "initialNumber" = 0, + } + */ + + const numberToMultiply = data.numberToMultiply + let initialNumber = data.initialNumber + let limit = data.limit + + if (!data.hasOwnProperty("numberToMultiply")) { + + error(`Number to multiply don't defined`) + return + } + + if (typeof(numberToMultiply) !== 'number') { + + error(`Number to multiply is not a number`) + return + } + + if (!data.hasOwnProperty("initialNumber") || typeof(initialNumber) !== 'number' || initialNumber < 0) { + + initialNumber = 0 + } + + if (!data.hasOwnProperty("limit") || typeof(limit) !== 'number' || limit < 0 || limit > 999) { + + limit = 10 + } + + let result = [] + let i = initialNumber + + while (i <= (initialNumber + limit)) { + + result.push(`${numberToMultiply} x ${i} = ${numberToMultiply * i}`) + i++ + } + + res.json({ + + 'status': 'success', + 'message': result + }) +}) + +app.listen(port) \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..9dfa4ce --- /dev/null +++ b/app.js @@ -0,0 +1,106 @@ +const url = 'http://localhost:3001' + +const axios = require('axios') +const colors = require('colors') +const inquirer = require('inquirer') + +const getData = async (numberToMultiply, initialNumber, limit) => { + try { + + const response = await axios({ + method: 'get', + url: url, + + data: { + "numberToMultiply": numberToMultiply, + "initialNumber": initialNumber, + "limit": limit + } + }) + + return await response.data + + } catch (error) { + + return await error.response.data + } +} + +function errorMessage(message) { + + console.log(message.brightRed) +} + +function titleMessage(message) { + + console.log() + console.log(message.bold.cyan) + console.log() +} + +function commonMessage(message) { + + console.log(message.brightGreen) +} + +function thanksForUsingIt() { + + console.log('Thanks for use this tool. Francisco Pessano.'.bold.brightYellow) +} + +inquirer + .prompt([{ + + name: 'numberToMultiplySelection', + message: 'Put the number to find your multiplication table.', + type: 'number', + + default: 10 + }, + { + name: 'initialNumberSelection', + message: 'Put the initial number of the table.', + type: 'number', + + default: 0 + }, + { + name: 'limitSelection', + message: 'Put a limit (max 999)', + type: 'number', + + default: 10 + }]) + + .then(async (answers) => { + + const numberToMultiply = answers.numberToMultiplySelection + const initialNumber = answers.initialNumberSelection + const limit = answers.limitSelection + + const data = await getData(numberToMultiply, initialNumber, limit) + // data.status + // data.message + + if (data.status === 'error') { + + errorMessage(data.message) + return + } + + if (data.status !== 'success') { + + return + } + + titleMessage('Here are your results:') + + data.message.forEach(count => { + + commonMessage(` - ` + count) + }); + + console.log() + thanksForUsingIt() + console.log() + }) \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..121a976 --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "mostrar-tablas-de-multiplicar-cli", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "api": "nodemon ./API/returnMultiplicationTables.js", + "game": "node app.js" + }, + "author": "FranP-Code", + "license": "ISC", + "devDependencies": { + "nodemon": "^2.0.15" + }, + "dependencies": { + "axios": "^0.24.0", + "colors": "^1.4.0", + "express": "^4.17.1", + "inquirer": "^8.2.0" + } +}