-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
766 additions
and
252 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
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,83 @@ | ||
import _ from 'lodash'; | ||
import moment from 'moment'; | ||
import ArticleModel from '../services/article.service'; | ||
import UserModel from '../services/user.service'; | ||
import TagModel from '../services/tag.service'; | ||
import Util from '../helpers/util'; | ||
import Helper from '../helpers/helper'; | ||
|
||
const util = new Util(); | ||
/** | ||
* | ||
* | ||
* @class Report | ||
*/ | ||
class Search { | ||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @return {Json} return json object | ||
* @memberof Search | ||
*/ | ||
static async processSearchQuery(req, res) { | ||
const { q, offset, limit } = req.query; | ||
const searchQuery = JSON.parse(q); | ||
const articles = await ArticleModel.searchArticle(searchQuery.keyword, offset, limit); | ||
const foundArticles = _.map( | ||
articles, | ||
_.partialRight(_.pick, [ | ||
'authorId', | ||
'slug', | ||
'title', | ||
'description', | ||
'body', | ||
'taglist', | ||
'favorited', | ||
'favoritedcount', | ||
'flagged', | ||
'images', | ||
'views', | ||
'createdAt', | ||
]) | ||
); | ||
|
||
await Promise.all(foundArticles.map(async (article) => { | ||
try { | ||
const userDetails = await UserModel.getOneUser(article.authorId); | ||
const { username, image } = userDetails; | ||
const readTime = Helper.calculateReadTime(article.body); | ||
const timeAgo = moment(article.createdAt).fromNow(); | ||
article.readtime = readTime; | ||
article.username = username; | ||
article.userImage = image; | ||
article.timeCreated = timeAgo; | ||
return true; | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
})); | ||
const users = await UserModel.searchUser(searchQuery.user, offset, limit); | ||
const foundUsers = _.map( | ||
users, | ||
_.partialRight(_.pick, [ | ||
'username', | ||
'image', | ||
'bio', | ||
]) | ||
); | ||
const tags = await TagModel.searchTag('Tag', searchQuery.tag, offset, limit); | ||
const foundTags = _.map( | ||
tags, | ||
_.partialRight(_.pick, [ | ||
'name', | ||
]) | ||
); | ||
util.setSuccess(200, 'Search successful', { foundArticles, foundUsers, foundTags }); | ||
return util.send(res); | ||
} | ||
} | ||
|
||
export default Search; |
Oops, something went wrong.