-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(search-functionality): add search functionality and custom filtering
based on parameters - enable filter by author - enable filter by tag - enable filter by title - enable filter by keywords [Finishes #166790009]
- Loading branch information
1 parent
698c533
commit ed00e56
Showing
12 changed files
with
238 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './auth'; | ||
export * from './article'; | ||
export * from './comment'; | ||
export * from './search'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import searchAlgolia from 'algoliasearch'; | ||
import dotenv from 'dotenv'; | ||
import db from '../models'; | ||
import { sanitize } from '../helpers/searchSanitizer'; | ||
|
||
dotenv.config(); | ||
const { articles } = db; | ||
export const search = async (req, res) => { | ||
const client = searchAlgolia(process.env.ALGO_APP_ID, process.env.ALGO_SEARCH_ONLY); | ||
const index = client.initIndex('authors_haven'); | ||
const { | ||
tag, | ||
author, | ||
title, | ||
keyword | ||
} = req.query; | ||
try { | ||
const articlesResults = await articles.findAll(); | ||
index.addObjects(articlesResults); | ||
index.search(`${tag || author || title || keyword}`, (err, results) => { | ||
const sanitizedResults = sanitize(results.hits); | ||
if (sanitizedResults.length !== 0) { | ||
return res.status(200).json({ results: sanitizedResults, }); | ||
} | ||
return res.status(204).json({ error: 'no results' }); | ||
}); | ||
} catch (ex) { | ||
return res.status(500).json({ | ||
error: 'something went wrong', | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export const sanitize = (object) => { | ||
const articleHolder = []; | ||
const objectStr = Object.prototype.toString.call(object); | ||
if (objectStr === '[object Array]' && objectStr) { | ||
object.forEach((element) => { | ||
articleHolder.push( | ||
Object.keys(element).reduce((obj, key) => { | ||
if (key !== '_highlightResult' && key !== 'objectID') { | ||
obj[key] = element[key]; | ||
} | ||
return obj; | ||
}, {}) | ||
); | ||
}); | ||
} | ||
return articleHolder; | ||
}; | ||
|
||
export const isEmpty = (object) => { | ||
object.forEach((element) => { | ||
if (Object.prototype.hasOwnProperty.call(object, element)) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import express from 'express'; | ||
import { search } from '../../controllers'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/articles/', search); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.