-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- search from users table - search from articles table - search by article tags [Delivers #164489942]
- Loading branch information
1 parent
ba3b463
commit b4bdadf
Showing
10 changed files
with
196 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import Sequelize from 'sequelize'; | ||
import model from '../models/index'; | ||
import TagData from '../helpers/tags'; | ||
|
||
const { Op } = Sequelize; | ||
// @assign model | ||
const User = model.user; | ||
const Article = model.article; | ||
/** | ||
* @param {class} --Search controller | ||
*/ | ||
class searchController { | ||
/** | ||
* | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @returns {Object} all item related to item | ||
*/ | ||
static async searchItem(req, res) { | ||
// item from req.query | ||
const { search } = req.query; | ||
try { | ||
// first lets search from user | ||
const user = await User.findAll({ | ||
attributes: { exclude: ['password', 'isAdmin', 'isActivated', 'createdAt', 'updatedAt'] }, | ||
where: { username: { [Op.like]: `%${search}%` } }, | ||
include: [{ model: Article }] | ||
}); | ||
const article = await Article.findAll({ | ||
where: { title: { [Op.like]: `%${search}%` } }, | ||
include: [{ | ||
model: User, | ||
as: 'authorfkey', | ||
attributes: { exclude: ['password', 'isAdmin', 'isActivated', 'createdAt', 'updatedAt'] } | ||
}] | ||
}); | ||
const tags = await TagData(req); | ||
return res.status(200).json({ | ||
status: 200, user, article, tags | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ error: `something wrong try again later ${error}` }); | ||
} | ||
} | ||
} | ||
export default searchController; |
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,9 @@ | ||
import Validator from './validateUser'; | ||
|
||
const search = (req, res, next) => { | ||
if (Validator.isEmpty(req.query.search)) { | ||
return res.status(400).json({ error: ' no result found.'}); | ||
} | ||
next(); | ||
}; | ||
export default 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,24 @@ | ||
import model from '../models/index'; | ||
|
||
const Article = model.article; | ||
const tagFetch = async (req) => { | ||
const { search } = req.query; | ||
const arr = []; | ||
const taglist = []; | ||
const tag = await Article.findAll({ attributes: ['taglist'] }); | ||
tag.forEach((element) => { | ||
const list = element.taglist; | ||
list.forEach((ls) => { | ||
arr.push(ls); | ||
}); | ||
}); | ||
// find in tags | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i].indexOf(search) !== -1) { | ||
taglist.push(arr[i]); | ||
} | ||
} | ||
return taglist; | ||
}; | ||
|
||
export default tagFetch; |
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,13 @@ | ||
import express from 'express'; | ||
import Search from '../../controllers/search'; | ||
import validation from '../../helpers/search'; | ||
|
||
const router = express.Router(); | ||
|
||
// @method POST | ||
// @desc search item | ||
// @access public | ||
|
||
router.post('/', validation, Search.searchItem); | ||
|
||
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
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