-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(articleSearch/Filter): Implement search/filter article functi…
…onality - Implement filter by author username only - Implement filter by tag only - Implement search by keywords - Implement any combination of supplied filters [Delivers #163478809]
- Loading branch information
Showing
9 changed files
with
438 additions
and
150 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import Sequelize from 'sequelize'; | ||
import { User, Tag, Article } from '../../models'; | ||
|
||
const { Op } = Sequelize; | ||
let foundAuthorId; | ||
let foundArticles; | ||
let foundTag; | ||
|
||
const findAuthorId = async (username) => { | ||
try { | ||
const foundAuthorObj = await User.findOne({ | ||
where: { | ||
username: { [Op.eq]: username } | ||
} | ||
}); | ||
|
||
return foundAuthorObj.id; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
const findTag = async (id) => { | ||
try { | ||
const foundTagObj = await Tag.findByPk(id); | ||
|
||
return foundTagObj; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const getArticlesByAllParams = async (username, tagId, searchString) => { | ||
try { | ||
foundAuthorId = await findAuthorId(username); | ||
foundTag = await findTag(tagId); | ||
foundArticles = await foundTag.getArticles({ | ||
where: { | ||
authorId: { [Op.eq]: foundAuthorId }, | ||
[Op.or]: [ | ||
{ body: { [Op.iLike]: `%${searchString}%` } }, | ||
{ title: { [Op.iLike]: `%${searchString}%` } } | ||
] | ||
} | ||
}); | ||
|
||
return foundArticles; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const getArticlesBySearchTagParams = async (tagId, searchString) => { | ||
try { | ||
foundTag = await findTag(tagId); | ||
foundArticles = await foundTag.getArticles({ | ||
where: { | ||
[Op.or]: [ | ||
{ body: { [Op.iLike]: `%${searchString}%` } }, | ||
{ title: { [Op.iLike]: `%${searchString}%` } } | ||
] | ||
} | ||
}); | ||
|
||
return foundArticles; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const getArticlesBySearchAuthorParams = async (username, searchString) => { | ||
try { | ||
foundAuthorId = await findAuthorId(username); | ||
foundArticles = await Article.findAll({ | ||
where: { | ||
authorId: { [Op.eq]: foundAuthorId }, | ||
[Op.or]: [ | ||
{ body: { [Op.iLike]: `%${searchString}%` } }, | ||
{ title: { [Op.iLike]: `%${searchString}%` } } | ||
] | ||
} | ||
}); | ||
|
||
return foundArticles; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const getArticlesByTagAuthorParams = async (tagId, username) => { | ||
try { | ||
foundTag = await findTag(tagId); | ||
foundAuthorId = await findAuthorId(username); | ||
foundArticles = await foundTag.getArticles({ | ||
where: { | ||
authorId: { [Op.eq]: foundAuthorId } | ||
} | ||
}); | ||
|
||
return foundArticles; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const getArticlesBySearchParam = async (searchString) => { | ||
try { | ||
foundArticles = Article.findAll({ | ||
where: { | ||
[Op.or]: [ | ||
{ body: { [Op.iLike]: `%${searchString}%` } }, | ||
{ title: { [Op.iLike]: `%${searchString}%` } } | ||
] | ||
} | ||
}); | ||
|
||
return foundArticles; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const getArticlesByTagParam = async (tagId) => { | ||
try { | ||
foundTag = await findTag(tagId); | ||
foundArticles = await foundTag.getArticles(); | ||
|
||
return foundArticles; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const getArticlesByAuthorParam = async (username) => { | ||
try { | ||
foundAuthorId = await findAuthorId(username); | ||
foundArticles = await Article.findAll({ | ||
where: { | ||
authorId: { [Op.eq]: foundAuthorId } | ||
} | ||
}); | ||
|
||
return foundArticles; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; |
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.