Skip to content

Commit

Permalink
Merge pull request #47 from andela/ft-search-article-functionality-16…
Browse files Browse the repository at this point in the history
…5413118

#165413118 search article functionality
  • Loading branch information
AJAkimana committed May 31, 2019
2 parents 1b1c62e + c162d12 commit 707798e
Show file tree
Hide file tree
Showing 6 changed files with 921 additions and 2 deletions.
187 changes: 187 additions & 0 deletions helpers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,5 +646,192 @@ class ArticleHelper {
}
return bookmarks;
}

/**
* @param {object} param - Request object
* @returns {object} response
* @static
*/
static async searchArticle(param) {
let result;
if (param.username) {
result = await Article.findAll({
include: [{
model: User, as: 'author', where: param, attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.title) {
result = await Article.findAll({
where: param,
include: [{
model: User, as: 'author', attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.name) {
result = await Article.findAll({
include: [{
model: User, as: 'author', attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', where: param, attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.keyword) {
result = await Article.findAll({
where: param.keyword,
include: [{
model: User, as: 'author', attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.authorTitle) {
result = await Article.findAll({
where: param.authorTitle[1],
include: [{
model: User, as: 'author', where: param.authorTitle[0], attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.authorTag) {
result = await Article.findAll({
include: [{
model: User, as: 'author', where: param.authorTag[0], attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', where: param.authorTag[1], attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.titleKeyword) {
result = await Article.findAll({
where: param.titleKeyword,
include: [{
model: User, as: 'author', attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.authorKeyword) {
result = await Article.findAll({
where: param.authorKeyword[1],
include: [{
model: User, as: 'author', where: param.authorKeyword[0], attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.titleTag) {
result = await Article.findAll({
where: param.titleTag[0],
include: [{
model: User, as: 'author', attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', where: param.titleTag[1], attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.tagKeyword) {
result = await Article.findAll({
where: param.tagKeyword[1],
include: [{
model: User, as: 'author', attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', where: param.tagKeyword[0], attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.authorTitleTag) {
result = await Article.findAll({
where: param.authorTitleTag[1],
include: [{
model: User, as: 'author', where: param.authorTitleTag[0], attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', where: param.authorTitleTag[2], attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.authorTitleKeyword) {
result = await Article.findAll({
where: param.authorTitleKeyword[1],
include: [{
model: User, as: 'author', where: param.authorTitleKeyword[0], attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.titleTagKeyword) {
result = await Article.findAll({
where: param.titleTagKeyword[0],
include: [{
model: User, as: 'author', attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', where: param.titleTagKeyword[1], attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.authorTagKeyword) {
result = await Article.findAll({
where: param.authorTagKeyword[2],
include: [{
model: User, as: 'author', where: param.authorTagKeyword[0], attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', where: param.authorTagKeyword[1], attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
if (param.allParams) {
result = await Article.findAll({
where: param.allParams[1],
include: [{
model: User, as: 'author', where: param.allParams[0], attributes: ['username', 'bio', 'image']
},
{
model: Tag, as: 'tagList', where: param.allParams[2], attributes: ['name']
}],
attributes: ['id', 'authorId', 'slug', 'title', 'description', 'readingTime', 'body', 'createdAt']
});
}
return result;
}
}
export default ArticleHelper;
Loading

0 comments on commit 707798e

Please sign in to comment.