Skip to content

Commit

Permalink
Add search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTimeey committed May 17, 2021
1 parent 5610640 commit 6b31dd7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
12 changes: 5 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const wordRouter = require('./src/routers/word.js');
const batchWordRouter = require('./src/routers/batch.js');
const randomWordRouter = require('./src/routers/random.js');
const { clientApiKeyValidation } = require('./src/common/authUtils');

const app = express();
Expand All @@ -17,17 +14,18 @@ const router = express.Router();
router.use(clientApiKeyValidation);

router.use(function(req, res, next) {
console.log('Something is happening!');
/*console.log('Something is happening!');*/
next();
});

router.get('/', function(req, res) {
res.json({ message: 'Hooray! Welcome to the API!' });
});

router.use('/words', wordRouter);
router.use('/batch', batchWordRouter);
router.use('/random', randomWordRouter);
router.use('/words', require('./src/routers/word.js'));
router.use('/batch', require('./src/routers/batch.js'));
router.use('/random', require('./src/routers/random.js'));
router.use('/search', require('./src/routers/search'));

app.use('/api', router);

Expand Down
24 changes: 24 additions & 0 deletions docs/postman/Word-API.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,30 @@
},
"response": []
},
{
"name": "Search word",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{host}}{{port}}/api/search?word=Mellifluous",
"host": [
"{{host}}{{port}}"
],
"path": [
"api",
"search"
],
"query": [
{
"key": "word",
"value": "Mellifluous"
}
]
}
},
"response": []
},
{
"name": "Create multiple words - DE",
"event": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "server.js",
"scripts": {
"start": "node server.js",
"serve": "node_modules/.bin/nodemon server.js --exec",
"serve": "nodemon server.js --exec",
"test": "mocha --recursive --exit --timeout 1000000",
"lint": "eslint --fix --ext .js,.jsx ."
},
Expand Down
27 changes: 27 additions & 0 deletions src/routers/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const express = require('express');
const Word = require('../db/models/word.js');
const router = express.Router();
const { validLanguage } = require('../common/languageUtils');

router.route('/').get(function(req, res) {
const searchedWord = req.query.word;
if (!searchedWord) {
res.status(400).json({
message: 'Query parameter not present!',
});
return;
}
Word.findOne({ word: searchedWord }, function(err, word) {
if (err) {
res.send(err);
} else {
const msg = word ? 'Word found!' : 'No matching word found!';
res.json({
message: msg,
word,
});
}
});
});

module.exports = router;

0 comments on commit 6b31dd7

Please sign in to comment.