Skip to content

Commit

Permalink
🔥 implemented serach by category feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tanwanimohit committed Oct 6, 2019
1 parent 03dac49 commit aaf3c37
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -3,6 +3,14 @@ Random Quotes generator Machine / API

This is a Random Quoutes Generator API, Developed in Node.js. I am maintaining a json file named `quotes.db` which is working like a database to get the Random Quote.

## How to Use?

- To Get Random Quote : Send Get Request to : `https://freequote.herokuapp.com/`
- To Get All Authors : Send Get Request to : `https://freequote.herokuapp.com/authors`
- To Get Quotes of Specific Author : `https://freequote.herokuapp.com/authors?authorname`
- To Get All Categories : Send Get Request to : `https://freequote.herokuapp.com/categories`
- To Get Quote of Specific Category : `https://freequote.herokuapp.com/categoryname`

## How to Contribute ?

- Fork the Repo / Download the Repo
Expand Down
79 changes: 68 additions & 11 deletions index.js
Expand Up @@ -33,18 +33,28 @@ app.get('/', (req, res) => {
});
});

app.get('/got', (req, res) => {
app.get('/categories', (req, res) => {
fs.readFile('quotes.db', function(err, buf) {
var qarr = JSON.parse(buf).filter(function(n) {
return n.category === 'got';
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;
}

});
var random = Math.floor(Math.random() * (qarr.length - 1));
//console.log(seenNames);
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE'
);
res.setHeader('Access-Control-Allow-Methods', 'GET');

// Request headers you wish to allow
res.setHeader(
Expand All @@ -55,10 +65,11 @@ app.get('/got', (req, res) => {
// 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]);
res.send(JSON.stringify(query));
});
});


app.get('/author', (req, res) => {
if (
Object.entries(req.query).length !== 0 &&
Expand Down Expand Up @@ -87,9 +98,23 @@ app.get('/author', (req, res) => {
return;
}
fs.readFile('quotes.db', function(err, buf) {
var query = [];
var seenNames = {};
var i=0;
var qarr = JSON.parse(buf).map(function(n) {
return n.author;
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');
Expand All @@ -103,7 +128,39 @@ app.get('/author', (req, res) => {
// 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(qarr));
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'
);

// 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]);
});
});

Expand Down
5 changes: 5 additions & 0 deletions quotes.db
Expand Up @@ -21901,6 +21901,11 @@
"author": "Robert C. Martin",
"category": "programming"
},
{
"quote": "Only Programmers can Cntr+s The World.",
"author": "Unknown Programmer",
"category": "programming"
},
{
"quote": "The most important property of a program is whether it accomplishes the intention of its user.",
"author": "C.A.R. Hoare",
Expand Down

0 comments on commit aaf3c37

Please sign in to comment.