From fc6a305e271cfa244048d8611f9a696442541647 Mon Sep 17 00:00:00 2001 From: Luciano Chiba Date: Mon, 7 Oct 2019 10:57:50 -0300 Subject: [PATCH] Refactored --- controllers/index.js | 156 ++++++++++++++++++++++++++++++++++++++ index.js | 176 +++---------------------------------------- quotes.db | 4 + 3 files changed, 171 insertions(+), 165 deletions(-) create mode 100644 controllers/index.js diff --git a/controllers/index.js b/controllers/index.js new file mode 100644 index 0000000..9899ebb --- /dev/null +++ b/controllers/index.js @@ -0,0 +1,156 @@ +var fs = require('fs'); + +const Home = (req, res) => { + fs.readFile('quotes.db', function (err, buf) { + var qarr = JSON.parse(buf); + var random = Math.floor(Math.random() * (qarr.length - 1)); + res.setHeader('Access-Control-Allow-Origin', '*'); + // Request methods you wish to allow + res.setHeader( + 'Access-Control-Allow-Methods', + 'GET, POST, OPTIONS, PUT, PATCH, DELETE' + ); + + // Request headers you wish to allow + res.setHeader( + 'Access-Control-Allow-Headers', + 'X-Requested-With,content-type' + ); + + // Set to true if you need the website to include cookies in the requests sent + // to the API (e.g. in case you use sessions) + res.setHeader('Access-Control-Allow-Credentials', true); + res.send(qarr[random]); + }); +} + +const Categories = (req, res) => { + fs.readFile('quotes.db', function (err, buf) { + var query = []; + var seenNames = {}; + var i = 0; + var qarr = JSON.parse(buf).map(function (n) { + if (n.category in seenNames) { + return null; + } + else { + seenNames[n.category] = true; + query[i++] = n.category; + return n.category; + } + + }); + //console.log(seenNames); + res.setHeader('Access-Control-Allow-Origin', '*'); + // Request methods you wish to allow + res.setHeader('Access-Control-Allow-Methods', 'GET'); + + // Request headers you wish to allow + res.setHeader( + 'Access-Control-Allow-Headers', + 'X-Requested-With,content-type' + ); + + // Set to true if you need the website to include cookies in the requests sent + // to the API (e.g. in case you use sessions) + res.setHeader('Access-Control-Allow-Credentials', true); + res.send(JSON.stringify(query)); + }); +} + +const Author = (req, res) => { + if ( + Object.entries(req.query).length !== 0 && + req.query.constructor === Object + ) { + let authorName = Object.keys(req.query); + authorName = authorName.map(author => author.toLowerCase())[0]; + let dbData; + let authorQuotes = []; + fs.readFile('quotes.db', function (err, buf) { + dbData = JSON.parse(buf); + for (var i = 0; i < dbData.length; i++) { + if (dbData[i]['author'].toLowerCase().indexOf(authorName) != -1) { + authorQuotes.push(dbData[i]); + } + } + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'GET'); + res.setHeader( + 'Access-Control-Allow-Headers', + 'X-Requested-With,content-type' + ); + res.setHeader('Access-Control-Allow-Credentials', true); + res.send(JSON.stringify(authorQuotes)); + }); + return; + } + fs.readFile('quotes.db', function (err, buf) { + var query = []; + var seenNames = {}; + var i = 0; + var qarr = JSON.parse(buf).map(function (n) { + if (n.author in seenNames) { + return null; + } + else { + seenNames[n.author] = true; + query[i++] = n.author; + return n.author; + } + + }); + //console.log(seenNames); + res.setHeader('Access-Control-Allow-Origin', '*'); + // Request methods you wish to allow + res.setHeader('Access-Control-Allow-Methods', 'GET'); + + // Request headers you wish to allow + res.setHeader( + 'Access-Control-Allow-Headers', + 'X-Requested-With,content-type' + ); + + // Set to true if you need the website to include cookies in the requests sent + // to the API (e.g. in case you use sessions) + res.setHeader('Access-Control-Allow-Credentials', true); + res.send(JSON.stringify(query)); + }); +} + +const Quotes = (req, res) => { + fs.readFile('quotes.db', function (err, buf) { + var qarr = JSON.parse(buf).filter(function (n) { + return n.category === req.params.id; + }); + //console.log(qarr.length); + var random = Math.floor(Math.random() * (qarr.length)); + res.setHeader('Access-Control-Allow-Origin', '*'); + // Request methods you wish to allow + res.setHeader( + 'Access-Control-Allow-Methods', + 'GET, POST, OPTIONS, PUT, PATCH, DELETE' + ); + + // Request headers you wish to allow + res.setHeader( + 'Access-Control-Allow-Headers', + 'X-Requested-With,content-type' + ); + + // Set to true if you need the website to include cookies in the requests sent + // to the API (e.g. in case you use sessions) + res.setHeader('Access-Control-Allow-Credentials', true); + if (qarr[random] == undefined) { + res.send({}); + } + else res.send(qarr[random]); + }); +} + +module.exports = { + Home, + Categories, + Author, + Quotes +} \ No newline at end of file diff --git a/index.js b/index.js index 860bca1..473527c 100644 --- a/index.js +++ b/index.js @@ -1,177 +1,23 @@ -//Routing Mechanism - URL Define Here. const express = require('express'); -//Object of Express +const PORT = process.env.PORT || 3000 const app = express(); -app.use(express.static('views')); - -//File Stream to Read Quotes from file -//I am not using db faster execution :p -var fs = require('fs'); - -app.get('/', (req, res) => { - fs.readFile('quotes.db', function(err, buf) { - var qarr = JSON.parse(buf); - var random = Math.floor(Math.random() * (qarr.length - 1)); - res.setHeader('Access-Control-Allow-Origin', '*'); - // Request methods you wish to allow - res.setHeader( - 'Access-Control-Allow-Methods', - 'GET, POST, OPTIONS, PUT, PATCH, DELETE' - ); - - // Request headers you wish to allow - res.setHeader( - 'Access-Control-Allow-Headers', - 'X-Requested-With,content-type' - ); - - // Set to true if you need the website to include cookies in the requests sent - // to the API (e.g. in case you use sessions) - res.setHeader('Access-Control-Allow-Credentials', true); - res.send(qarr[random]); - }); -}); - -app.get('/categories', (req, res) => { - fs.readFile('quotes.db', function(err, buf) { - var query = []; - var seenNames = {}; - var i=0; - var qarr = JSON.parse(buf).map(function(n) { - if(n.category in seenNames) - { - return null; - } - else - { - seenNames[n.category] = true; - query[i++]=n.category ; - return n.category; - } - - }); - //console.log(seenNames); - res.setHeader('Access-Control-Allow-Origin', '*'); - // Request methods you wish to allow - res.setHeader('Access-Control-Allow-Methods', 'GET'); - - // Request headers you wish to allow - res.setHeader( - 'Access-Control-Allow-Headers', - 'X-Requested-With,content-type' - ); - - // Set to true if you need the website to include cookies in the requests sent - // to the API (e.g. in case you use sessions) - res.setHeader('Access-Control-Allow-Credentials', true); - res.send(JSON.stringify(query)); - }); -}); - -app.get('/author', (req, res) => { - if ( - Object.entries(req.query).length !== 0 && - req.query.constructor === Object - ) { - let authorName = Object.keys(req.query); - authorName = authorName.map(author => author.toLowerCase())[0]; - let dbData; - let authorQuotes = []; - fs.readFile('quotes.db', function(err, buf) { - dbData = JSON.parse(buf); - for (var i = 0; i < dbData.length; i++) { - if (dbData[i]['author'].toLowerCase().indexOf(authorName) != -1) { - authorQuotes.push(dbData[i]); - } - } - res.setHeader('Access-Control-Allow-Origin', '*'); - res.setHeader('Access-Control-Allow-Methods', 'GET'); - res.setHeader( - 'Access-Control-Allow-Headers', - 'X-Requested-With,content-type' - ); - res.setHeader('Access-Control-Allow-Credentials', true); - res.send(JSON.stringify(authorQuotes)); - }); - return; - } - fs.readFile('quotes.db', function(err, buf) { - var query = []; - var seenNames = {}; - var i=0; - var qarr = JSON.parse(buf).map(function(n) { - if(n.author in seenNames) - { - return null; - } - else - { - seenNames[n.author] = true; - query[i++]=n.author ; - return n.author; - } - - }); - //console.log(seenNames); - res.setHeader('Access-Control-Allow-Origin', '*'); - // Request methods you wish to allow - res.setHeader('Access-Control-Allow-Methods', 'GET'); +const Controller = require('./controllers') - // Request headers you wish to allow - res.setHeader( - 'Access-Control-Allow-Headers', - 'X-Requested-With,content-type' - ); - - // Set to true if you need the website to include cookies in the requests sent - // to the API (e.g. in case you use sessions) - res.setHeader('Access-Control-Allow-Credentials', true); - res.send(JSON.stringify(query)); - }); -}); - - -app.get('/:id', (req, res) => { - fs.readFile('quotes.db', function(err, buf) { - var qarr = JSON.parse(buf).filter(function(n) { - return n.category === req.params.id; - }); - //console.log(qarr.length); - var random = Math.floor(Math.random() * (qarr.length)); - res.setHeader('Access-Control-Allow-Origin', '*'); - // Request methods you wish to allow - res.setHeader( - 'Access-Control-Allow-Methods', - 'GET, POST, OPTIONS, PUT, PATCH, DELETE' - ); - - // Request headers you wish to allow - res.setHeader( - 'Access-Control-Allow-Headers', - 'X-Requested-With,content-type' - ); +app.set('view engine', 'ejs'); +app.use(express.static('views')); - // Set to true if you need the website to include cookies in the requests sent - // to the API (e.g. in case you use sessions) - res.setHeader('Access-Control-Allow-Credentials', true); - if(qarr[random]==undefined) - { - res.send({}); - } - else res.send(qarr[random]); - }); -}); +app + .get('/', Controller.Home) + .get('/categories', Controller.Categories) + .get('/author', Controller.Author) + .get('/:id', Controller.Quotes) -//Httpserver Port Number 3000. -app.listen(process.env.PORT || 3000, function() { +app.listen(PORT, () => { console.log( 'Express server listening on port %d in %s mode', this.address().port, app.settings.env ); -}); - -//Set The File Type To App As EJS. -app.set('view engine', 'ejs'); +}); \ No newline at end of file diff --git a/quotes.db b/quotes.db index 33dcda6..af17c1f 100644 --- a/quotes.db +++ b/quotes.db @@ -22083,5 +22083,9 @@ { "quote": "I'm not obsessed with sex, I just can't stop thinking about it. The performance of it. The awkwardness of it. The drama of it. The moment you realise someone wants your body. Not so much the feeling of it.", "author": "Fleabag" + }, + { + "quote": "Creio no riso e nas lágrimas como antídotos contra o ódio e o terror.", + "author": "Charles Chaplin" } ]